我如何在javascript中进行地理编码以获得lat和long?

时间:2013-03-19 20:20:40

标签: javascript google-maps

这就是我的尝试,但我没有得到任何回应。可能有什么不对?

使用的API:

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=***&sensor=false"
            type="text/javascript"></script>



function initialize() {
     var addrLookup = 'San Diego , CA   92101';
     var geoCoder = new google.maps.Geocoder();
     var geoRequest = { address: addrLookup };
     geoCoder.geocode(geoRequest,function(results,status){
        window.alert("response");
        console.log(status);
     });
}

1 个答案:

答案 0 :(得分:0)

略微修改您的脚本:

<html>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
        function initialize() {
              var addrLookup = 'San Diego , CA   92101';
              var geoCoder = new google.maps.Geocoder();
              var geoRequest = { address: addrLookup };
              geoCoder.geocode(geoRequest,function(results,status){

                  console.log('latitude: ',results[0].geometry.location.lat());
                  console.log('longitude: ',results[0].geometry.location.lng());
              });
           }
    </script>

    <body onload="initialize()">


</body></html>