Click here to Skip to main content
15,887,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have embedded Google Maps within my web application.

I want to implement a feature for Reverse geocoding (getting the address from the latitude and longitude). I have already done it on Map click. You may check the following link...

Reverse Geocode on Map click[^]

Whenever you click on a map, it creates a maker and shows the address of the locaton..

Now I am trying to get the address on Marker click..

Can you help me? How should I code the marker?
Posted
Updated 28-Jul-10 5:39am
v4
Comments
kalyan10qwerty 7-Jan-12 0:51am    
hi will you please provide the code for reverse geocode on map click.

1 solution

Is this for the marker that you just created?

If so you can store the address details in an array and use them to populate the infowindow in the markers click event.

If not you could do some thing like this :-

google.maps.event.addListener(Marker, "click",function() {
    geocoder.geocode({'latLng': this.getPostion()}, function(results, status) {           
   if (status == google.maps.GeocoderStatus.OK) {
      wincont = ''
       $.each(results, function(){          
             $.each(this.address_components, function(){
                  switch (this.types.join(", ")) {
                            case 'street_number':
                                wincont += this.long_name
                                break;
                            case 'route':
                                wincont += this.long_name
                                break;
                            case 'locality, political':
                                wincont += this.long_name
                                break;
                            case 'postal_code':
                                wincont += this.long_name
                                break;
                    }
              });
         });
         gInfoWindow = new google.maps.infowindow()
         gInfoWindow.setContent(wincont);
         gInfoWindow.open(gMap,marker);
         } else {
              alert("An Error Occurred with Google's Geo-location Look up, Please Try again");
         }

});


note that this is using jQuery, you can just as easily use a normal for each loop, the results set can contain multiple results, you can just use the first if you wish, at the moment it will populate the infowindow with EVERY result (probably not what you want!) but it should be easy to only use the first result.

also note that this is only looking for specific address fields, this is copied from some old code where I only needed those fields, the post code value often comes back with only the general area code so you may want to do a length check on it first.

If you have multiple markers on the map you will want to bind a click event to each of them, I usually store an array of markers and add the event to the array reference.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900