我想在任何地方长按地图后删除当前标记,然后在该点重新创建一个新标记。我已经在地图上长按谷歌地图并创建了新标记,但也显示了前一个标记。
我的代码是:
public class EditLocation extends Fragment {
View v;
Context c;
GoogleMap MAP;
Button back;
int loc;
String lat;
boolean isTapped = true;
public EditLocation(Context c, int location, String latitude) {
// TODO Auto-generated constructor stub
this.c = c;
this.loc = location;
this.lat = latitude;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.map, container, false);
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(c);
if (status != ConnectionResult.SUCCESS) {
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status,
(Activity) c, requestCode);
dialog.show();
} else {
FragmentManager myFM = ((FragmentActivity) c)
.getSupportFragmentManager();
final SupportMapFragment myMAPF = (SupportMapFragment) myFM
.findFragmentById(R.id.fragmentmap);
MAP = myMAPF.getMap();
MAP.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) c
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
final Location location = locationManager
.getLastKnownLocation(provider);
final LatLng currentPosition = new LatLng(location.getLatitude(),
location.getLongitude());
MAP.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
MAP.addMarker(new MarkerOptions()
.position(currentPosition)
.snippet(
"Lat:" + location.getLatitude() + "Lng:"
+ location.getLongitude())
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title("ME"));
Log.e("lat", "" + point);
}
});
MAP.setOnMapLongClickListener(new OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng point) {
// TODO Auto-generated method stub
// isTapped = false;
MAP.clear();
MAP.addMarker(new MarkerOptions().position(point)
.title(point.toString()));
}
});
}
return v;
}
答案 0 :(得分:35)
只需创建一个新的标记对象,然后在添加新标记之前删除上一个标记
Marker marker;
MAP.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng arg0) {
if (marker != null) {
marker.remove();
}
marker = MAP.addMarker(new MarkerOptions()
.position(
new LatLng(arg0.latitude,
arg0.longitude))
.draggable(true).visible(true));
}
});
修改强> 的
对OnMapClick
MAP.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
if (marker != null) {
marker.remove();
}
marker = MAP.addMarker(new MarkerOptions()
.position(currentPosition)
.snippet(
"Lat:" + location.getLatitude() + "Lng:"
+ location.getLongitude())
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title("ME"));
Log.e("lat", "" + point);
}
});
答案 1 :(得分:4)
在添加标记之前,请先清除谷歌地图。像这样:
@Override
public void onMapLongClick(LatLng latLng) {
googleMap.clear();
googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title(latLng.toString())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
答案 2 :(得分:2)
这是一种简单的方法您只需要更改标记的position
即可。创建全局对象为Marker marker;
之后添加标记到地图,如
marker = map.addMarker(markerOptions).position(new Latlng(31.647316, 74.763791));
在使用marker.setPosition(newlaLng);
后,您需要添加标记。
答案 3 :(得分:2)
首先清除Google地图,然后添加一个新标记。 检查下面的代码
mMap.clear();
//google map marker adding code here
答案 4 :(得分:0)
请尝试打击代码: -
// Global Variable...
private Marker mPreviousMarker ;
@Override
public boolean onMarkerClick(Marker marker) {
if (mPreviousMarker != null) {
mPreviousMarker.remove();
}
mPreviousMarker = googleMap.addMarker(new MarkerOptions().position(latLng).icon(bitmapDescriptor));
}
LatLng: - 您想要添加的latlong和bitmapDescroptor是图标。 {只是为了理解}
答案 5 :(得分:0)
对于在此问题中尝试过第一个解决方案的人,您会得到标记尚未初始化的错误。>
因为,我们正在比较 标记和空。当我们甚至还没有初始化标记时。
解决方案:
Int mMarkerCount = 0; //Global Variable
Marker mMarker; //Global Variable
if(mMarkerCount > 0){
if(mMarker != null){
mMarker.remove();
}
}
mMarker = mMap.addMarker(new MarkerOptions().position(latLng));
mMarkerCount++;
问题的解决方案:
Int mMarkerCount = 0; //Global Variable
Marker mMarker;
mMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng latlng) {
if(mMarkerCount > 0){
if(mMarker != null){
mMarker.remove();
}
}
mMarker = mMap.addMarker(new MarkerOptions().position(latLng));
mMarkerCount++;
}
});
一开始,计数将为零。因此,标记删除方法不会被调用。标记一次被初始化,计数被增加。我们可以借助remove方法删除先前的标记,并创建新标记,如代码所示。
答案 6 :(得分:0)
@Override
public void onMapReady(GoogleMap googleMap) {
MarkerOptions markerOptions = new MarkerOptions();
LatLng latLng = new LatLng(address.getLat(), address.getLng());
markerOptions.position(latLng);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 5));
googleMap.addMarker(markerOptions);
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
if (touchMarker != null) {
touchMarker.remove();
}
googleMap.clear();
MarkerOptions markerOptions = new MarkerOptions().position(latLng);
touchMarker = googleMap.addMarker(markerOptions);
address.setLat(touchMarker.getPosition().latitude);
address.setLng(touchMarker.getPosition().longitude);
}
});
}