6

当我的地图活动被调用时,我会在 onCreate 中调用 addUserMapPoint。此函数包含两个实例,我尝试使用 myOverlay.getMyLocation 获取位置信息。在此活动的初始加载中,第一次尝试的结果返回一个空 GeoPoint,并且在主 UI 线程完成位于 myOverlay.runOnFirstFix(new Runnable()... 的侦听器线程中的第二次尝试后,在一秒钟后调用并且确实包含一个包含纬度和经度的 GeoPoint。此侦听器函数内部的调用似乎确实将点放在地图上,并且 mapController.animateTo(gp) 行确实将地图移动到我的位置。我的应用程序有一个刷新按钮,当单击再次触发此活动。我需要纬度和经度才能从另一个服务获取位置数据。刷新后,

如果我无法通过第一次调用 myOverlay.getMyLocation 来获取 GeoPoint,那么如何从 myOverlay.runOnFirstFix(new Runnable()... 线程中找到的第二次调用中传递纬度和经度值。你会注意到我一直在尝试将 lat 和 lon 添加到 MyApp 这是 helper bean 类但是即使在刷新后这个类中的 lat 和 lon 也是 null 如果我在第一次活动时手动在 addUserMapPoint 函数中手动设置 lat 和 lon被访问这些值被保留。我猜这是因为它是在主 UI 线程上设置的。

public class MapActivity extends com.google.android.maps.MapActivity {
    private MapView mapView = null;
    private MapController mapController = null;
    private MyLocationOverlay myOverlay = null;

    public static MyApp app;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        app = (MyApp) getApplicationContext();

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapController = mapView.getController();

        List<Overlay> mapOverlays = mapView.getOverlays();
        mapOverlays.clear();

        addUserMapPoint(mapView);

        if (!app.isLocServOff()) {
            //map other points – service call to get items from our service near lat and lon
            addOtherMapPoints(mapOverlays);

        } else {
            Toast.makeText(app.getApplicationContext(),"Current location could not be found.",Toast.LENGTH_LONG).show();
        }

    }

    private void addUserMapPoint(MapView mapView){
            myOverlay = new MyLocationOverlay(app.getApplicationContext(), mapView);
            myOverlay.disableCompass();
        myOverlay.enableMyLocation();


            if(app.getMyLat()==null||app.getMyLon()==null){
                GeoPoint gp = myOverlay.getMyLocation();
                if(gp!=null){
                    app.setMyLat(Helper.latLon1E6Calc(gp.getLatitudeE6()));
                    app.setMyLon(Helper.latLon1E6Calc(gp.getLongitudeE6()));
                    app.setLocServOff(false);
                }else{
                    app.setLocServOff(true);
                }
        }


        myOverlay.runOnFirstFix(new Runnable() {   
                public void run() {     
                    GeoPoint gp = myOverlay.getMyLocation();    
                    if(gp!=null){
                        app.setMyLat(Helper.latLon1E6Calc(gp.getLatitudeE6()));
                        app.setMyLon(Helper.latLon1E6Calc(gp.getLongitudeE6()));
                        app.setLocServOff(false);   
                        mapController.animateTo(gp);
                    }else{
                        app.setLocServOff(true);
                    }
                }    
            });

        mapView.getOverlays().add(myOverlay);
    }   

}

以下问题正在请求您的帮助。如何在主 UI 线程中获取包含 lat 和 lon 的 GeoPoint,或者如何从 GeoPoint 传递这些值,我可以从 myOverlay.runOnFirstFix(new Runnable()... 线程获取?

如果您要建议我使用 Handler 或 runOnUiThread,请提供代码示例,将 lat 和 lon 传递回主 UI 线程/地图视图可以使用的东西。我已经尝试过类似下面的代码,但没有产生预期的结果。我能够让 toast 消息显示出来,但无法以我可以使用的方式传递 lat 和 lon。

    final Handler handler = new Handler();   
    myOverlay.runOnFirstFix(new Runnable() {                     
        @Override public void run() {

            handler.post(new Runnable() {                                   
                @Override public void run() {

                        GeoPoint gp = myOverlay.getMyLocation();    
                        if(gp!=null){
                            app.setMyLat(Helper.latLon1E6Calc(gp.getLatitudeE6()));
                            app.setMyLon(Helper.latLon1E6Calc(gp.getLongitudeE6()));
                            app.setLocServOff(false);   
                            mapController.animateTo(gp);                                

                        }else{
                            app.setLocServOff(true);
                        }

                    //Toast.makeText(getApplicationContext(),"wowoowowowoowoowowow",Toast.LENGTH_LONG).show();                      
                    }                                  
                });

            }                
        }); 

我还使用如下代码来获取纬度和经度并且它可以工作,但是因为当前位置有时会与返回的纬度和经度不同,因为例如我无法获得 gps 信号但是返回旧值。我添加了检查以查看纬度/经度数据是否超过 2 分钟,但我仍然无法将最新的经度和经度与 myOverlay.getMyLocation 返回的经度匹配。

    LocationManager locMgr = (LocationManager)appcontext.getSystemService(Context.LOCATION_SERVICE);
    MyLocationListener locLstnr = new MyLocationListener();

    //fetch current location for current location 
    locMgr.requestSingleUpdate(LocationManager.GPS_PROVIDER, locLstnr, appcontext.getMainLooper());  
4

5 回答 5

3

下面你可以找到一些关于如何在 UI 线程中获取当前位置的示例,但首先是一些背景信息。

GPS在请求新位置后,可能需要一些时间(15 秒到 1 分钟)才能获得第一个修复。这就是您第一次尝试获取它myOverlay失败的原因,并且只有在第一次修复之后您才能获得价值。

在此停电getLastKnownLocation()期间,如果您赶时间,您可以使用它来获取最后一个众所周知的 GPS 位置。如果不可用,则返回null

编码:

最后已知位置

LocationManager locMgr=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
Location loc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(loc != null){
    //we have a valid location. Check location date
}

请求单一位置更新

LocationManager locMgr=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
locMgr.requestSingleUpdate(LocationManager.GPS_PROVIDER, locationListener, appcontext.getMainLooper);

请求持续位置更新

LocationManager locMgr = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
//use 0 for minDistance and minDistance between updates if you need the maximum update frequency.
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, minDistance, minTime, locationListener);

用于单次和连续位置更新的位置监听器

这是最后一段代码,也是您获取上面请求的新位置的地方。

当 GPS 检索到与您上面定义的请求条件匹配的新位置时,会立即调用此侦听器,除非您的设备正忙于做其他无法中断的事情(即回调在暂停线程上或锁定) .

从内部onLocationChanged()您可以根据需要设置任何类级别。如果您从 UI 线程注册了侦听器,那么它将在 UI 上运行。

LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location fix) {
        fix.setTime(fix.getTime() + timeZoneOffset); //Add Timezone offset if needed
        //here you have a fresh new location in fix...
        //You can set the value of any class level field from here
    }

    public void onProviderDisabled(String provider) {
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
};

