【问题标题】:How to separate location listener and Google map如何分离位置监听器和谷歌地图
【发布时间】:2014-07-13 04:39:42
【问题描述】:

我正在尝试在安卓手机上制作一个位置跟踪应用程序,它与手机中内置的谷歌地图一样。即使应用程序关闭,它也会继续跟踪手机,因此它将在其他应用程序的后台运行。

我正在考虑拆分用户界面(谷歌地图)和位置跟踪这两个类,其中位置跟踪将始终运行,但谷歌地图只会在用户打开应用程序时显示。因此,这就是我目前所达到的目标。

Google 地图的 MapDisplay 类

public class MapDisplay extends Activity {

    private GoogleMap map;
    private LocationService locationService;
    private Intent locationServiceIntent;

    private Location myLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_display);
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.googleMap)).getMap();
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        map.setMyLocationEnabled(true);

        locationServiceIntent = new Intent(this, LocationService.class);
        x=0;
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        startService(locationServiceIntent);
        bindService(locationServiceIntent, locationServiceConnection, Context.BIND_AUTO_CREATE);

    }

    @Override
    public void updateLocation()
    {
        // for testing purpose, to make sure it will update periodically
        Toast.makeText(MapDisplay.this, String.valueOf(myLocation.getLatitude()), Toast.LENGTH_SHORT).show();
    }

    private ServiceConnection locationServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            locationService = ((LocationService.LocationBinder)service).getService();
            myLocation = locationService.getMyLocation();
            updateLocation();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
             locationService = null;
        }
    };
}

定位服务类

public class LocationService extends Service implements LocationListener
{

    private LocationManager locationManager;
    private Location myLocation;
    private IBinder locationBinder = new LocationBinder();

    public class LocationBinder extends Binder
    {
        public LocationService getService()
        {
            return LocationService.this;
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        return locationBinder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
    {
        locationManager   = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        myLocation = null;

        boolean networkProviderAvailable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        boolean gpsProviderAvailable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if(networkProviderAvailable)
        {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
        }
        else if(gpsProviderAvailable)
        {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        }
        else
        {
            stopSelf();
        }  
        return super.onStartCommand(intent, flags, startId);
    }
    public Location getMyLocation()
    {
        return myLocation;
    }

    @Override
    public void onLocationChanged(Location location) {
        myLocation = location;
    }

    @Override
    public void onProviderDisabled(String arg0) {
    }

    @Override
    public void onProviderEnabled(String arg0) {
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    }
}

使用上面的代码,我遇到了应用程序崩溃,因为这行

myLocation = locationService.getMyLocation();
String.valueOf(myLocation.getLatitude())

它无法从 LocationService 类中获取位置。

所以,我的问题是:

  • 1) 这是制作应用程序的好方法吗?
  • 2) onLocationChanged 方法有新更新时,如何定期调用 MapDisplay 中的 updateLocation()?

如果有人可以帮助我,我将不胜感激。

【问题讨论】:

    标签: android google-maps location locationlistener


    【解决方案1】:

    您必须考虑尚未收到位置的情况。为此,您的客户端代码需要检查 getLocation 的返回是否为 null,并进行相应处理。

    此外,您可能希望在 onLocationChanged 中获得更多智能。现在,如果一个 gps 位置进入,然后一个网络位置进入,网络位置将覆盖 gps 位置,尽管不太准确。我会看一下我在http://gabesechansoftware.com/location-tracking/ 写的一些代码,你会看到一个类 FallbackLocationTracker,它将跟踪 gps 和网络位置,返回迄今为止收到的最准确的位置。我认为它可以满足您的要求。

    【讨论】:

      猜你喜欢
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      相关资源
      最近更新 更多