Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法从google地图中删除标记_Android_Google Maps_Location_Google Maps Markers_Google Maps Api 2 - Fatal编程技术网

Android 无法从google地图中删除标记

Android 无法从google地图中删除标记,android,google-maps,location,google-maps-markers,google-maps-api-2,Android,Google Maps,Location,Google Maps Markers,Google Maps Api 2,我正在用android开发一个小应用程序来学习,并作为我最后一年的项目 在这篇文章中,我找到了用户位置,并在谷歌地图中添加了一个标记 但现在我无法移除它 我已经尝试了Clear()方法和remove()方法 这是我的密码 getUserLocation(); Toast.makeText(this, "Latitude:" + lat + " Longitude:" + lang, Toast.LENGTH_LONG).show(); getAddress(l

我正在用android开发一个小应用程序来学习,并作为我最后一年的项目

在这篇文章中,我找到了用户位置,并在谷歌地图中添加了一个标记

但现在我无法移除它

我已经尝试了Clear()方法和remove()方法

这是我的密码

getUserLocation();

Toast.makeText(this, "Latitude:" + lat + " Longitude:" + lang,
                Toast.LENGTH_LONG).show();

getAddress(lat, lang);

drawMarker(lat, lang);
这是我在oncreate()方法中编写的代码

下面是函数代码

private void getUserLocation() {

    location_manager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (location_manager != null) {
        provider = location_manager.getBestProvider(criteria, true);
        location = location_manager.getLastKnownLocation(provider);
        location_manager.requestLocationUpdates(provider,
                Map.MIN_TIME_BW_UPDATES,
                Map.MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
        if (location != null) {
            lat = location.getLatitude();
            lang = location.getLongitude();
        } else {

            Toast.makeText(Map.this, "Unable to idntify location",
                    Toast.LENGTH_LONG).show();

            lat = -22.00000;
            lang = -33.0000;

        }
    }
}

@Override
public void onLocationChanged(Location location) {

    mlocationMarker.remove();
    google_map.clear();
    drawMarker(lat, lang);

}

private void drawMarker(double lattitude, double longitude) {

    google_map.setMyLocationEnabled(true);

    LatLng latlng = new LatLng(lattitude, longitude);

    google_map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
    google_map.animateCamera(CameraUpdateFactory.zoomTo(15));

    LatLng currentPosition = new LatLng(lattitude, longitude);
    mlocation = new MarkerOptions();
    mlocation.position(currentPosition);
    mlocation.snippet("Address:" + addresses.get(0).getAddressLine(0)
            + "City:" + addresses.get(0).getAddressLine(1) + "Country:"
            + addresses.get(0).getAddressLine(2));
    mlocation.icon(BitmapDescriptorFactory
            .defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
    mlocation.title("ME");
    mlocationMarker = google_map.addMarker(mlocation);
}
在这里,我可以找到位置并放置标记,但在onlocationChanged()中,它不起作用,我不知道如何解决它

这是我的更新请求行

location_manager.requestLocationUpdates(provider,
                Map.MIN_TIME_BW_UPDATES,
                Map.MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

在我看来,你的代码应该可以工作。我会尝试添加一些日志来验证onLocationChanged是否确实被调用


在一个旁注中,你可以考虑只更新标记的位置,而不是删除它,并添加一个新的标记。有关详细信息,请参阅。

请检查带有已接受答案的同一问题的链接。。。[1]: