【发布时间】:2014-09-19 20:12:49
【问题描述】:
我需要得到...例如,伦敦的纬度和经度。
所以我将字符串“伦敦”传递给一个函数,然后该函数返回我纬度和 lng,所以我可以在谷歌地图中将“中心”位置设置为伦敦。
这是我的一段代码:
function initialize() {
// i have below line working, as i was passing in lat and lng value directly....
//var latitudeLongtitude = new google.maps.LatLng(centreLatitude, centreLongitude)
// now i m trying to pass in a string - 'london' , it is not working....
var latitudeLongtitude = new google.maps.LatLng(getLatLong("London").lat(), getLatLong("London").lng());
var mapOptions = {
zoom: zoom,
center: latitudeLongtitude,
mapTypeId: mapType
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var marker = new google.maps.Marker({
position: latitudeLongtitude,
map: map,
title: 'Hello World!',
icon: markersImage
});
}
google.maps.event.addDomListener(window, 'load', initialize);
function getLatLong(address){
var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
return results[0].geometry.location;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
基本上,我试图修改基于this example的代码
请任何人都可以帮助我提供代码示例,谢谢.....
【问题讨论】:
-
您是否有有效的 API 密钥等。Read this 并遵循 TOS
-
是的....我有一个有价值的 api 密钥
-
你指的example不正确,地理编码器是asynchronous,回调函数无法返回结果。
标签: javascript google-maps-api-3 geolocation latitude-longitude geo