Hou*_*oux 7 javascript php google-maps-api-3
点击地图后我需要获取地址和坐标,我正在使用谷歌地图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;
    ?>
Tin*_*ehr 17
这是一个检索地址的简单示例.结果会变得复杂(这是一个数组,从我看到它是交叉街道,社区,州和国家的任意组合.我没有看到模式)
我只使用第一行结果,这是街道,城市,州,国家的组合.在这里阅读详细信息:
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;
        }
      });
    }
  }
| 归档时间: | 
 | 
| 查看次数: | 10688 次 | 
| 最近记录: |