使用邮政编码的Google API地理编码无法正常工作

时间:2018-10-10 12:20:13

标签: php google-geocoding-api

Google Maps地理编码API不会返回google maps返回的结果

$result_string = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($zip)."&sensor=false&mode=driving");

$result = json_decode($result_string, true);

$result1[]=$result['results'][0];
$result2[]=$result1[0]['geometry'];
$result3[]=$result2[0]['location'];

return $result3[0];

1 个答案:

答案 0 :(得分:0)

检查下面的代码,希望对您有所帮助。

<input type="button" onclick="GetLocation()" value="Get Location" />
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
<!--
    function GetLocation() {
        var geocoder = new google.maps.Geocoder();
        var address = document.getElementById("txtAddress").value;
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
                alert("Latitude: " + latitude + "\nLongitude: " + longitude);
            } else {
                alert("Request failed.")
            }
        });
    };
</script>