is there a way to set a longClickListsner
on a marker on google maps v2? I want to have the user long press on the marker and have a dialog show with options to delete or view information. Can this be done?
问题:
回答1:
I have another proposition.
First i make the marker draggable:
mapa.addMarker(new MarkerOptions() ...
.setDraggable(true);
After you can make a listener setOnMarkerDragListener like this:
mapa.setOnMarkerDragListener(new OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker marker) {
// TODO Auto-generated method stub
//Here your code
}
@Override
public void onMarkerDragEnd(Marker marker) {
// TODO Auto-generated method stub
}
@Override
public void onMarkerDrag(Marker marker) {
// TODO Auto-generated method stub
}
});
And then you can override that you want (normally onMarkerDragStart to simulate a long click)
Hope it helps
回答2:
Marker class doesn't have LongClickListener. Though this approach is far from perfect here is an idea about what you can do.
Set a long click listener for your GoogleMap object. On long click, check if the clicked position is close enough to any of your markers. To decide this closeness threshold, you may use map zoom level.
Here is the not so good sample code. I haven't tried it but it may suit your needs.
map.setOnMapLongClickListener(new OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng latLng) {
for(Marker marker : yourMarkerList) {
if(Math.abs(marker.getPosition().latitude - latLng.latitude) < 0.05 && Math.abs(marker.getPosition().longitude - latLng.longitude) < 0.05) {
Toast.makeText(MapActivity.this, "got clicked", Toast.LENGTH_SHORT).show(); //do some stuff
break;
}
}
}
});
回答3:
As ARP suggested, you can use the OnMarkerDragListener to simulate the long click. In my case, I am creating new markers when the onMapLongClick() occurred, so I wanted to do something similar (but not equal) that the
@Override
public void onMarkerDragStart(Marker marker) {
marker.remove();
MarkerOptions options = createBasedOnMarker(marker);
this.googleMap.addMarker(options);
}
this way, you will remove the marker that was previously within the map and create another in the same position, with the same content (you have to have a Map or Pair to keep the info from the markers you create).
Hope my workaround helps.
回答4:
ARPs answer is fine, but it suffers from a ugly effect: the marker shifts up when you start dragging and when you finish the marker is still in a higher position than you are pointing with your finger.
Have a look to http://www.youtube.com/watch?v=RxAHHJD4nU8
回答5:
akaya's answer isn't a bad approach, but if you have the com.google.maps.android:android-maps-utils:0.3+ library included with your project you can be exact about the distance:
double distance = SphericalUtil.computeDistanceBetween(
marker.getPosition(),
latLng
);
if( distance < 50 ) { // closer than 50 meters?
//..do stuff..
}
回答6:
Hey Take a look at this code I have just stopped the marker from getting dragged and used the onMarkerDragStart
's long click detection.
public abstract class LinkMarkerLongClickListener implements GoogleMap.OnMarkerDragListener {
private int previousIndex = -1;
private Marker cachedMarker = null;
private LatLng cachedDefaultPostion = null;
private List<Marker> markerList;
private List<LatLng> defaultPostions;
public LinkMarkerLongClickListener(List<Marker> markerList){
this.markerList = new ArrayList<>(markerList);
defaultPostions = new ArrayList<>(markerList.size());
for (Marker marker : markerList) {
defaultPostions.add(marker.getPosition());
marker.setDraggable(true);
}
}
public abstract void onLongClickListener(Marker marker);
@Override
public void onMarkerDragStart(Marker marker) {
onLongClickListener(marker);
setDefaultPostion(markerList.indexOf(marker));
}
@Override
public void onMarkerDrag(Marker marker) {
setDefaultPostion(markerList.indexOf(marker));
}
@Override
public void onMarkerDragEnd(Marker marker) {
setDefaultPostion(markerList.indexOf(marker));
}
private void setDefaultPostion(int markerIndex) {
if(previousIndex == -1 || previousIndex != markerIndex){
cachedMarker = markerList.get(markerIndex);
cachedDefaultPostion = defaultPostions.get(markerIndex);
previousIndex = markerIndex;
}
cachedMarker.setPosition(cachedDefaultPostion);
}
}
Your can call the listener like this !
map.setOnMarkerDragListener(new LinkMarkerLongClickListener(markers) {
@Override
public void onLongClickListener(Marker marker) {
ToastUtils.showToast(Mission1Activity.this, marker.getTitle());
}
});
回答7:
Just in case someone may need it, I have adjusted a logarithmic function to calculate the closeness threshold @akaya mentioned, which works fine for zooms between 5.0 and 18. Here is the function:
float zoom = mMap.getCameraPosition().zoom;
double maxDistance = 2000000 * Math.exp(-0.644*zoom);