javascript 获取当前地址 - Google Places API
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 获取当前地址 - Google Places API相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html>
<html>
<body>
<P id="textPosition">position will be displayed here</P>
<P id="textAddress">address will be displayed here</P>
<script>
function initMap() {
// get current position
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
document.getElementById("textPosition").innerHTML = pos.lat + ',' +pos.lng;
// get address from position
var geocoder = new google.maps.Geocoder;
geocoder.geocode({'location': pos}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
document.getElementById("textAddress").innerHTML = results[0].formatted_address;
} else {
handleLocationError('No results found');
}
} else {
handleLocationError('Geocoder failed due to: ' + status);
}
});
}, function() {
handleLocationError();
});
} else {
// Browser doesn't support Geolocation
handleLocationError('Browser doesn\'t support Geolocation');
}
}
function handleLocationError(errorMessage) {
window.alert('Error: ' + errorMessage);
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&language=he-IL&callback=initMap">
</script>
</body>
</html>
以上是关于javascript 获取当前地址 - Google Places API的主要内容,如果未能解决你的问题,请参考以下文章
Javascript教程:获取当前地址栏url
JavaScript中关于地址的获取
GPS室内精度
JavaScript获取当前url路径
arcgis api for javascript中怎么获取当前地图的大小
用javaScript怎么样获取用户本地的ip地址?