Google Map API用于检测用户位置

时间:2012-01-26 02:12:00

标签: google-maps jquery-mobile

我正在学习jquery mobile。我遇到了一个使用以下google maps api

的示例代码
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); 
          }

在上面的代码中,在results数组中,有一个address_components数组。如何确定结果对象的属性,然后再确定address_component的属性?

1 个答案:

答案 0 :(得分:1)