gpt4 book ai didi

google-maps-api-3 - 使用 Google map API v3 如何获取给定地址的 LatLng?

转载 作者:行者123 更新时间:2023-12-02 23:17:35 24 4
gpt4 key购买 nike

如果用户输入地址,我想转换为等效的 LatLng。

我已阅读文档,我认为我可以使用 Geocoder 类来执行此操作,但不知道如何实现它。

感谢您的帮助!

最佳答案

https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple 上有一个很好的例子

稍微缩短一下:

geocoder = new google.maps.Geocoder();

function codeAddress() {

//In this case it gets the address from an element on the page, but obviously you could just pass it to the method instead
var address = document.getElementById( 'address' ).value;

geocoder.geocode( { 'address' : address }, function( results, status ) {
if( status == google.maps.GeocoderStatus.OK ) {

//In this case it creates a marker, but you can get the lat and lng from the location.LatLng
map.setCenter( results[0].geometry.location );
var marker = new google.maps.Marker( {
map : map,
position: results[0].geometry.location
} );
} else {
alert( 'Geocode was not successful for the following reason: ' + status );
}
} );
}

关于google-maps-api-3 - 使用 Google map API v3 如何获取给定地址的 LatLng?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3926836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com