Javascript 如何获取位置';文本中地址的纬度和长度
我需要的是……例如,伦敦的lat和lng。Javascript 如何获取位置';文本中地址的纬度和长度,javascript,google-maps-api-3,geolocation,latitude-longitude,geo,Javascript,Google Maps Api 3,Geolocation,Latitude Longitude,Geo,我需要的是……例如,伦敦的lat和lng。 因此,我将字符串“london”传入一个函数,然后函数返回lat和lng,这样我就可以在谷歌地图中将“center”位置设置为london 下面是我的一段代码: function initialize() { // i have below line working, as i was passing in lat and lng value directly.... //var latitudeLongtitude = new google.ma
因此,我将字符串“london”传入一个函数,然后函数返回lat和lng,这样我就可以在谷歌地图中将“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);
}
});
}
基本上,我是试图修改代码的基础上
请任何人都可以帮助我了解代码示例,谢谢….As
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' );
你是否有一个有效的API密钥等,并遵循TOSIES…我有一个有值的API密钥你所指的是不正确的,地理编码器是,你不能从回调函数返回结果。sry…我感到困惑…到底是怎么回事?所以我首先使用geo.geocode方法,然后在第二个位置初始化,然后在第三个位置使用函数getLatLong(address)?我应该把getLatLong('London')?干杯…你需要把它而不是“initialize()”调用。我不知道你叫它哪儿。在body标记或window.load方法中