javascript jQuery Google Maps 按 ID 获取标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20052037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Google Maps Get Marker By Id
提问by Dawid van der Hoven
I currently have the following code:
我目前有以下代码:
jQuery.each(json,function(index,value){
var id =json[index]["id"];
var location =json[index]["location"];
var tel =json[index]["tel"];
var lat=json[index]["lat"];
var lng=json[index]["lng"];
marker = new google.maps.Marker({
id:id,
position: new google.maps.LatLng (lat, lng),
icon: {
path: google.maps.SymbolPath.CIRCLE,
fillOpacity: 1,
fillColor: '#002664',
strokeWeight: 1,
strokeColor: '#FFFFFF',
scale: 5 //pixels
},
map: map
});
google.maps.event.addListener(marker, 'click', function() {
jQuery('.office-information').slideUp(300);
if(!jQuery('#office-info-'+id).is(':visible')){
jQuery('#office-info-'+id).slideDown(300);
}
});
google.maps.event.addListener(marker, 'click', function() {
});
jQuery('.get-direction').on('click', function(){
var markerID = this.id.replace( /[^\d.]/g,'');
console.log(marker);
});
});
The problem area is:
问题区域是:
jQuery('.get-direction').on('click', function(){
var markerID = this.id.replace( /[^\d.]/g,'');
console.log(marker);
});
basically, when I click on a link with a class of get-direction
i grab its id and strip everything but the numbers from it, that leaves me with the markers id. But How do I get a marker by id? I actually just need the latitude and longitude from the marker.
基本上,当我点击一个类的链接时,get-direction
我会抓取它的 id 并从中删除除数字之外的所有内容,这给我留下了标记 id。但是如何通过 id 获取标记?我实际上只需要标记的纬度和经度。
Any Help Greatly Appreciated.
非常感谢任何帮助。
回答by paulitto
You can create a global associative array and store all your markers in there. E.g. like this
您可以创建一个全局关联数组并将所有标记存储在其中。比如像这样
var arrMarkers = {};
jQuery.each(json,function(index,value){
...
marker = new google.maps.Marker({
...
});
arrMarkers[id] = marker;
...
jQuery('.get-direction').on('click', function(){
var markerID = this.id.replace( /[^\d.]/g,'');
console.log(arrMarkers[markerID]);
});
}