存储Google Map标记并从中选择特定标记 [英] Storing Google Map Markers and Selecting a specific marker from it
问题描述
我有一个网页,该网页使用$.getJSON
接收值为listing_id, lat, lng
的AJAX数据.回调函数采用这些值系列,并为每组listing_id, lat, lng
创建一个Google地图标记.然后将标记放入数组markers
中,并将其listing_id
放入数组markersListingId
中.
I have a webpage that receives AJAX data with values listing_id, lat, lng
using $.getJSON
. The callback function takes these series of values and creates a Google map marker for each set of listing_id, lat, lng
. The marker is then pushed into an array markers
and its listing_id
into an array markersListingId
.
var latLng = new google.maps.LatLng(json[i].lat, json[i].lng);
var marker = new google.maps.Marker({
position: latLng,
icon: base_url + 'images/template/markers/listing.png',
});
markers.push(marker);
markersListingId.push(json[i].listing_id);
问题:当我要选择与特定listing_id
相对应的特定标记时,尤其是在删除标记并将其添加到markers
数组的更为复杂的情况下,就会出现问题.
Problem: The problem arises when I want to select a specific Marker that corresponds to a particular listing_id
, especially in more complicated situations with markers being deleted and added to the markers
array.
我可以让for循环遍历markersListingId
数组中所有标记的ID,然后使用循环的索引i
来获取markers
数组中的标记,但这会使跟踪变得非常棘手.
I can have for loops looping through all the marker's id in markersListingId
array then using the loop's index i
to get the marker in markers
array, but this will make tracking very tricky.
var id_to_select = 1234;
for(var i = 0; i < markersListingId; i++) {
if(markersListingId[i] == id_to_select) {
markers[i].setMap(null);
}
}
问题:如何存储标记,以便可以使用其listing_id
轻松选择特定标记?
Question: How can I store the markers so that I can easily select a specific marker by using its listing_id
?
推荐答案
创建标记时,可以使用哈希表来代替使用数组吗?
When you create the markers, instead of using an array, can you use a hash table?
var markers = {};
//id goes in place of 1234
n = 1234;
markers[n] = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(0,0)
});
s = '2234'
markers[s] = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(20,0)
});
google.maps.event.addListener(map, 'click', function(event) {
k = 1234;
markers[k].setMap(null);
markers['2234'].setMap(null);
});
}
这篇关于存储Google Map标记并从中选择特定标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!