Reputation: 3829
I have implemented a openlayer map with several markers with drupal. I want to get the longitude and latitude of the marker when I click on the marker. I have just write a code which shows the longitude and latitude when I click on the map. Instead I want to alert the position only when I click on the marker that I have plotted on the map. How to get the longitude and latitude of the marker?
jQuery(function ($) {
var ol_data = $('.openlayers-map').data('openlayers');
var map = ol_data.openlayers;
map.events.register("click", map, function (e) {
var lonlat = map.getLonLatFromPixel(e.xy);
alert("You clicked near " + lonlat.lat + " N, " + +lonlat.lon + " E");
});
Upvotes: 3
Views: 4839
Reputation: 5194
This may help you
var markerslayer = map.getLayer('Markers');//get marker layer
var marker = markerslayer.markers[0];//get marker
var lonlat = marker.lonlat;//get marker latlon(position)
Upvotes: 1
Reputation: 595
I have not found a way to do this in layer scope, but you can do this for every marker in the layer:
marker.events.register('click', marker, function (evt) {
alert("lon: "+evt.object.lonlat.lon+" , lat: "+evt.object.lonlat.lon);
});
jsFiddle: http://jsfiddle.net/Kenny806/SmMxW/1/
The problem with this attitude is, that with many markers you will get a lot of registered events and possibly performance issues.
Upvotes: 0