【问题标题】:get address after clicking in the map点击地图后获取地址
【发布时间】:2012-05-09 16:40:25
【问题描述】:

点击地图后我需要获取地址和坐标,我使用的是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();
            }

        }

我使用这个 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;

    ?>

【问题讨论】:

    标签: php javascript google-maps-api-3


    【解决方案1】:

    这是一个检索地址的简单示例。结果可能会变得复杂(它是一个数组,据我所见,它是交叉街道、社区、州和国家的任意组合。我没有看到模式)

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

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

    这里有一个演示:http://jsfiddle.net/eB2RX/1/

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

    部分代码:

      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;
            }
          });
        }
      }
    

    【讨论】:

    • 拜托,我得到法语的结果,我需要英语的,任何想法。
    • @Houx 对不起,我没有适合你的解决方案,我得到的最接近的是这篇文章 stackoverflow.com/questions/6464788/…,我尝试了使用突尼斯 LatLng 的帖子中的 URL:maps.googleapis.com/maps/api/geocode/…我认为 Google 人员没有将突尼斯街道名称翻译成英文。
    • 您只需添加 URL "&language=en" 即可获得英文结果。无论如何谢谢你:)
    猜你喜欢
    • 2015-09-06
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    相关资源
    最近更新 更多