javascript 点击地图后获取地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10375925/
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-26 09:39:41  来源:igfitidea点击:

get address after clicking in the map

phpjavascriptgoogle-maps-api-3

提问by Houx

I need to get address and coordinates after clicking on the map, I'm using google map api v3, i recuperated the coordinates in inputs but i need also arddress of the point.

单击地图后,我需要获取地址和坐标,我使用的是 google map api v3,我恢复了输入中的坐标,但我还需要该点的地址。

 function initialize() {
            var myOptions = {
                center: new google.maps.LatLng(36.835769, 10.247693 ),
                zoom: 15,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("map_canvas"),
            myOptions);
            google.maps.event.addListener(map, 'click', function(event) {
                placeMarker(event.latLng);
            });

            var marker;
            function placeMarker(location) {
                if(marker){ //on vérifie si le marqueur existe
                    marker.setPosition(location); //on change sa position
                }else{
                    marker = new google.maps.Marker({ //on créé le marqueur
                        position: location, 
                        map: map
                    });
                }
                latitude.value=location.lat();
                longitude.value=location.lng();
            }

        }

I used this php script to get the address but if don't know how to use it in javascript? Can i use javascript to get address?

我使用这个 php 脚本来获取地址,但如果不知道如何在 javascript 中使用它?我可以使用javascript获取地址吗?

<?php
    $url      = 'http://where.yahooapis.com/geocode?location=40.714224,-73.961452&gflags=R&flags=J';
    $response = json_decode(file_get_contents($url));
    $location = $response->ResultSet->Results[0];
    foreach ((array) $location as $key => $value) {
            if($key == 'city')
                $city = $value;
            if($key == 'country')
                $country = $value;
            if($key == 'street')
                $street = $value;
    }
    echo "Street: ".$street."<br>";
    echo "City: ".$city;
    echo "<br/>Country: ".$country;

    ?>

回答by Tina CG Hoehr

Here's a simple example of retrieving the address. The results can get complicated (it is an array, from what I saw it's an arbitrary mix of cross streets, neighborhoods, state, and country. I didn't see a pattern)

这是检索地址的简单示例。结果可能会变得复杂(它是一个数组,从我看来,它是十字路口、社区、州和国家的任意组合。我没有看到模式)

I am only using the first line of results which is a combination of street, city, state, country. Read here the details:

我只使用第一行结果,它是街道、城市、州、国家的组合。在此处阅读详细信息:

https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingResults

https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingResults

A demo is here: http://jsfiddle.net/eB2RX/1/

演示在这里:http: //jsfiddle.net/eB2RX/1/

I noticed the resolution is not very good, I mean, in the USA you get street numbers but not in Tunisia. I only saw Street names.

我注意到分辨率不是很好,我的意思是,在美国你可以得到街道号码,但在突尼斯没有。我只看到街道名称。

Part of the code:

部分代码:

  var map;
  var geocoder;
  var mapOptions = { center: new google.maps.LatLng(0.0, 0.0), zoom: 2,
    mapTypeId: google.maps.MapTypeId.ROADMAP };

  function initialize() {
        var myOptions = {
            center: new google.maps.LatLng(36.835769, 10.247693 ),
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        geocoder = new google.maps.Geocoder();
        var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
        google.maps.event.addListener(map, 'click', function(event) {
            placeMarker(event.latLng);
        });

        var marker;
        function placeMarker(location) {
            if(marker){ //on vérifie si le marqueur existe
                marker.setPosition(location); //on change sa position
            }else{
                marker = new google.maps.Marker({ //on créé le marqueur
                    position: location, 
                    map: map
                });
            }
            document.getElementById('lat').value=location.lat();
            document.getElementById('lng').value=location.lng();
            getAddress(location);
        }

  function getAddress(latLng) {
    geocoder.geocode( {'latLng': latLng},
      function(results, status) {
        if(status == google.maps.GeocoderStatus.OK) {
          if(results[0]) {
            document.getElementById("address").value = results[0].formatted_address;
          }
          else {
            document.getElementById("address").value = "No results";
          }
        }
        else {
          document.getElementById("address").value = status;
        }
      });
    }
  }