DG3
DG3

Reputation: 5298

Google Map API to detect user location

I am learning about jquery mobile. I came across a sample code which uses the following google maps api

http://maps.google.com/maps/api/js?sensor=false

And the code is to detect the current location of the user

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); 
          }

In the above code, in the results array, there is an address_components array. How to determine the properties of the results object and then again the properties of the address_component?

Upvotes: 0

Views: 6358

Answers (1)

Related Questions