我正在尝试使用谷歌地图API从地址获取经纬度值,但似乎没有返回任何值。我的语法有问题吗?
var point = GClientGeocoder.getLatLng('1600 Pennsylvania Avenue NW Washington, DC 20500');
alert(point);
我正在尝试使用谷歌地图API从地址获取经纬度值,但似乎没有返回任何值。我的语法有问题吗?
var point = GClientGeocoder.getLatLng('1600 Pennsylvania Avenue NW Washington, DC 20500');
alert(point);
看起来你正在使用Google Maps的v2版本;如果你有兴趣使用v3版本(因为Google已经正式弃用了v2版本),你可以尝试以下操作:
var mygc = new google.maps.Geocoder();
mygc.geocode({'address' : '1600 Pennsylvania Avenue NW Washington, DC 20500'}, function(results, status){
console.log( "latitude : " + results[0].geometry.location.lat() );
console.log( "longitude : " + results[0].geometry.location.lng() );
});
这与其他示例类似,除了:
Google API文档为v3提供了更多详细信息。干杯!
address_string = '1600 Pennsylvania Avenue NW Washington, DC 20500';
geocoder = new GClientGeocoder();
geocoder.getLatLng(
address_string,
function(point) {
if (point !== null) {
alert(point); // or do whatever else with it
} else {
// error - not found, over limit, etc.
}
}
);
GClientGeocoder
的一个函数,你需要创建一个 new GClientGeocoder()
并调用它的函数。可以尝试这样写:
geocoder = new GClientGeocoder();
point = geocoder.getLatLng(address)