【问题标题】:How to get devices coordinate the correct, fast and accurate way?如何让设备以正确、快速和准确的方式进行协调?
【发布时间】:2015-05-29 12:45:10
【问题描述】:

一年前,我搜索了如何让设备经久耐用。我在这里的一篇文章中发现,正确的方法是让我的活动实现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;
  }

我现在再次尝试执行此操作,并使用了一段时间未使用的手机,我可以获取位置。顺便说一下我打开的时候

谷歌地图

几秒钟后,应用程序找到了我的坐标。我确定我做错了什么。我怎样才能像谷歌地图一样高效地定位设备的坐标?

【问题讨论】:

  • 我无法得到您真正想要的,但是..FusedLocationAPI 可能是最好的解决方案。

标签: android geolocation location coordinates


【解决方案1】:

挑战在于 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 2019-06-07
    • 2019-01-27
    • 2012-01-07
    相关资源
    最近更新 更多