gpt4 book ai didi

javascript - jQuery Google map 通过 ID 获取标记

转载 作者:行者123 更新时间:2023-12-01 02:30:09 25 4
gpt4 key购买 nike

我目前有以下代码:

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);
});

});

问题区域是:

jQuery('.get-direction').on('click', function(){
var markerID = this.id.replace( /[^\d.]/g,'');
console.log(marker);
});

基本上,当我点击带有 get-direction 类的链接时,我会抓取它的 id 并删除除数字之外的所有内容,这样我就得到了标记 id。但是如何通过 id 获取标记呢?实际上我只需要标记的纬度和经度。

非常感谢任何帮助。

最佳答案

您可以创建一个全局关联数组并将所有标记存储在其中。例如。像这样

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]);
});
}

关于javascript - jQuery Google map 通过 ID 获取标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20052037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com