Gmap geocoding details

Hello experts.
I have a database table with the latitude and longitude of places.
I need help to create script to catch the postalcode of these places using gmap geocoding details.
Testing on this site http://gmaps-samples-flash.googlecode.com/svn/trunk/demos/GeocodingDetails/GeocodingDetails.html  ,PostalCode = (Object)#8

Any help?
LVL 2
PanosAsked:
Who is Participating?
 
lwadwellConnect With a Mentor Commented:
Have a look at the google API documentation for reverse geocoding
https://developers.google.com/maps/documentation/javascript/geocoding#ReverseGeocoding
0
 
PanosAuthor Commented:
Hi lwadwell.
Thank yu for the link.
I made some changes.
1.I will use the lat and lng dynamic so i will set it as a var directly:
var input = '40.714224,-73.961452';
2. Having the latlng on pageload  i execute the codeLatLng() function on pageload:
<body onload="initialize();codeLatLng()">

Now i need to catch only the postalcode from 'results[1].formatted_address' set the value to the postalcode hidden field and than submit the form to the action page to store the value.
Or using jquery post all the data to the action page.

Any help?
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Google Maps JavaScript API v3 Example: Reverse Geocoding</title>
    <style type="text/css">
	html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

#map_canvas {
  height: 100%;
}

@media print {
  html, body {
    height: auto;
  }

  #map_canvas {
    height: 650px;
  }
}
	</style>
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script>
      var geocoder;
      var map;
      var infowindow = new google.maps.InfoWindow();
      var marker;
      function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(40.730885,-73.997383);
        var mapOptions = {
          zoom: 8,
          center: latlng,
          mapTypeId: 'roadmap'
        }
        map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
      }

      function codeLatLng() {
        //var input = document.getElementById('latlng').value;
		var input = '40.714224,-73.961452';
        var latlngStr = input.split(',', 2);
        var lat = parseFloat(latlngStr[0]);
        var lng = parseFloat(latlngStr[1]);
        var latlng = new google.maps.LatLng(lat, lng);
        geocoder.geocode({'latLng': latlng}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
              map.setZoom(11);
              marker = new google.maps.Marker({
                  position: latlng,
                  map: map
              });
              infowindow.setContent(results[1].formatted_address);
              infowindow.open(map, marker);
            } else {
              alert('No results found');
            }
          } else {
            alert('Geocoder failed due to: ' + status);
          }
        });
      }
    </script>
  </head>
  <body onload="initialize();codeLatLng()">
    <div>
     <form id="frmpostalcode" name="frmpostalcode" method="post" action="myactionpage"
      <input id="postalcode" name="postalcode" type="hidden">
     </form>
    </div>
    <div id="map_canvas" style="height: 90%; top:60px; border: 1px solid black;"></div>
  </body>
</html>

Open in new window

0
 
lwadwellConnect With a Mentor Commented:
In results[1] there is another element called 'address_components' ... this is an array with the different address elements broken down into individual values (do a "console.log(results[1]);" and see for yourself).  I would use that instead.  The entire object looks like
{
    "address_components": [
        {
            "long_name": "Grand St - Bedford Av",
            "short_name": "Grand St - Bedford Av",
            "types": [
                "bus_station",
                "transit_station",
                "establishment"
            ]
        },
        {
            "long_name": "Williamsburg",
            "short_name": "Williamsburg",
            "types": [
                "neighborhood",
                "political"
            ]
        },
        {
            "long_name": "Brooklyn",
            "short_name": "Brooklyn",
            "types": [
                "sublocality",
                "political"
            ]
        },
        {
            "long_name": "Kings",
            "short_name": "Kings",
            "types": [
                "administrative_area_level_2",
                "political"
            ]
        },
        {
            "long_name": "New York",
            "short_name": "NY",
            "types": [
                "administrative_area_level_1",
                "political"
            ]
        },
        {
            "long_name": "United States",
            "short_name": "US",
            "types": [
                "country",
                "political"
            ]
        },
        {
            "long_name": "11211",
            "short_name": "11211",
            "types": [
                "postal_code"
            ]
        }
    ],
    "formatted_address": "Grand St - Bedford Av, Brooklyn, NY 11211, USA",
    "geometry": {
        "location": {
            "Xa": 40.714321,
            "Ya": -73.96115099999997
        },
        "location_type": "APPROXIMATE",
        "viewport": {
            "ca": {
                "b": 40.7129720197085,
                "f": 40.71566998029149
            },
            "ea": {
                "b": -73.96249998029151,
                "f": -73.95980201970849
            }
        }
    },
    "types": [
        "bus_station",
        "transit_station",
        "establishment"
    ]
} 

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
PanosAuthor Commented:
hmmmm..
The link you posted is telling the same.
Can you help me a little more hw to use it?
My knowledge in javascript does not help me.
0
 
PanosAuthor Commented:
I got it with this help:http://stackoverflow.com/questions/2989323/gmaps-address-component-types-get-country-name

      var geocoder;
      var map;
      var infowindow = new google.maps.InfoWindow();
      var marker;

      function initialize() {
          geocoder = new google.maps.Geocoder();
          var latlng = new google.maps.LatLng(40.730885, - 73.997383);
          var mapOptions = {
              zoom: 8,
              center: latlng,
              mapTypeId: 'roadmap'
          }
          map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
      }

      function codeLatLng() {
          //var input = document.getElementById('latlng').value;
          var input = '40.714224,-73.961452';
          var latlngStr = input.split(',', 2);
          var lat = parseFloat(latlngStr[0]);
          var lng = parseFloat(latlngStr[1]);
          var latlng = new google.maps.LatLng(lat, lng);
          geocoder.geocode({
              'latLng': latlng
          }, function (results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                  if (results[1]) {
                      map.setZoom(11);
                      marker = new google.maps.Marker({
                          position: latlng,
                          map: map
                      });
                      var postal_code;

                      for (i = 0; i < results[0].address_components.length; i++) {
                          for (j = 0; j < results[0].address_components[i].types.length; j++) {
                              if (results[0].address_components[i].types[j] == "postal_code") postal_code = results[0].address_components[i].long_name
                          }
                      }
                      alert(postal_code);
                      infowindow.setContent(results[1].formatted_address);
                      infowindow.open(map, marker);
                  } else {
                      alert('No results found');
                  }
              } else {
                  alert('Geocoder failed due to: ' + status);
              }
          });
      }

Open in new window

0
 
lwadwellConnect With a Mentor Commented:
a) how to use what? ... and b) what will you process the form in?

But I should ask what you are trying to do?  You want to reverse geocode a series of lat-long values in a database into postcode (which country btw).  How many do you want to do (google does limit the number of requests ... as do other services).
What I trying to get to, eventually, it understand whether javascript is the right 'tool' to crack the nut with.
0
 
PanosAuthor Commented:
...google does limit the number of requests
Do you know what the limits are?
0
 
PanosAuthor Commented:
thank you lwadwell for your help
0
 
PanosAuthor Commented:
thank you
regards
panos
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.