我正在使用 JSON(本地文件)加载一些标记信息,但由于某种原因,只有循环中的最后一个标记显示在地图上。
我可以看到在控制台中正确打印了所有坐标,所以我很确定循环很好,而且大部分代码都很好。
<script>
function myMap() {
var mapCanvas = document.getElementById("content_map" );
var mapOptions = {
center: new google.maps.LatLng(55.812757, -4.195363),
zoom: 5,
disableDefaultUI: true,
styles: [
{
"elementType": "geometry",
"stylers": [
{
"color": "#f5f5f5"
}
]
},
{
"elementType": "labels.icon",
"stylers": [
{
"visibility": "off"
}
]
},
{
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#616161"
}
]
},
{
"elementType": "labels.text.stroke",
"stylers": [
{
"color": "#f5f5f5"
}
]
},
{
"featureType": "administrative.land_parcel",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "administrative.land_parcel",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#bdbdbd"
}
]
},
{
"featureType": "poi",
"elementType": "geometry",
"stylers": [
{
"color": "#eeeeee"
}
]
},
{
"featureType": "poi",
"elementType": "labels.text",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "poi",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#757575"
}
]
},
{
"featureType": "poi.business",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "poi.park",
"elementType": "geometry",
"stylers": [
{
"color": "#e5e5e5"
}
]
},
{
"featureType": "poi.park",
"elementType": "labels.text",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "poi.park",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#0d99ba"
}
]
},
{
"featureType": "road",
"elementType": "geometry",
"stylers": [
{
"color": "#ffffff"
}
]
},
{
"featureType": "road.arterial",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "road.arterial",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#757575"
}
]
},
{
"featureType": "road.highway",
"elementType": "geometry",
"stylers": [
{
"color": "#9e9e9e"
}
]
},
{
"featureType": "road.highway",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "road.highway",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#616161"
}
]
},
{
"featureType": "road.local",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "road.local",
"elementType": "labels",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "road.local",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#9e9e9e"
}
]
},
{
"featureType": "transit.line",
"elementType": "geometry",
"stylers": [
{
"color": "#e5e5e5"
}
]
},
{
"featureType": "transit.station",
"elementType": "geometry",
"stylers": [
{
"color": "#eeeeee"
}
]
},
{
"featureType": "water",
"elementType": "geometry",
"stylers": [
{
"color": "#0d99ba"
}
]
},
{
"featureType": "water",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#9e9e9e"
}
]
}
]
};
var contentString = '<div id="content">'+
'<img src="./img/logo_img_map.png" alt="">'+
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
$.getJSON('data.json', function(json) {
for (i = 0; i < json.ChargeDevice.length; i++) {
addMarker(json.ChargeDevice[i].ChargeDeviceLocation.Latitude,json.ChargeDevice[i].ChargeDeviceLocation.Longitude);
console.log(json.ChargeDevice[i].ChargeDeviceLocation.Latitude, json.ChargeDevice[i].ChargeDeviceLocation.Longitude);
};
function addMarker(lat,lng) {
var image = './img/logo_img_map.png';
var map = new google.maps.Map(mapCanvas, mapOptions);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: map,
icon: image
});
};
});
};
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=MYKEY"></script>
我现在已经查看了大量线程,但我无法弄清楚为什么它在这种情况下不起作用。我正在运行我的函数,因为它发生在循环中......所以标记真的不应该保留吗?
注意:我的 API 密钥已被删除,因此它不会在线显示,但它可以在我的网络服务器上运行并且地图加载正常。实际上只有一个标记显示,而我的 JSON 文件中有 2 个数据集。
我还没有完成点击环境,但我认为现在这不重要,是吗?
任何帮助,将不胜感激!
我认为问题可能在于您为每个标记声明了一张新地图。我会声明一次地图,并为每个标记引用它。
请尝试以下操作:
<script>
function myMap() {
var mapCanvas = document.getElementById("content_map" );
var mapOptions = {
center: new google.maps.LatLng(55.812757, -4.195363),
zoom: 5,
disableDefaultUI: true,
styles: [
{
"elementType": "geometry",
"stylers": [
{
"color": "#f5f5f5"
}
]
},
{
"elementType": "labels.icon",
"stylers": [
{
"visibility": "off"
}
]
},
{
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#616161"
}
]
},
{
"elementType": "labels.text.stroke",
"stylers": [
{
"color": "#f5f5f5"
}
]
},
{
"featureType": "administrative.land_parcel",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "administrative.land_parcel",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#bdbdbd"
}
]
},
{
"featureType": "poi",
"elementType": "geometry",
"stylers": [
{
"color": "#eeeeee"
}
]
},
{
"featureType": "poi",
"elementType": "labels.text",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "poi",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#757575"
}
]
},
{
"featureType": "poi.business",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "poi.park",
"elementType": "geometry",
"stylers": [
{
"color": "#e5e5e5"
}
]
},
{
"featureType": "poi.park",
"elementType": "labels.text",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "poi.park",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#0d99ba"
}
]
},
{
"featureType": "road",
"elementType": "geometry",
"stylers": [
{
"color": "#ffffff"
}
]
},
{
"featureType": "road.arterial",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "road.arterial",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#757575"
}
]
},
{
"featureType": "road.highway",
"elementType": "geometry",
"stylers": [
{
"color": "#9e9e9e"
}
]
},
{
"featureType": "road.highway",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "road.highway",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#616161"
}
]
},
{
"featureType": "road.local",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "road.local",
"elementType": "labels",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "road.local",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#9e9e9e"
}
]
},
{
"featureType": "transit.line",
"elementType": "geometry",
"stylers": [
{
"color": "#e5e5e5"
}
]
},
{
"featureType": "transit.station",
"elementType": "geometry",
"stylers": [
{
"color": "#eeeeee"
}
]
},
{
"featureType": "water",
"elementType": "geometry",
"stylers": [
{
"color": "#0d99ba"
}
]
},
{
"featureType": "water",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#9e9e9e"
}
]
}
]
};
var map = new google.maps.Map(mapCanvas, mapOptions);
var contentString = '<div id="content">'+
'<img src="./img/logo_img_map.png" alt="">'+
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
$.getJSON('data.json', function(json) {
for (var i = 0; i < json.ChargeDevice.length; i++) {
addMarker(json.ChargeDevice[i].ChargeDeviceLocation.Latitude,json.ChargeDevice[i].ChargeDeviceLocation.Longitude);
console.log(json.ChargeDevice[i].ChargeDeviceLocation.Latitude, json.ChargeDevice[i].ChargeDeviceLocation.Longitude);
};
function addMarker(lat,lng) {
var image = './img/logo_img_map.png';
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: map,
icon: image
});
};
});
};
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=MYKEY"></script>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句