SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Obtaining Latitude & Longitude from GMapsV3

    I'm using Googles Map API V3 to geocode an address, then I want to store the Latitude and Longitude that it is returning. The Latitude that I am hard coding to "35" is showing up as expected, however I'm failing to capture the "results" of the GeoCode request. Can anyone spot where I am missing them at?

    Code:
    function loadScript()
    {
    var script = document.createElement("script");
    script.src = "http://maps.googleapis.com/maps/api/js?      key=AIzaSyDaukwjA3JdWZhHyUhvxiEHfKAjPhAz8jM&sensor=false&callback=initialize"; document.body.appendChild(script);
    }
    
    window.onload = loadScript;
    
    
    var geocoder;
      var map;
      function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var mapOptions = {
          zoom: 17,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
      }
    
      function codeAddress() {
       var addresstxt = document.getElementById("properties_street_address").value;
       var citytxt = document.getElementById("properties_city").value;
       var statetxt = document.getElementById("properties_state").value;
       var zipcodetxt = document.getElementById("properties_zip_code").value;
       
        var address = addresstxt + ' ' + citytxt+','+ statetxt+' '+zipcodetxt;
        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
            var lat = '35';
            var lng = results[0].geometry.location.lng;
            document.getElementById("properties_lattitude").value = lat;
            document.getElementById("properties_longitude").value = lng;
          } else {
            alert("Geocode was not successful for the following reason: " + status);
          }
        });
      }

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Normally Google maps attach prototypes to object such as these so it can retrieve the lat/lng for you, try the following and if it doesn't work simply do a console log of the object to see what is contains.

    Code JavaScript:
    results[0].geometry.location.lng()
    or try logging the object
    Code JavaScript:
    console.log(results[0].geometry.location)

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much! the parenthesis at the end made all the difference.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    No problem


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •