【发布时间】: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