java - 为什么 map 标记会在 map 周围徘徊

标签 java android google-maps google-maps-android-api-2

我在 Android Studio 中,我有一个具有两个标记的 map fragment (com.google.android.gms.maps.SupportMapFragment)。一个是静态的,称为目标。另一个跟踪我的 Action ,被称为猎人。它可以工作,但问题是 Hunter 标记会四处晃动。我将更新间隔设置为 15 毫秒,但在步行速度下,它每个 block 只更新一次。

那么,我该如何正确添加标记来跟踪我的位置并使其相当准确并实时更新呢?或者我在这里做错了什么?

格雷格

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

    }
}

设置 map 并添加静态目标标记

   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之后,看起来非 map Activity 正在获取 onProviderEnabled() 回调,但您的 map Activity 没有获取它(除非在使用 map Activity 期间启用了 GPS radio )。

与其仅在 onProviderEnabled() 回调中调用 registerForUpdates(),不如在 onCreate() 中调用它(如果启用了 GPS radio ) , 如果禁用 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() 重写中的所有位置回调中取消注册,这样您就不会因为您的应用程序。

关于java - 为什么 map 标记会在 map 周围徘徊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30824549/

相关文章:

java - 将报告导出为 html 格式

java - Edittext 和 ProgressDialog 均不出现

java - 单例返回多个实例

google-maps - 谷歌地图 v3 与 cordova/phonegap 模糊

google-maps - Delphi 的谷歌地图库突然停止工作?

ios - 使用 url 方案将标记添加到 ios 中的谷歌地图

Java 套接字事件驱动

java - Selenium Webdriver 和 Firefox Server Not Found 错误

javascript - 如何找到 document.DocumentElement.Client 的坐标

java - Android 的浮点到 double 转换