问候。

于 2012-12-22T01:24:33.590 回答
2

在寻找设备的位置时,您必须考虑的一些最重要的点是:

  1. 不能保证在足够的时间内收到卫星 GPS 定位。例如,设备在建筑物内/不在开阔的天空下。
  2. 确保卫星 GPS 监听器不会长时间保持活动状态。让听者保持开启状态将意味着 GPS 收音机一直处于开启状态,这使其成为电池消耗最大的原因。

在下面的代码示例中,LinkedBlockingQueue 中的 poll 方法直到指定的时间间隔结束或 Location 排队时才会返回。

使用类似下面的内容来获取当前位置:

    Location getCurrentLocation() {
    long startmillis = 0;
    LinkedBlockingQueue<Location> mQueue = new LinkedBlockingQueue<Location>();
            try{


                long millisSinceLastCollection = System.currentTimeMillis() - startmillis; 


                startmillis = System.currentTimeMillis();
                mQueue.clear();

    // Register for Satellite GPS listener as well as Network GPS listener.
                registerGPSListeners();

                // Wait for a maximum of one minutes for a fix
                Location firstfix = mQueue.poll(1, TimeUnit.MINUTES);


                if(firstfix != null && firstfix.getProvider().equals(LocationManager.GPS_PROVIDER)) {
                    return firstfix;
                }

                long elapsedmillis = System.currentTimeMillis() - startmillis;
                long remainingmillis = ONE_MINUTE_IN_MS - elapsedmillis; 
                if (remainingmillis <= 0){
                    return firstfix;
                }

                Location secondfix = mQueue.poll(remainingmillis, TimeUnit.MILLISECONDS);

                if(secondfix != null && secondfix.getProvider().equals(LocationManager.GPS_PROVIDER)) {
                    return secondfix;
                }

                /*
                 * In case we receive fix only from Network provider, return it.
                 */
                if(firstfix != null && firstfix.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
                    return firstfix;
                }

            } catch(Exception e){
                Logger.e("GPS: Exception while listening for the current location", e);
            } finally {
                Logger.i("GPS: Unsubscribing from any existing GPS listeners");
                unregisterGPSListeners();

            }
    }


