Getting country code from Google Maps and HTML 5 GeoLocation

前端 未结 1 1905
南方客
南方客 2021-01-15 16:17

I am trying to use HTML 5 GeoLocation to get a longitude and latitude and then use Google Maps API to get the country code of that longitude/latitude. Can anybody tell me w

相关标签:
1条回答
  • 2021-01-15 16:52

    Try this:

    <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 讨论(0)
提交回复
热议问题