简体   繁体   中英

How can i get Marker id via javaScript in Gmap

When i create Marker in my bean i set each marker an ID.The reason is i want to access them in the client side(javaScript) by referring that ID. So far i couldn't do that because i cant get the id of the marker.How can i do this.This is my code.

//in my bean
LatLng coord = new LatLng(36.885233, 30.702323);
Marker m1 = new Marker(coord, "User A");
m1.setId("a");

getSimpleModel().addOverlay(m1);


//in my xhtml

var markers = map.getMap().markers;
for (var i = 0; i < markers.length; i++) {
                var marker = markers[i];
                 //i want to get the id of the marker here 

}

you can get the id of the elements with this javascript code:

map.getMap().markers[i].id

Where 'i' is the element position in markers array. However I think this object id is automatically generated by google library, in your case maybe is better set all the Object DATA in bean: markes, polygons, circles, ... has 'data' attribute, so you can get all the element attributes you want.

Bean :

Setting each marker:

    (...)
    if(obj != null ){
         marker.setData(obj);
   } 

Listener method:

 public void onMarkerSelect(OverlaySelectEvent event) {
      marker = (Marker) event.getOverlay();
  }

View:

Nest a listener in your gmap code:

 <p:ajax event="overlaySelect" listener="#{mapBean.onMarkerSelect}" />

So in view you can acess the selected element with:

#{mapBean.marker.data.id}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM