我已经使用数据库动态创建了具有多个标记的Google Map,现在有人可以建议我如何分别创建每个标记的点击事件吗?
You can do like this
private Marker marker1;
marker1 = mMap.addMarker(new MarkerOptions()
.position(LatLng(-31.952854, 115.857342))
.title("Title");
marker1.setTag(0);
mMap.setOnMarkerClickListener(this);
and in implementation of listener
@Override
public boolean onMarkerClick(final Marker marker) {
//marker.getTag()
}
Use this for Multiple markers:
First make your app to implement GoogleMap.OnMarkerClickListener Then create a Marker array :
Marker[] marker = new Marker[20]; //change length of array according to you
then inside
onMapReady(){
mMap.setOnMarkerClickListener(this);
for(int i = 0 ; i < yourMarkerListsize ; i++ ) {
marker[i] = mMap.addMarker(new MarkerOptions()
.position(new LatLng(list(i).getLat(), list(i).getLon())
.anchor(0.5f, 0.5f)
.title(title)
.snippet(snippet)
.icon(yourIcon));
}
then finally
@Override
public boolean onMarkerClick(Marker marker) {
//you can get assests of the clicked marker
return false;
}
Hope it helps!!!
If you are looking for Xamarin code for the this, following is how you do it
GoogleMaps Gmap;
GMap.MarkerClick += GMap_MarkerClick;
GMap.InfoWindowClick += GMap_InfoWindowClick;
private void GMap_MarkerClick(object sender, GoogleMap.MarkerClickEventArgs e)
{
Marker marker = e.Marker;
marker.ShowInfoWindow();
}
private void GMap_InfoWindowClick(object sender, GoogleMap.InfoWindowClickEventArgs e)
{
}
this is how you handle both marker and marker info window click events in xamarin Android Hope this helps you.
I think the below approach may help you:
As others have said, you can set an OnMarkerClickListener
to the marker, to perform an event on Marker click and add launch activity using an URL, refer here . So you can store the activity url also in DB. When dynamically creating the marker, maintain a Map whose key will be object of Marker
class and Value will be String
the URL.
so, inside onMarkerClick()
method:
@Override
public boolean onMarkerClick(Marker marker) {
String activityUrl = markerURLMap.get(marker);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(activityUrl));
startActivity(intent);
}
In order to attach unique URLs per marker click, you need to attach an object that you tag within Marker.Tag as follows:
UrlObject url = new UrlObject()
{
Url = "enteryoururlhere"
};
marker.Tag = UrlObject;
Then, you would get your URL that is unique to each marker by calling the following:
public boolean onMarkerClick(Marker marker) {
UrlObject obj = marker.Tag;
String activityUrl = obj.Url;
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(activityUrl));
startActivity(intent);
}
Your UrlObject class would be easy to setup:
class UrlObject : Java.Lang.Object
{
public string Url { get; set; }
}
Edit: You would make a tag for every marker in your loop. That way, when each marker is created, it is tagged properly with each url.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.