使用 Google 地图 API v3 如何使用给定地址获取 LatLng?

编程入门 行业动态 更新时间:2024-10-09 09:23:30
本文介绍了使用 Google 地图 API v3 如何使用给定地址获取 LatLng?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

如果用户输入地址,我想转换为等效的 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/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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-31 21:59:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/815289.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   地址   地图   Google   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!