为什么地图标记在地图上徘徊

时间:2015-06-13 23:27:43

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

我在Android Studio中,我有一个有两个标记的地图片段(com.google.android.gms.maps.SupportMapFragment)。一个是静态的,称为Target。另一个追踪我的动作,被称为猎人。它有效,但问题是猎人标记徘徊。我的更新间隔设置为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);
}

2 个答案:

答案 0 :(得分:1)

看到你的other question后,看起来非地图活动正在获得onProviderEnabled()回调,但你的地图活动没有得到它(除非在使用地图时启用了GPS广播活性)。

如果启用了GPS广播,则可以在registerForUpdates()中呼叫onProviderEnabled(),如果禁用了GPS,则可以回拨到网络位置,而不是仅在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()

需要注意的另一点是,您应该从请求位置更新的活动fulfill覆盖中的所有位置回调中取消注册,这样您的应用就不会造成不必要的电池消耗。

答案 1 :(得分:0)

我使用网络代替GPS。

更改

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

fn read_shader_code(string_path: &str) -> String {
    let path = Path::new(string_path);
    let display = path.display();

    let mut file = match File::open(&path) {
        Err(why) => panic!("Couldn't open {}: {}", display, Error::description(&why)),
        Ok(file) => file,
    };

    let mut s = String::new();
    match file.read_to_string(&mut s) {
        Err(why) => panic!("couldn't read {}: {}", display, Error::description(&why)),
        Ok(_) => s,
    }
}

它现在可以顺利地跟踪我的动作。感谢Daniel Nugent向我指出另一篇文章。

格雷格