【发布时间】:2019-06-14 12:39:32
【问题描述】:
我是 javascript 的初学者。 我正在构建一个应用程序并决定实现一些地理定位功能,主要是向用户显示他当前的位置。 我做到了,但我的函数返回一个完整的地址(如街道、号码、社区、城市、邮政编码和国家) 这就是到目前为止所做的......
/* CURRENT LOCATION */
function geolocationSuccess(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var geocoder = new google.maps.Geocoder();
var latlng = {lat: latitude, lng: longitude};
geocoder.geocode({'location': latlng}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
if (results[0]){
var user_address = results[0].formatted_address;
document.getElementById("current_location").innerHTML = user_address;
}else {
console.log('No results found for these coords.');
}
}else {
console.log('Geocoder failed due to: ' + status);
}
});
}
function geolocationError() {
console.log("please enable location for this feature to work!");
}
$(document).ready(function() {
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationError);
} else {
alert("Geolocation not supported!");
}
});
我怎样才能只显示城市?
【问题讨论】:
标签: javascript geolocation google-geocoder