0
我工作的一個Web開發項目,我需要得到用戶的具體位置信息(如地區,城市,州和國家)。我怎麼做?如何使用Google地理定位API?
我工作的一個Web開發項目,我需要得到用戶的具體位置信息(如地區,城市,州和國家)。我怎麼做?如何使用Google地理定位API?
你可以得到這個通過谷歌的地理編碼和HTML5 Geolocater完成。您需要提供谷歌的地理編碼與ADRESS或緯度和經度,我們將開始使用HTML5 Geolocater。我會告訴你如何做到這一點,但你可以通過我提供的鏈接閱讀更多關於地理編碼的內容。無論如何,我們可以通過使用HTML5 Geolocater來獲得用戶的經驗。然後,您可以使用Google的Geocoder獲取城市,州,街道和郵編。 下面是做到這一點的步驟:
首先,把谷歌的地理編碼成變量。
var geocoder = new google.maps.Geocoder;
然後搶用戶緯度經度與HTML5地理編碼和使用谷歌的地理編碼將它做成一個地址。
if(navigator.geolocation){
// call this function to get the location
navigator.geolocation.getCurrentPosition(function(position) {
// the results return as a lat lng,
//which we will put them into a variable to use in Google's Geocoder
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
// Now we can get the location info(city, state, zip) with Google's Geocoder
geocoder.geocode({'location': pos}, function(results, status) {
if(status === 'OK'){
console.log(results);
}
});
});
}
然後,你可以做任何你想要的結果。希望這可以幫助!
消息來源: https://developers.google.com/maps/documentation/javascript/geolocation