// GPS issue fix edit.
    private void registerGPSListeners() {

        LocationManager locationManager = (LocationManager)AirWatchApp.getAppContext().getSystemService(Context.LOCATION_SERVICE);

        if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30000, 100, oneShotNetworkGPSLocationListener, MyAppApp.getAppContext().getMainLooper());

        if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100, oneShotSatelliteGPSLocationListener, AirWatchApp.getAppContext().getMainLooper());
        }
    }

    private void unregisterGPSListeners(){

        final LocationManager locationManager = (LocationManager)MyApp.getAppContext().getSystemService(Context.LOCATION_SERVICE);
        locationManager.removeUpdates(oneShotSatelliteGPSLocationListener);
        locationManager.removeUpdates(oneShotNetworkGPSLocationListener);
    }

//One shot location listener
    protected LocationListener oneShotSatelliteGPSLocationListener = new LocationListener() {
        public void onLocationChanged(Location location) {

            try {
                mQueue.put(location);
            } catch (InterruptedException e) {
                Logger.e("Exception in putting new Location to the queue", e);
            }

            Logger.d("GPS: Location received from Satellite GPS Provider");
            unregisterGPSListeners();
        }

        public void onProviderDisabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
        public void onProviderEnabled(String provider) {}
    };

    //One shot location listener
    protected LocationListener oneShotNetworkGPSLocationListener = new LocationListener() {
        public void onLocationChanged(Location location) {

            try {
                mQueue.put(location);
            } catch (InterruptedException e) {
                Logger.e("Exception in putting new Location to the queue", e);
            }

            Logger.d("GPS: Location received from Network GPS Provider");
            // Stop Listener for one-shot location fix from Network GPS provider.
            final LocationManager locationManager = (LocationManager)AirWatchApp.getAppContext().getSystemService(Context.LOCATION_SERVICE);
            locationManager.removeUpdates(oneShotNetworkGPSLocationListener);
            Logger.d("GPS: Unsubscribed the network location listener.");
        }

        public void onProviderDisabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
        public void onProviderEnabled(String provider) {}
    };
于 2012-12-27T17:13:02.307 回答
2
handler.post(new Runnable() {                                   
            @Override public void run() {

                    GeoPoint gp = myOverlay.getMyLocation();    
                    if(gp!=null){
                        app.setMyLat(Helper.latLon1E6Calc(gp.getLatitudeE6()));
                        app.setMyLon(Helper.latLon1E6Calc(gp.getLongitudeE6()));
                        app.setLocServOff(false);

                        // HERE WE HAVE VALID gp VALUE AND WE NEED TO SHARE IT

                        mapController.animateTo(gp);                                

                    }else{
                        app.setLocServOff(true);
                    }
                }                                  
            });

我认为您的 app.set/get|MyLat/Lon 无法正常工作,因为您从不同的线程调用它们。修复它同步设置和获取 MyLat/Long 的方法。(创建对象进行同步并在其上同步)

或者,如果您喜欢处理程序的方式,这应该可以:

