How do i get address by given lat/long

浪子不回头ぞ 提交于 2019-12-13 07:45:27

问题


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>

回答1:


Address with street number is at index 0. Use

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 ...".

You can get components of formatted_address using

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



回答2:


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:

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



回答3:


I think what you are looking for is:

results[1].geometry.location



回答4:


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

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



来源:https://stackoverflow.com/questions/20743351/how-do-i-get-address-by-given-lat-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!