Google映射apigeocoder.geocodereturn相同值
Google map api geocoder.geocode return the same value
我想从多个邮政编码获取纬度和经度,但是当我循环遍历邮政编码数组以调用 geocoder.geocode 方法时,它 returns 相同的纬度和经度值。我的源代码如下
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个的纬度和经度也是相同的值。
不知道为什么,求助
抱歉,我认为我的邮政编码数据是正确的。
我更新邮编数据后,问题解决了
var postcodeList = [
{postcode: "ON N6A1A1", name: "1. Lawson Cafe"},
{postcode: "ON N6J2J8", name: "2. LA Cafe"},
{postcode: "ON N6J1H6", name: "3. Frangos COffee Shop"},
{postcode: "ON N6C3P4", name: "4. Cortado"},
{postcode: "ON N6B2K9", name: "5. Walker's Restaurant"},
{postcode: "ON N6B1L4", name: "6. Kambie Chinese Restaurant"},
{postcode: "ON N6C4M8", name: "7. Curry's Restaurant"}
];
我想从多个邮政编码获取纬度和经度,但是当我循环遍历邮政编码数组以调用 geocoder.geocode 方法时,它 returns 相同的纬度和经度值。我的源代码如下
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个的纬度和经度也是相同的值。 不知道为什么,求助
抱歉,我认为我的邮政编码数据是正确的。 我更新邮编数据后,问题解决了
var postcodeList = [
{postcode: "ON N6A1A1", name: "1. Lawson Cafe"},
{postcode: "ON N6J2J8", name: "2. LA Cafe"},
{postcode: "ON N6J1H6", name: "3. Frangos COffee Shop"},
{postcode: "ON N6C3P4", name: "4. Cortado"},
{postcode: "ON N6B2K9", name: "5. Walker's Restaurant"},
{postcode: "ON N6B1L4", name: "6. Kambie Chinese Restaurant"},
{postcode: "ON N6C4M8", name: "7. Curry's Restaurant"}
];