简体   繁体   English

谷歌地图 JS API V3,使用远程 json 源的信息窗口将多个标记添加到 map

[英]Google maps JS API V3,add multiple markers to map with infowindows from remote json source

I want to place multiple markers on a google map, each with its own infowindow.我想在谷歌 map 上放置多个标记,每个标记都有自己的信息窗口。 The data that i need to place these markers is dynamic and is obtained from a remote api that provides a json output.我需要放置这些标记的数据是动态的,并且是从提供 json output 的远程 api 获得的。 The json is of the format, json 的格式为,

{0 : {lat:"", lng:"", description:"", .....}, 1 : {lat: "", .....}}

and so on.等等。 It can contain any number of value sets.它可以包含任意数量的值集。 The format of each set however is defined.然而,每组的格式是定义的。 Also, the keys are such, that they can be iterated over easily.此外,键是这样的,它们可以很容易地被迭代。 The code i have written is as follows:我写的代码如下:

var map;
var initPos;
var marker;
var pins;
var infowindow = null;

function count(obj) {
  var i = 0;
  for (var x in obj)
    if (obj.hasOwnProperty(x))
      i++;
  return i;
};

function initialize() {
    var myOptions = {
    zoom: 4,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    infowindow = new google.maps.InfoWindow();

    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
        initPos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        map.setCenter(initPos);
        }, function() {
            initPos = new google.maps.LatLng(19.074448,72.872314);
            map.setCenter(initPos);
        });
    }
    else {
        initPos = new google.maps.LatLng(19.074448,72.872314);
        map.setCenter(initPos);
    }
    var i, pins, html, pinLatLng, pinMarkers, contentString;
    $.getJSON('/user/map/', function (data) {
        for (i = 0; i < count(data); i++) {
            pins = data[i];
            html = '<h4>'+pins['title']+'</h4>'+'<p>'+pins['description']+'</p>'+'<p><a href="'+pins['url']+'">Read More</a>'
            pinLatLng = new google.maps.LatLng(parseFloat(pins['lat']), parseFloat(pins['lng']));
            pinMarkers = new google.maps.Marker({position: pinLatLng, map: map, animation: google.maps.Animation.Drop});
            contentString = "Some content";
            google.maps.event.addListener(pinMarkers, "click", (function(pinMarkers, i) {
                return function() {
                    infowindow.setContent(html);
                    infowindow.open(map, pinMarkers);
                }
            })(pinMarkers, i));
        }
    });
};

Don't pay attention to the navigator.geolocation part, but look at the other things.不要关注 navigator.geolocation 部分,而是看其他的东西。 Now what this script does is, it only marks the last marker in the json on the map and the rest are not shown.现在这个脚本的作用是,它只标记 map 上 json 中的最后一个标记,并且未显示 rest。 What am i doing wrong?我究竟做错了什么? EDIT: So i was making a mistake in the json format, which i've fixed now.编辑:所以我在 json 格式中犯了一个错误,我现在已经修复了。 Now the problem is that all the infowindows display the same data rather than different ones for each.现在的问题是所有信息窗口都显示相同的数据,而不是每个不同的数据。 It does show different markers now though.它现在确实显示了不同的标记。 EDIT2 : Its working now. EDIT2 :它现在工作。 Followed the steps given here http://you.arenot.me/2010/06/29/google-maps-api-v3-0-multiple-markers-multiple-infowindows/按照此处给出的步骤http://you.arenot.me/2010/06/29/google-maps-api-v3-0-multiple-markers-multiple-infowindows/

Have you checked that jQuery is getting the JSON data in the correct format?您是否检查过 jQuery 是否以正确的格式获取 JSON 数据? jQuery can be very stringent with its JS checking, and in this case I'd recommend the format: jQuery 的 JS 检查可能非常严格,在这种情况下,我建议使用以下格式:

[{object},{object},{object}]

rather than:而不是:

{0:{object},1:{object},2:{object}}

which might also need you to quote the identifiers:这可能还需要您引用标识符:

{"0":{object},"1":{object},"2":{object}}

So, before the first line of your marker function add:因此,在标记 function 的第一行之前添加:

console.log(data);

and see what it comes out with.看看结果如何。 Once you've confirmed the data is correct I'd also recommend looping through with jQuery's $.each operator:一旦您确认数据是正确的,我还建议您使用 jQuery 的 $.each 运算符进行循环:

$.each(data,function(key,val){
 //stuff
});

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

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