javascript 我如何通过给定的纬度/经度获取地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20743351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:11:31  来源:igfitidea点击:

How do i get address by given lat/long

javascriptgoogle-mapsreverse-geocoding

提问by user1987095

How do i get address by given lat/long not location.I have tried with "Reverse Geocoding" but it showing only location name.

我如何通过给定的经纬度而不是位置获取地址。我尝试过“反向地理编码”,但它只显示位置名称。

My code is

我的代码是

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var geocoder;
var map;
var marker;
var marker2;

function initialize() {
    geocoder = new google.maps.Geocoder();
    var
    latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
        zoom: 5,
        center: latlng
    }
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    google.maps.event.addListener(map, 'click', function (event) {
        //alert(event.latLng);          
        geocoder.geocode({
            'latLng': event.latLng
        }, function (results, status) {
            if (status ==
                google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                    alert(results[1].formatted_address);
                } else {
                    alert('No results found');
                }
            } else {
                alert('Geocoder failed due to: ' + status);
            }
        });
    }); <!--click event--> }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="map-canvas"></div>

回答by Code L?ver

Check these two references:

检查这两个参考:

How to get address location from latitude and longitude in Google Map.?

如何从谷歌地图中的经纬度获取地址位置。?

Get location address from Latitude and Longitude in google maps

从谷歌地图中的纬度和经度获取位置地址

The answer of these will help you.

这些答案对你有帮助。

use this URL to get the address:

使用此 URL 获取地址:

http://maps.googleapis.com/maps/api/geocode/json?latlng=44.4647452,7.3553838&sensor=true

回答by Anto Jurkovi?

Address with street number is at index 0. Use

带有街道号码的地址在索引 0 处。使用

results[0].formatted_address

instead of

代替

results[1].formatted_address

And you will get the address

你会得到地址

For example, at index 0 you get: "36 Osborne Street, Wollongong ...", at index 1 you get "Wollongong Hospital Crown St, Wollongong ...".

例如,在索引 0 处,您会得到:“36 Osborne Street, Wollongong ...”,在索引 1 处,您会得到“Wollongong Hospital Crown St, Wollongong ...”

You can get components of formatted_addressusing

您可以获得formatted_address使用的组件

results[0].address_components[0]
...
results[0].address_components[5]

回答by Rami Sakr

I think what you are looking for is:

我认为你正在寻找的是:

results[1].geometry.location

回答by Shylendra Madda

Try to pass your lat and long to the following query string, and you will get a json array, fetch your city from there

尝试将您的 lat 和 long 传递给以下查询字符串,您将获得一个 json 数组,从那里获取您的城市

http://maps.googleapis.com/maps/api/geocode/json?latlng=44.4647452,7.3553838&sensor=true

http://maps.googleapis.com/maps/api/geocode/json?latlng=44.4647452,7.3553838&sensor=true