我使用的是谷歌地图地理编码功能。我得到了一些地址,并将它们加载到PHP变量中。地址由一个文本框定义,用户可以在其中键入邮政编码。我举一个例子:
用户在文本框中键入邮政编码(例如12345)。现在它从系统加载地址(这是内部的,但它可以加载地址)。然后它会显示一些地址。这些地址现在存储在PHP
变量($address
)中。现在我想从这些不同的地址得到latitude
和longitude
。
我看了一下这个线程:从地址到纬度和经度数字的Javascript地理编码不起作用
我使用了文章中的代码:
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert(latitude + ' - ' + longitude);
这很好,但它只是根据我在文本框中键入的地址给我latlng
,而不是根据我得到的所有地址。
有没有办法从所有不同的地址获得latitude
和longitude
?如果是,有人能解释一下吗?我是JavaScript
和Google Maps API
的新手。
提前感谢
您可以使用这里给出的示例。有关特定功能,请参阅以下代码:
geocoder = new google.maps.Geocoder();
function findAddress(address)
{
// You can pass the address from your PHP to Javascript using AJAX. Here, I'm assuming that you have done it and that you're passing it as a string argument to this function
geocoder.geocode( { 'address': address}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
return position: results[0].geometry.location;
}
else
{
// Show some message that the address cannot be found on map.
}
});
}