android - 其他一些函数中的 mMap.clear 正在从公共 void onMapLongClick(LatLng latLng) 中删除标记?
问题描述
所以我有一个功能
public void centerOnMapLocation (Location location, String title) {
if (location != null) {
LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.clear();
mMap.addMarker(new MarkerOptions().position(userLocation).title(title));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 13));
}
}
在 android 地图活动中,我实现了 onMapLongClick(LatLng latLng)
@Override
public void onMapLongClick(LatLng latLng) {
String addressTitle = "";
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
ArrayList<Address> addressArrayList = (ArrayList<Address>) geocoder.getFromLocation(latLng.latitude, latLng.latitude, 1);
if (addressArrayList != null & addressArrayList.size() > 0) {
if (addressArrayList.get(0).getAddressLine(0) != null) {
addressTitle += addressArrayList.get(0).getAddressLine(0);
}
}
} catch (IOException e) {
e.printStackTrace();
}
mMap.addMarker(new MarkerOptions().position(latLng).title(addressTitle));
}
在地图活动和函数内部,我添加了一个标记。但是在长按时,标记消失了,当我
从前一个函数“centerOnMapLocation”中删除“mMap.clear()”时,我可以重新添加标记并保留在那里。
我的问题是我什至没有在 onMapLongClick 中调用该函数,但是当我删除 mMap.clear(); 从其他功能。我可以在里面添加标记??我做错了什么