【发布时间】:2017-02-26 02:32:36
【问题描述】:
我想从多个邮政编码中获取纬度和经度,但是当我循环通过邮政编码数组调用 geocoder.geocode 方法时,它返回相同的纬度和经度值。我的源代码如下
var postcodeList = [
{postcode: "ON N6A 1A1", name: "1. Lawson Cafe"},
{postcode: "ON N6J 2J8", name: "2. LA Cafe"},
{postcode: "ON N6J 1H6", name: "3. Frangos COffee Shop"},
{postcode: "ON N6C 3P4", name: "4. Cortado"},
{postcode: "ON N6B 2K9", name: "5. Walker's Restaurant"},
{postcode: "ON N6B 1L4", name: "6. Kambie Chinese Restaurant"},
{postcode: "ON N6C 4M8", name: "7. Curry's Restaurant"}
];
var googleMap;
function initialize() {
googleMap = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
mapTypeControl: false,
center: new google.maps.LatLng(51.509865, -0.118092),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
for (i = 0; i < postcodeList.length; i++) {
searchAddress(postcodeList[i].postcode);
}
}
function searchAddress(postcode) {
var geocoder = new google.maps.Geocoder();
var location;
geocoder.geocode({'address': postcode}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log("PostCode: " + postcode);
console.log(" Latitude: " + results[0].geometry.location.lat());
console.log(" Longtitude: " + results[0].geometry.location.lng());
} else {
console.log("Geocode was not successful for the following reason: " + status);
}
});
}
上面源码的结果是
PostCode: ON N6A 1A1
Latitude: 42.9975524
Longtitude: -81.25463660000003
PostCode: ON N6J 2J8
Latitude: 42.9535959
Longtitude: -81.27941229999999
PostCode: ON N6J 1H6
Latitude: 42.9535959
Longtitude: -81.27941229999999
PostCode: ON N6C 3P4
Latitude: 42.9730003
Longtitude: -81.2540343
PostCode: ON N6B 2K9
Latitude: 42.9818077
Longtitude: -81.23811510000002
PostCode: ON N6B 1L4
Latitude: 42.9818077
Longtitude: -81.23811510000002
PostCode: ON N6C 4M8
Latitude: 42.9711174
Longtitude: -81.2363939
在上面的结果输出中,2、3的经纬度为相同的值,5、6的经纬度也为相同的值。 我不知道为什么,请帮助我
【问题讨论】:
-
你为什么认为它们应该不同?
-
@geocodezip - 不同的邮政编码代表不同的位置
-
我认为这意味着问题在于邮政编码的格式,谷歌只是将邮政编码的位置返回到空格(FSA)。
标签: javascript google-maps geocode postal-code