views:

1144

answers:

2

Hi.

I am playing around with Google Maps for the first time. And I am struggling to extact some information about the location where the marker is.

I have set up an example for you to see. The problem happens when I drag the A-node, the getGeocode() returns undefined?

I need to extract zip-code, city, and address from the node when I drag it around.

Please see http://dev.korebogen.dk/gmap/

Thanks in advance!

Update

I edited my code from the responses - and now I get both addresses out (still need to strip it so I get the info in seperate variables) but for some reason my direction output turns out to be "0" and "1" (array) do I really need to reverse geocode that aswell?

And is it possible to make a click event on the map to add A and B markers (if there is no directions when loading?) http://dev.korebogen.dk/gmap/ is updated!

It comes from:

var param = i + "@" + newMarkers[i].getLatLng().y + "," + newMarkers[i].getLatLng().x;

This is the updated part:

GEvent.addListener(newMarkers[i], "dragend", function() {
      var points = [];
      for (var i = 0; i < newMarkers.length; i++) {
       var param = i + "@" + newMarkers[i].getLatLng().y + "," + newMarkers[i].getLatLng().x;
       //points[i] = newMarkers[i].getLatLng();
       points.push(param);
       if (!i) {
        var x = new GClientGeocoder();

        x.getLocations(newMarkers[0].getLatLng(), function(addresses) {
         if (addresses.Status.code == 200) {
          var address = addresses.Placemark[0].address;
          document.getElementById("start").innerHTML = address;
         }
        });

        x.getLocations(newMarkers[1].getLatLng(), function(addresses) {
         if (addresses.Status.code == 200) {
          var address = addresses.Placemark[0].address;
          document.getElementById("end").innerHTML = address;
         }
        });
       }
      }
      gdir.loadFromWaypoints(points);

     });
+1  A: 

You're trying to convert a position to an address, commonly called "reverse geocoding".

See example code here.

In short, instead of calling getGeocode, you should be calling GClientGeocoder.getLocations

Andrew Gove
Super! Got me in the right direction! :-D
A: 

The problem here is that the object you are getting back from your loadFromWaypoints request is not populated with all the members that you are asking for in onGDirectionsLoad (). If you a look in your javascript error log you will see:

Error: gdir.getRoute(0).getStartGeocode().AddressDetails.Country is undefined

Ok, according to the Google Documention, the response from loadFromWaypoints and GClientGeodcoder should be identical.

I took the liberty of doing some testing with your sample and it suddenly becomes obvious that the responses are not the same. If you have a look at http://www.cannonade.net/geo.js, you can see that I stringify the GClientGeocoder request and the response for the first marker and they are very different. So you need to do a couple of things:

  • Check your responses to ensure that you don't get javascript errors for unpopulated results:

    if ('AddressDetails' in resp)

  • Do a GClientGeocoder request for each marker point to get the full address information. My sample does this for the first marker.

Cannonade
Oops, forgot the link to the doco that says loadFromWaypoints returns results matching GClientGeocoder http://code.google.com/apis/maps/documentation/reference.html#GDirections.getGeocode
Cannonade
This is great! I need to post some code, so please see my updated post in the top :)