final Handler handler = new Handler(); // BE SURE TO RUN THIS LINE ON UI THREAD
...   
myOverlay.runOnFirstFix(new Runnable() {                     
    @Override public void run() {

        // THIS PART WORKS AS BEFORE
        final GeoPoint gp = myOverlay.getMyLocation();
        mapController.animateTo(gp);                        
        ...
        // AND THIS RUNNABLE TO UPDATE MyLat/MyLong FROM UI THREAD
        handler.post(new Runnable() {                                   
            @Override public void run() {
               app.setMyLat(Helper.latLon1E6Calc(gp.getLatitudeE6()));
               app.setMyLon(Helper.latLon1E6Calc(gp.getLongitudeE6()));
            });

        }                
    }); 
于 2012-12-28T13:21:33.510 回答
2

Android 修改用户界面并处理来自单个用户界面线程(主线程)的输入事件。

如果程序员不使用任何并发构造,Android 应用程序的所有代码都在此线程中运行。

GPS是确定用户位置的最佳方法,但是过多地 ping 全球定位卫星会很快耗尽移动设备的电池,需要很长时间才能获取用户位置,而且这种方法在室内并不总是有效。You are not getting your location in first attempt that's why you are getting null over there.

AndroidNetwork Location Provider根据手机信号塔和 Wi-Fi 信号计算出用户的位置。它不仅比 GPS 使用更少的电池电量,而且速度更快,无论用户在室外还是室内,它都可以工作。

我 在该节目下方提供我的工作代码progress dialog,听用户的位置并在谷歌地图location overlay上获取位置显示用户后

我假设您在Menifest file
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
我的主要课程中具有以下权限

public class MyLocationOnMap extends MapActivity {

private MapView mapView;
private MyLocationOverlay itemizedoverlay;
private LocationManager locationManager;
private String provider;
private MyLocationListener locationListener;
MyBroadCastreceiver myBroadCastreceiver;
/**
 * My current Location <i>longitude</i>.
 */
static int longitude;
/**
 * My current Location <i>latitude</i>.
 */
static int latitude;
/**
 *My progress indicator. 
 */
ProgressDialog loadingDialog;

public static final String INTENT_FILTER_TAG="my location broadcast receiver";


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_location_on_map);

    loadingDialog = new ProgressDialog(this);
    loadingDialog.setTitle("Hot Spots!");
    loadingDialog.setMessage("Please wait ...");
    loadingDialog.setIndeterminate(true);
    loadingDialog.setCancelable(false);

    loadingDialog.show();

 // Configure the Map
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapView.setStreetView(true);

    /**
     * Get your location manager and Location Listener...
     */
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationListener=new MyLocationListener();

    myBroadCastreceiver = new MyBroadCastreceiver();

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Log.i("GPS_Enabled", "GPS enable! listening for gps location.");
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, locationListener);
            registerReceiver(myBroadCastreceiver, new IntentFilter(INTENT_FILTER_TAG));
        } else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            Log.i("Network_Enabled", "Network enable! listening for Network location.");
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, locationListener);
            registerReceiver(myBroadCastreceiver, new IntentFilter(INTENT_FILTER_TAG));
        } else {
            loadingDialog.dismiss();
            Toast.makeText(this, "No Provider enable!", Toast.LENGTH_LONG).show();
        }

}//End of onCreate......

 /**
 * My BroadCast Receiver, that is called when i get the location of user.
 * @author Rupesh Yadav.
 *
 */
class MyBroadCastreceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        //Remove location update when you get user`s location very first time.
        locationManager.removeUpdates(locationListener);
        //Remove the broadcast listener that update my location on map.
        unregisterReceiver(myBroadCastreceiver);

        GeoPoint point = new GeoPoint(latitude, longitude);
        mapView.getController().animateTo(point);

        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = MyLocationOnMap.this.getResources().getDrawable(R.drawable.hs_mapoverlay);
        itemizedoverlay = new MyLocationOverlay(drawable, MyLocationOnMap.this);

        OverlayItem overlayitem = new OverlayItem(point, "Hello!", "My Current Location :)");

        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);

        loadingDialog.dismiss();

    }

}

/**
 * My Location listener...
 */
class MyLocationListener implements LocationListener{
    @Override
    public void onLocationChanged(Location location) {
        latitude=(int) ((location.getLatitude())*1E6);
        longitude=(int) ((location.getLongitude())*1E6);

        //Send broadcast to update my location.
        Intent sendLocationIntent=new Intent(INTENT_FILTER_TAG);
        sendBroadcast(sendLocationIntent);
    }
    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }

}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}

MyLocationOverlay 类

public class MyLocationOverlay extends ItemizedOverlay<OverlayItem> {

Context mContext;
private ArrayList<OverlayItem> hsOverlays = new ArrayList<OverlayItem>();

public MyLocationOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    // TODO Auto-generated constructor stub
}

public MyLocationOverlay(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker));
    mContext = context;
}

@Override
protected OverlayItem createItem(int i) {
    // TODO Auto-generated method stub
    return hsOverlays.get(i);
}

@Override
public int size() {
    // TODO Auto-generated method stub
    return hsOverlays.size();
}

/**
 * add new OverlayItem objects to map OverlayItem ArrayList.
 * 
 * @param overlay
 */
public void addOverlay(OverlayItem overlay) {
    hsOverlays.add(overlay);
    populate();
}

/**
 * Called when user clicks on map overlay.
 */
@Override
protected boolean onTap(int index) {
    // TODO Auto-generated method stub
    // return super.onTap(index);
    OverlayItem item = hsOverlays.get(index);
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
    dialog.setTitle(item.getTitle());
    dialog.setMessage(item.getSnippet());
    dialog.show();
    return true;
}

您可以根据需要修改Location Listener& 。Broadcasr Receiver我希望这能解决你的问题。
最好的祝福!

于 2012-12-28T13:52:10.583 回答
2

