使用 Google 地图 API v3 如何使用给定地址获取 LatLng? [英] Using Google maps API v3 how do I get LatLng with a given address?

查看:26
本文介绍了使用 Google 地图 API v3 如何使用给定地址获取 LatLng?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If a user inputs an address, I want to convert to the equivalent LatLng.

我已经阅读了文档,我想我可以使用 Geocoder 类来做到这一点,但不知道如何实现它.

I've read the documentation, and I think I can use the Geocoder class to do this, but can't figure out how to implement it.

感谢您的帮助!

推荐答案

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 地图 API v3 如何使用给定地址获取 LatLng?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