我正在使用 Google map 地理编码功能。我得到了一些地址,并将其加载到 PHP 变量中。地址由用户可以输入邮政编码的文本框定义。我举个例子:
用户在文本框中键入邮政编码(例如 12345)。现在它从系统加载地址(这是内部的 - 但它可以加载地址)。然后它显示一些地址。这些地址现在存储在 PHP
变量 ($address
) 中。现在我想从这些不同的地址获取纬度
和经度
。
我看了这个帖子:Javascript geocoding from address to latitude and longitude numbers not working
我使用了文章中的代码:
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert(latitude + ' - ' + longitude);
这很好,但它只是给我在文本框中输入的地址中的latlng
,而不是我获得的所有地址。
有没有办法从所有不同的地址获取纬度
和经度
?如果是,有人可以解释一下吗?我是 JavaScript
和 Google Maps API
的新手。
提前致谢
最佳答案
您可以使用示例 given here 。 具体功能请参见下面的代码:
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.
}
});
}
关于javascript - 使用 PHP 和 JavaScript 从不同地址获取 latlng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20488458/