简体   繁体   English

如何在openlayer中获取标记位置

[英]How to get the marker position in openlayer

I have implemented a openlayer map with several markers with drupal. 我已经实现了一个带有drupal的几个标记的openlayer地图。 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");
    });

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)

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/ 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. 这种态度的问题在于,通过许多标记,您将获得大量注册事件和可能的性能问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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