【发布时间】:2014-08-26 07:05:05
【问题描述】:
我创建了一个不错的 Google 地图表单,它从数据库中获取客户数据(使用 jQuery post 调用 php)并将其加载到 clients_details。如果数据库中提供了 Latlng 的 clients_details[]['location'] 一切正常,并且标记按预期显示。问题是,当未提供 clients_details[]['location'] 时,我使用来自 clients_details[]['address'] 的地址并尝试使用 geocoder.geocode 获取标记的位置。然而令人惊讶的是,每次代码到达地理编码器时,它都会从地理编码器中跳转并在初始化地图后返回到它!所以标记不会被添加到地图中!
我假设它与 JavaScript 函数优先级有关,但不确定如何
<script>
var clients_details // getting this from database;
var infowindow =[];
var geocoder;
var map;
function showMarkers(clients_details)
{
var marker = [];
for (var i = 0; i < clients_details.length; i++)
{
content = 'Test Content' ;
infowindow[i] = new google.maps.InfoWindow({
content: content,
maxWidth: 350
});
var client_location;
if (clients_details[i]['location'] !== null)
{
// Geting Lat and Lng from the database string
LatLng = clients_details[i]['location'];
client_location = new google.maps.LatLng (LatLng);
}
else
{
client_address = clients_details[i]['address'];
geocoder.geocode(
{ 'address': client_address},
function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
client_location = results[0].geometry.location;
}
else
alert('Geocode was not successful for the following\n\
reason: ' + clients_details[i]['name']+'\n' + status);
});
}
marker[i] = new google.maps.Marker({
position: client_location,
map: map,
title: clients_details[i]['name']
});
// Add 'click' event listener to the marker
addListenerMarkerList(infowindow[i], map, marker[i]);
}// for
}// function
【问题讨论】:
标签: javascript jquery google-maps google-maps-api-3 google-geocoder