存储Google Map标记并从中选择特定标记 [英] Storing Google Map Markers and Selecting a specific marker from it

查看:123
本文介绍了存储Google Map标记并从中选择特定标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,该网页使用$.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?

http://jsfiddle.net/j9J8x/

    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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