【发布时间】:2019-12-09 23:07:52
【问题描述】:
我正在开展一个项目,我需要在给定城市中显示自行车站,从 API 检索数据并使用 Google Maps API。我遵循了 Google 文档上的所有内容,但似乎无法显示标记。
我尝试直接在 google.maps.Marker 中传递自行车站位置参数,但它也不会显示。
JavaScript:
class GoogleMap {
constructor(latGmap, lngGmap, zoomGmap) {
this.latGmap = latGmap
this.lngGmap = lngGmap
this.zoomGmap = zoomGmap
this.map = new google.maps.Map(document.getElementById("map"), {
center: { lat: this.latGmap, lng: this.lngGmap },
zoom: this.zoomGmap
})
}
}
class Station {
constructor(stationName, stationStatus, stationAddress, stationAvailableBikes, stationAvailableBikeStands, stationLat, stationLng) {
this.stationName = stationName;
this.stationStatus = stationStatus;
this.stationAddress = stationAddress;
this.stationAvailableBikes = stationAvailableBikes;
this.stationAvailableBikeStands = stationAvailableBikeStands;
this.stationLat = stationLat;
this.stationLng = stationLng;
}
}
const fetchData = async function () {
let dataURL = "https://api.jcdecaux.com/vls/v1/stations?contract=amiens&apiKey=4cb8707fc70c97865d22d1324513f8e6464ed37b";
try {
let response = await fetch(dataURL)
if (response.ok) {
return response.json()
}
else {
console.error('Retour du serveur : ', response.status)
}
} catch (e) {
console.log(e);
}
}
let map;
function initMap() {
let googleMap = new GoogleMap(49.893034, 2.297347, 14);
fetchData().then(data => {
for (i = 0; i < data.length; i++) {
let station = data[i]
let stations = new Station(station.name, station.status, station.address, station.available_bikes, station.available_bike_stands, station.position.lat, station.position.lng)
let lat = stations.stationLat;
let lng = stations.stationLng;
let location = new google.maps.LatLng(lat, lng)
let labels = station.available_bikes.toString();
let marker = new google.maps.Marker({
position: location,
label: labels,
map: map
})
console.log(marker)
}
})
}
HTML:
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAeMga8sAPw4m43CMWgFu_SNunKJxjyoLg&callback=initMap" type="text/javascript"></script>
使用获取的数据,我应该会显示 26 个标记,但什么都没有出现。
【问题讨论】:
标签: javascript google-maps google-maps-markers