为什么地图标记会在地图周围徘徊

Why Does Map Marker Lurch Around The Map

我在 Android Studio 中,我有一个带有两个标记的地图片段 (com.google.android.gms.maps.SupportMapFragment)。一个是静态的,称为目标。另一个跟踪我的动作,被称为猎人。它可以工作,但问题是 Hunter 标记会四处晃动。我将更新间隔设置为 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);
}

看到你的 other question 后,看起来非地图 Activity 得到了 onProviderEnabled() 回调,但你的地图 Activity 没有得到它(除非在使用地图期间启用了 GPS 无线电 Activity)。

如果启用了 GPS 无线电,则在 onCreate() 中调用它,而不是仅在 onProviderEnabled() 回调中调用它,如果禁用 GPS 则回退到网络位置,并且网络位置已启用。

像这样:

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

另一件需要注意的事情是,对于请求位置更新的 Activity,您应该从 onPause() 覆盖中的所有位置回调中取消注册,这样您的应用程序就不会造成不必要的电池耗尽。

我使用的是网络而不是 GPS。

已更改

 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

它现在可以流畅地跟踪我的动作。感谢 Daniel Nugent 向我指出了这一点 post.

格雷格