如何让设备协调正确,快速,准确的方式?

时间:2015-05-29 12:45:11

标签: android geolocation location coordinates

一年前,我已经搜索了如何让设备拉长。我在这里的一篇文章中发现,正确的方法是我的活动实现LocationListener并且我已经使用了为实现它而提供的代码并且它运行良好。

        public Location getLocation() {

         LocationManager mLocationManager;
         Location mLocation = null;

      try {
          mLocationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);

          // getting GPS status
          boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

          // getting network status
          boolean isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

          if (!isGPSEnabled && !isNetworkEnabled) {
            enableLocationProvider();
          } else {
              // First get location from Network Provider
              if (isNetworkEnabled) {
                  mLocationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER,  20000,  1, this);
                  Log.d("Network", "Network");
                  if (mLocationManager != null) {
                      mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                      if (mLocation != null) {
                          double lat = mLocation.getLatitude();
                          double lng = mLocation.getLongitude();
                      }
                  }
              }
              //get the location by gps
              if (isGPSEnabled) {
                  if (mLocation == null) {
                      mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,20000,1, this);
                      Log.d("GPS Enabled", "GPS Enabled");
                      if (mLocationManager != null) {mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                          if (mLocation != null) {
                            double lat = mLocation.getLatitude();
                            double lng = mLocation.getLongitude();
                          }
                      }
                  }
              }
          }

      } catch (Exception e) {
          e.printStackTrace();
      }

      return mLocation;
  }

我现在再次试图这样做并使用了一段时间没有使用的手机,我可以获得该位置。顺便说一句,我打开了

  

谷歌地图

几秒钟后,应用程序找到我的坐标。我确定我做错了什么。如何像谷歌地图那样有效地找到设备的坐标?

1 个答案:

答案 0 :(得分:0)

挑战是GPS需要一些时间进行初始化,同时如果您尝试访问该位置,它将为您提供空值。到目前为止,这是我在Stack Overflow中找到的最佳方法。它的鲁棒性并避免获得空值,并确保在GPS完成初始化时返回位置数据

用于获取位置更改的MyLocation类,

public class MyLocation {
    Timer timer1;
    LocationManager lm;
    LocationResult locationResult;
    public boolean gps_enabled=false;
    boolean network_enabled=false;

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

        //exceptions will be thrown if provider is not permitted.
        try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
        try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}

        //don't start listeners if no provider is enabled
        if(!gps_enabled && !network_enabled)
            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(), 20000);
        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() {
             lm.removeUpdates(locationListenerGps);
             lm.removeUpdates(locationListenerNetwork);

             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);
    }
}

要接收位置更新,请覆盖gotLocation()方法,该方法会在位置发生变化时为您提供数据。

MyLocation.LocationResult locationResult = new MyLocation.LocationResult() {
                        @Override
                        public void gotLocation(Location location) {
                            //Got the location!


                        }
                    };

                    MyLocation myLocation = new MyLocation();
                    myLocation.getLocation(getActivity(), locationResult);