我在Android Studio中,并且有一个包含两个标记的地图片段(com.google.android.gms.maps.SupportMapFragment)。一种是静态的,称为目标。另一个跟踪我的动作,称为“猎人”。它有效,但问题是猎人标记四处徘徊。我将更新间隔设置为15毫秒,但是以步行速度,每个块仅更新一次。
因此,如何正确添加标记来跟踪我的位置并使其具有合理的准确性并实时更新?还是我在这里做错了什么?
格雷格
onCreate()
setUpMapIfNeeded(); if (mMap != null) { LatLng Target = new LatLng(TargetLat, TargetLong); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Target, 15)); } LocationManager locationManager = (LocationManager) this.getSystemService(this.LOCATION_SERVICE); LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { makeUseOfNewLocation(location); } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) { registerForUpdates(); } public void onProviderDisabled(String provider) { deregisterForUpdates(); } }; locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); private void registerForUpdates() { _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_FREQUENCY_MILLIS, 0, _locationListener); }
位置更改时更新标记。当我们接近目标时,振动并更改文字。
private void makeUseOfNewLocation(Location loc) { HunterLat = loc.getLatitude(); HunterLong = loc.getLongitude(); if (Hunter != null) { LatLng NewLoc = new LatLng(HunterLat, HunterLong); Hunter.setPosition(NewLoc); TextView txtItem = (TextView) findViewById(R.id.ItemName); if (HunterLong > TargetLong - .0007 && HunterLong < TargetLong + .0007 && HunterLat > TargetLat - .0007 && HunterLat < TargetLat + .0007) { txtItem.setTextColor(Color.MAGENTA); txtItem.setText(ItemName); if (!Vibrated){ Vibrated = true; Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE); v.vibrate(500); } } else { txtItem.setTextColor(Color.BLACK); txtItem.setText(getResources().getString(R.string.hunt_item)); } } else { Hunter = mMap.addMarker(new MarkerOptions().position(new LatLng(HunterLat, HunterLong)).title("You")); BitmapDescriptor icon2 = BitmapDescriptorFactory.fromResource(R.drawable.ic_map_person); Hunter.setIcon(icon2); } }
设置地图并添加静态目标标记
private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) .getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { setUpMap(); } } } private void setUpMap() { Marker Target = mMap.addMarker(new MarkerOptions().position(new LatLng(TargetLat, TargetLong)).title(Location)); BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.ic_target); Target.setIcon(icon); }
看到其他问题后,非地图活动似乎正在获取onProviderEnabled()回调,但地图活动未获取回调(除非在使用地图活动期间启用了GPS无线电)。
onProviderEnabled()
而不是只调用registerForUpdates()在onProviderEnabled()回调中,调用它onCreate(),如果GPS无线电被启用,和回落到网络位置,如果GPS被禁用,网络位置已启用。
registerForUpdates()
onCreate()
像这样:
LocationManager locationManager = (LocationManager) this.getSystemService(this.LOCATION_SERVICE); if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { registerForUpdates(); } else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); }
还要注意的另一件事是,您应该onPause()为请求位置更新的活动从覆盖中的所有位置回调中取消注册,以免造成应用程序不必要的电池消耗。
onPause()