Trouble converting coordinates to address using reverse geocoding

268 views
Skip to first unread message

gmoney

unread,
Aug 16, 2011, 12:48:12 PM8/16/11
to google-map...@googlegroups.com
Here is a link to the working code:
http://punchupproductions.com/external/hotel/

I;m having trouble getting the "Start" field to show the address location instead of the coordinates. I know I have to use reverse geocoding but I'm not sure how to go about it. Note: I am using the W3C geolocation to get the users current location. Any help is greatly appreciated.

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  var chicago = new google.maps.LatLng(41.850033, -87.6500523);
  var myOptions = {
    zoom:7,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: chicago
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  directionsDisplay.setMap(map);
  directionsDisplay.setPanel(document.getElementById("directionsPanel"));
  geocoder = new GClientGeocoder();
}

// Try W3C Geolocation (Preferred)
  if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      map.setCenter(initialLocation);           
      document.getElementById('start').value = initialLocation;           
      }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  // Try Google Gears Geolocation
  } else if (google.gears) {
    browserSupportFlag = true;
    var geo = google.gears.factory.create('beta.geolocation');
    geo.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
      map.setCenter(initialLocation);
      document.getElementById('start').value = initialLocation;     
    }, function() {
      handleNoGeoLocation(browserSupportFlag);
    });
  // Browser doesn't support Geolocation
  } else {
    browserSupportFlag = false;
    handleNoGeolocation(browserSupportFlag);
  }
 
  function handleNoGeolocation(errorFlag) {
    if (errorFlag == true) {
      alert("Geolocation service failed.");
      initialLocation = newyork;
    } else {
      alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
      initialLocation = siberia;
    }
    map.setCenter(initialLocation);
  }


function calcRoute() {
  var start = document.getElementById("start").value;
  var end = document.getElementById("end").value;
  var request = {
    origin:start,
    destination:end,
    travelMode: google.maps.TravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
}

geoco...@gmail.com

unread,
Aug 19, 2011, 7:52:08 PM8/19/11
to Google Maps JavaScript API v3
On Aug 16, 9:48 am, gmoney <dmurphy...@gmail.com> wrote:
> Here is a link to the working code:http://punchupproductions.com/external/hotel/
>
> I;m having trouble getting the "Start" field to show the address location
> instead of the coordinates. I know I have to use reverse geocoding but I'm
> not sure how to go about it. Note: I am using the W3C geolocation to get the
> users current location. Any help is greatly appreciated.
>


Here is an example that reverse geocodes the start and end markers if
you drag them (working example of using the reverse geocoder with a
dynamic input). Probably more complicated than you need, but it does
do a reverse geocode.

-- Larry

xelawho

unread,
Aug 21, 2011, 11:02:11 PM8/21/11
to Google Maps JavaScript API v3
> I;m having trouble getting the "Start" field to show the address location
> instead of the coordinates. I know I have to use reverse geocoding but I'm
> not sure how to go about it. Note: I am using the W3C geolocation to get the
> users current location.

I just finished working on something very similar (although I put the
user's address in the infowindow). Maybe this will give you some
ideas:
http://xelawho.com/map/selvatica.htm

jean-baptiste demonte

unread,
Aug 22, 2011, 7:03:03 AM8/22/11
to google-map...@googlegroups.com
Hi,

i saw that you use jQuery on your main page .... so you can use gmap3 for jquery, it's easier :

http://gmap3.net/api/geo-latlng.html
http://gmap3.net/api/get-address.html

JB

xelawho

unread,
Aug 22, 2011, 1:55:57 PM8/22/11
to Google Maps JavaScript API v3
> I;m having trouble getting the "Start" field to show the address location
> instead of the coordinates.

... of course, there's also something to be said for looking for
errors in firebug and using the v3
geocoder = new google.maps.Geocoder();
instead of the v2
geocoder = new GClientGeocoder();
Reply all
Reply to author
Forward
0 new messages