2012-01-20 46 views
1

我想使用HTML 5地理位置获取经度和纬度,然后使用Google Maps API获取经度/纬度的国家代码。谁能告诉我在哪里,我在我的代码去错了,我现在得到了JavaScript错误“this.lat不是一个函数”在main.js:从谷歌地图和HTML 5获取国家代码地理位置

<html> 

<head> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script> 
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=drnhdhddfhgfghfg" type="text/javascript"></script> 

<script type="text/javascript"> 

    if (navigator.geolocation) { 

      navigator.geolocation.getCurrentPosition(function(position) { 

      var lat = position.coords.latitude; 
      var lng = position.coords.longitude; 

       var latlng = new google.maps.LatLng(lat, lng); 

      $.post('http://maps.googleapis.com/maps/api/geocode/json', { latlng: latlng, sensor: false }, function (results) { 
       alert(results); 
      }); 
      }); 




    } 
    else { 
      alert("Geolocation services are not supported by your browser."); 
    } 

</script> 

</head> 

<body> 



</body> 

</html> 
+0

可能[Google Maps Jquery:如何从Google Places API获取国家/地区代码]的副本(http://stackoverflow.com/questions/33887081/google-maps-jquery-how-to-get-the-country-code-from -google-places-api) – geocodezip

回答

1

试试这个:

<html> 

<head> 
<script src="http://maps.google.com/maps/api/js?sensor=true" type="text/javascript"></script> 
<script type="text/javascript"> 

if (navigator.geolocation) { 

     navigator.geolocation.getCurrentPosition(function(position) { 

     var lat = position.coords.latitude; 
     var lng = position.coords.longitude; 

     var latlng = new google.maps.LatLng(lat, lng); 

    var geocoder = new google.maps.Geocoder(); 

geocoder.geocode({'latLng': latlng}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
    if (results[1]) { 
    alert(results[7].formatted_address); 
    } 
    } else { 
    alert("Geocoder failed due to: " + status); 
    } 
}); 
     }); 
     } else { 
     alert("Geolocation services are not supported by your browser."); 
} 

</script> 
</head> 
<body> 
</body> 
</html> 
+0

每当我尝试引用Geocoder类时,都无法识别它。任何想法为什么? – user517406

+0

是否正确引用地图API v3? – jenswirf

+0

我不确定,你有一个如何正确引用它的例子吗? – user517406