[英]Google maps api and geocoding api - issues adding markers
I will start by saying that I am relatively new to JS, so please forgive my ignorance if this is obvious.我首先要说我对 JS 比较陌生,所以如果这很明显,请原谅我的无知。
I am trying to add markers to a google map.我正在尝试向谷歌地图添加标记。 I have created an array coordList, then used the geocoding api to get the lag and long from the addresses and pushed them into coordList.我创建了一个数组 coordList,然后使用地理编码 api 从地址中获取滞后和长,并将它们推送到 coordList。
I am now trying to use the coordList array to plot markers on the map, however I cannot seem to get the values from the coordList array.我现在正在尝试使用 coordList 数组在地图上绘制标记,但是我似乎无法从 coordList 数组中获取值。 When I run console.log(typeof coordList) - it tells me it's an object, but when i look at the array with console.log(coordList) it just looks like a normal array?当我运行 console.log(typeof coordList) - 它告诉我它是一个对象,但是当我用 console.log(coordList) 查看数组时,它看起来像一个普通数组?
var coordList = [];
var address = [];
address.push('52+Kalynda+pde,+bohle+plains,+QLD')
address.push('51+Frank+St,+Kirwan+QLD+4817');
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: new google.maps.LatLng(-19.259854,146.8001348),
mapTypeId: 'roadmap'
});
}
function getLatLong(address){
var index;
for (index = 0; index < address.length; ++index) {
var request = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + address[index] + '&key=[MY_key]';
$.getJSON( request, function( data ) {
var lat = data.results[0].geometry.location.lat;
var lng = data.results[0].geometry.location.lng;
var coords = [];
coords.push(lat);
coords.push(lng);
//push coords into coordList
coordList.push(coords);
});
}
}
// Loop through the results array and place a marker for each
// set of coordinates.
function addMarkers(coordList) {
for (var i = 0; i < coordList.length; i++) {
var coords = coordList[i];
var latLng = new google.maps.LatLng(coords[0],coords[1]);
var marker = new google.maps.Marker({
position: latLng,
map: map
});
}
}
getLatLong(address);
addMarkers(coordList);
Your problem is that $.getJSON() is an asynchronous request and your code executes addMarkers() before than $.getJSON() finishes, so coordList is empty.你的问题是 $.getJSON() 是一个异步请求,你的代码在 $.getJSON() 完成之前执行 addMarkers() ,所以 coordList 是空的。
You can add the markers inside $.getJSON() callback.您可以在 $.getJSON() 回调中添加标记。 For example:例如:
var address = [];
address.push('52+Kalynda+pde,+bohle+plains,+QLD')
address.push('51+Frank+St,+Kirwan+QLD+4817');
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: new google.maps.LatLng(-19.259854,146.8001348),
mapTypeId: 'roadmap'
});
}
function getLatLongAndAddMarkers(address){
var index;
for (index = 0; index < address.length; ++index) {
var request = 'https://maps.googleapis.com/maps/api/geocode/json?dress=' + address[index] + '&key=[MY_key]';
$.getJSON( request, function( data ) {
var latLong = new google.maps.LatLng(data.results[0].geometry.location);
//add markers here
var marker = new google.maps.Marker({
position: latLong,
map: map
});
});
}
}
getLatLongAndAddMarkers(address);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.