我已经使用这个类来检测我的纬度和经度:希望这对你也有用。

使用示例: GPSUtility.getInstance(Context).getLatitude(); GPSUtility.getInstance(CamPhotoModeAct.this).getLongitude()

public class GPSUtility {
    public static final String TAG = "GPSUtility";

    private Context ctx;

    Timer timer1;
    LocationManager lm;
    LocationResult locationResult;
    boolean gps_enabled=false;
    boolean network_enabled=false;

    private double latitude;
    private double longitude;

    private static SharedPreferences SHARED_PREF;
    private static SharedPreferences.Editor EDITOR_SHARED_PREF;

    private static GPSUtility this_instance;

    public GPSUtility(Context ctx){
        this.ctx = ctx;
        SHARED_PREF = ctx.getSharedPreferences(ConstantsG.SHARED_PREF_FILE, Context.MODE_PRIVATE);
        EDITOR_SHARED_PREF = SHARED_PREF.edit();
        this.getLocation(innerLocationResult);
    }

    public static GPSUtility getInstance(Context ctx){
        if(this_instance == null)
            this_instance = new GPSUtility(ctx);
        return this_instance;
    }

    public static void updateLocation(Context ctx){
        GPSUtility.getInstance(ctx);//this writes the latitude and longitude in sharable preference file
    }

    public double getLatitude(){
        String latitudeStr = SHARED_PREF.getString(ConstantsG.KEY_LATITUDE,null);
        if(latitudeStr == null){
            latitude = 0.0;
        }
        else{
            latitude = Double.parseDouble(latitudeStr);
        }
        return latitude;
    }

    public double getLongitude(){
        String longitudeStr = SHARED_PREF.getString(ConstantsG.KEY_LONGITUDE,null);
        if(longitudeStr == null){
            longitude = 0.0;
        }
        else{
            longitude = Double.parseDouble(longitudeStr);
        }
        return longitude;
    }

    private void updateWithNewLocation(Location location) {

        if (location != null) {
            latitude = location.getLatitude();
            EDITOR_SHARED_PREF.putString(ConstantsG.KEY_LATITUDE, String.valueOf(latitude) );

            longitude = location.getLongitude();
            EDITOR_SHARED_PREF.putString(ConstantsG.KEY_LONGITUDE, String.valueOf(longitude));

            EDITOR_SHARED_PREF.commit();
        }
    }

    public boolean getLocation(LocationResult result)
    {
        //I use LocationResult callback class to pass location value from GPSUtility to user code.
        locationResult=result;
        if(lm==null)
            lm = (LocationManager) this.ctx.getSystemService(Context.LOCATION_SERVICE);

        //exceptions will be thrown if provider is not permitted.
        try {
            gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {
            Log.e(TAG, "Exception error: " + ex.getLocalizedMessage(), ex);
        }
        try {
            network_enabled = lm
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {
            Log.e(TAG, "Exception error: " + ex.getLocalizedMessage(), ex);
        }

        //Toast.makeText(context, gps_enabled+" "+network_enabled,     Toast.LENGTH_LONG).show();

        //don't start listeners if no provider is enabled
        if(!gps_enabled && !network_enabled){
            Toast.makeText(this.ctx, "You should enable gps or be connected to network.", Toast.LENGTH_LONG).show();
            return false;
        }

        if(gps_enabled)
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
        if(network_enabled)
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        timer1=new Timer();


        timer1.schedule(new GetLastLocation(), 10000);
        return true;
    }

    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    class GetLastLocation extends TimerTask {
        @Override
        public void run() {
            //Context context = getClass().getgetApplicationContext();
             Location net_loc=null, gps_loc=null;
             if(gps_enabled)
                 gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
             if(network_enabled)
                 net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

             //if there are both values use the latest one
             if(gps_loc!=null && net_loc!=null){
                 if(gps_loc.getTime()>net_loc.getTime())
                     locationResult.gotLocation(gps_loc);
                 else
                     locationResult.gotLocation(net_loc);
                 return;
             }

             if(gps_loc!=null){
                 locationResult.gotLocation(gps_loc);
                 return;
             }
             if(net_loc!=null){
                 locationResult.gotLocation(net_loc);
                 return;
             }
             locationResult.gotLocation(null);
        }
    }


    public static abstract class LocationResult{
        public abstract void gotLocation(Location location);
    }

    LocationResult innerLocationResult = new LocationResult() {
        @Override
        public void gotLocation(Location location) {
            updateWithNewLocation(location);
        }
    };
}
于 2012-12-28T15:36:08.857 回答