Google maps 谷歌地图API检测用户位置
我正在学习jquery mobile。我遇到了一个使用以下GoogleMapsAPI的示例代码Google maps 谷歌地图API检测用户位置,google-maps,jquery-mobile,Google Maps,Jquery Mobile,我正在学习jquery mobile。我遇到了一个使用以下GoogleMapsAPI的示例代码 http://maps.google.com/maps/api/js?sensor=false 代码用于检测用户的当前位置 function getPosition(position) { var lat = position.coords.latitude; var lng = position.coords.longitude; var latl
http://maps.google.com/maps/api/js?sensor=false
代码用于检测用户的当前位置
function getPosition(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();
if (geocoder) {
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
//window.alert(results[1].address_components[0].long_name);
var locationVal = results[1].address_components[0].long_name;
var stateVal = results[1].address_components[1].short_name;
var location = document.getElementById('location');
location.value = locationVal;
}
}
});
}
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getPosition);
}
在上面的代码中,结果数组中有一个address\u components数组。如何确定results对象的属性,然后再确定address_组件的属性?请参见此处:顺便说一句,如果您使用的是API,则必须将sensor设置为true