我需要的是......例如,伦敦的lat和lng 所以我将一个字符串'london'传入一个函数然后该函数返回lat和lng,所以我可以在google map中将'center'位置设置为london。
这是我的代码:
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
上修改代码库请任何人都可以帮我解决CODE示例,谢谢......
答案 0 :(得分:0)
由于google.maps.Geocoder
是异步方法,因此您需要在收到响应后调用initialize()
:
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
initialize( results[0].geometry.location.lat, results[0].geometry.location.lng );
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
并将您的initialize()
功能更改为接受lat
和lng
参数:
function initialize( lat, lng ) {
var latitudeLongtitude = new google.maps.LatLng( lat, lng );
//....
之后,您可以通过调用
来初始化地图getLatLong( 'London' );