我试图在我的测试页面上显示多个标记。我有一个JSON:
var myJson =
{
"hotels": [
{
"hotelCode" : "1",
"isAvailable" : "true",
"latitude" : "53.427901",
"longitude" : "-6.233612",
"price" : "99"
},
etc.
这是我的JS代码:
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(53.345897,-6.269204);
var myLatlng2 = new google.maps.LatLng(myJson.hotels[0].latitude, myJson.hotels[0].longitude);
console.log(myJson.hotels[0].latitude, myJson.hotels[0].longitude);
var mapOptions = {
zoom: 12,
center: myLatlng
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Hello World!'
});
var marker2 = new google.maps.Marker({
position: myLatlng2,
map: map,
title: 'Hello World!'
});
console.log(marker);
var key = 0;
for(key in myJson.hotels)
{
var newLatlng = new google.maps.LatLng(myJson.hotels[key].latitude + "," + myJson.hotels[key].longitude);
var i = new google.maps.Marker(
{
position: newLatlng,
map: map,
title: 'Hello World!'
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
我的问题是: - 带硬编码的标记(myLatlng) - 显示正常 - 没有显示markey2 - 来自'的标记。循环不显示。
console.log(myJson.hotels[0].latitude, myJson.hotels[0].longitude);
给了我:
53.427901 -6.233612