2017-09-27 124 views
0

獲取當前現在的位置名字我googlemap API,它能夠讓用戶currentlocationlatitudelongitude,而是我想要得到的location name如何使用谷歌地圖API

例如我的位置是班加羅爾=> vidyaranyapura我想vidyaranyapura

這裏是演示:https://jsfiddle.net/aoe4vf17/100/

HTML:

<div id="map"></div> 

的javascript:

var map = new google.maps.Map(document.getElementById("map"), { 
    center: new google.maps.LatLng(53.3478, -6.2597), 
    zoom: 16, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}); 

var infoWindow = new google.maps.InfoWindow({ 
    map: map 
}); 
// Try HTML5 geolocation. 
if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
    var pos = { 
     lat: position.coords.latitude, 
     lng: position.coords.longitude 
    }; 
    console.log(position); 
    infoWindow.setPosition(pos); 
    infoWindow.setContent('<b>You are here.</b>'); 
    map.setCenter(pos); 
    var marker = new google.maps.Marker({ 
     position: pos, 
     map: map, 
     title: String(pos.lat) + ", " + String(pos.lng), 
    }); 
    }, function() { 
    handleLocationError(true, infoWindow, map.getCenter()); 
    }); 
} else { 
    // Browser doesn't support Geolocation 
    handleLocationError(false, infoWindow, map.getCenter()); 
} 

回答

1

您可以使用反向地理編碼用於此目的。 以下是提供給Google API的latitudelongitude的工作示例。

更多信息可以在這裏找到。
https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

var geocoder = new google.maps.Geocoder; 
 

 
function geocodeLatLng(lat, lng) { 
 

 
    var latlng = { 
 
    lat: lat, 
 
    lng: lng 
 
    }; 
 

 
    geocoder.geocode({ 
 
    'location': latlng 
 
    }, function(results, status) { 
 
    if (status === 'OK') { 
 
     if (results[0]) { 
 

 
     //This is yout formatted address 
 
     window.alert(results[0].formatted_address); 
 

 
     } else { 
 
     window.alert('No results found'); 
 
     } 
 
    } else { 
 
     window.alert('Geocoder failed due to: ' + status); 
 
    } 
 
    }); 
 

 
} 
 

 

 
geocodeLatLng(53.3478, -6.2597);
<script src="https://maps.googleapis.com/maps/api/js?v=3&amp;sensor=false"></script>

0

製作一個利用這一點,並確保在HTTPS域中運行它。

function getlocation(){ 
     /* Chrome need SSL! */ 
     var is_chrome = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase()); 
     var is_ssl = 'https:' == document.location.protocol; 
     if(is_chrome && ! is_ssl){ 
      return false; 
     } 
     /* HTML5 Geolocation */ 
     navigator.geolocation.getCurrentPosition(
      function(position){ // success cb 

       /* Current Coordinate */ 
       var lat = position.coords.latitude; 
       var lng = position.coords.longitude; 
       var google_map_pos = new google.maps.LatLng(lat, lng); 

       /* Use Geocoder to get address */ 
       var google_maps_geocoder = new google.maps.Geocoder(); 
       google_maps_geocoder.geocode(
        { 'latLng': google_map_pos }, 
        function(results, status) { 
         if (status == google.maps.GeocoderStatus.OK && results[0]) { 
          console.log(results[0].formatted_address); 
          currentlocation = results[0].formatted_address; 

         } 
        } 
       ); 
      }, 
      function(){ // fail cb 
      } 
     ); 
}