Google 地图 JS API V3,添加多个标记以从远程 json 源使用 infowindows 进行映射
我想在谷歌地图上放置多个标记,每个标记都有自己的信息窗口。我需要放置这些标记的数据是动态的,并且是从提供 json 输出的远程 api 获取的。 json的格式
{0 : {lat:"", lng:"", description:"", .....}, 1 : {lat: "", .....}}
等等。它可以包含任意数量的值集。然而,每组的格式是被定义的。而且,这些键可以很容易地进行迭代。我写的代码如下:
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));
}
});
};
不要关注navigator.geolocation部分,而是看看其他的东西。现在这个脚本的作用是,它只在地图上标记 json 中的最后一个标记,其余部分不会显示。我做错了什么? 编辑:所以我在 json 格式中犯了一个错误,我现在已经修复了。现在的问题是所有信息窗口显示相同的数据,而不是每个信息窗口显示不同的数据。但现在它确实显示了不同的标记。 EDIT2:现在可以使用了。按照此处给出的步骤进行操作 http://you.arenot.me/2010/06/29/google-maps-api-v3-0-multiple-markers-multiple-infowindows/
I want to place multiple markers on a google map, each with its own infowindow. The data that i need to place these markers is dynamic and is obtained from a remote api that provides a json output. The json is of the format,
{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. Now what this script does is, it only marks the last marker in the json on the map and the rest are not shown. What am i doing wrong?
EDIT : So i was making a mistake in the json format, which i've fixed now. 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. Followed the steps given here http://you.arenot.me/2010/06/29/google-maps-api-v3-0-multiple-markers-multiple-infowindows/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否检查过 jQuery 是否以正确的格式获取 JSON 数据? jQuery 的 JS 检查可能非常严格,在这种情况下,我建议使用以下格式:
而不是:,
这可能还需要您引用标识符:
因此,在标记函数的第一行之前添加:
并查看它是什么出来了。一旦您确认数据正确,我还建议使用 jQuery 的 $.each 运算符进行循环:
Have you checked that jQuery is getting the JSON data in the correct format? jQuery can be very stringent with its JS checking, and in this case I'd recommend the format:
rather than:
which might also need you to quote the identifiers:
So, before the first line of your marker function add:
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: