【发布时间】:2018-04-23 10:24:03
【问题描述】:
在过去的几天里,我一直在尝试使用 Json 数据集在谷歌地图上放置标记。我在使用另一个数据集之前已经这样做了,我似乎无法找出我做错了什么。 这是我正在使用的代码(也带有指向数据集的链接) 我会喜欢一些输入,所以我可以找出问题所在。
function initMap() {
var xhr = new XMLHttpRequest();
var url = 'https://api.openchargemap.io/v2/poi/?output=json&countrycode=NO&maxresults=*';
xhr.open('GET', url, true);
xhr.onload = function () {
xhr.Data = JSON.parse(this.response);
if (this.status == 200) {
xhr.Data.forEach(poi => {
var map;
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 60.391011, lng: 5.325950},
zoom: 4
});
for (var i = 0; i < poi.AddressInfo; i++) {
var data = xhr.Data.poi[i],
latLng = new google.maps.LatLng(data.AddressInfo.latitude, data.AddressInfo.longitude);
//Creating a marker and putting it on the map
var marker = new google.maps.Marker({
position: latLng,
map: map,
});
}
})
}
}
xhr.send();
}
【问题讨论】:
-
for (var i = 0; i < poi.AddressInfo; i++) {你希望在这里发生什么?poi.AddressInfo是一个对象。 -
对...摆脱
for()循环 -
for 循环的想法是能够遍历 API 的所有 POI 点以获取所有点,如果没有该循环,我将如何做到这一点?
-
我为您解决了一些问题。仔细查看
map变量的定义位置以及我如何访问纬度和经度(javascript 区分大小写)。 jsfiddle.net/x4gM4/674 -
“如果没有那个循环,我该怎么做” - 你已经在
forEach循环中:xhr.Data.forEach(poi => { ... }
标签: javascript html json api google-maps