Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript geocoding from address to latitude and longitude numbers not working

I'm using the following geocoding function to convert a textual address into latitude and longitude numbers, but it's not working right. The alert writes "undefined".

Can anyone say what's wrong?

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript">  var geocoder = new google.maps.Geocoder(); var address = "new york";  geocoder.geocode( { 'address': address}, function(results, status) {  if (status == google.maps.GeocoderStatus.OK) {     var latitude = results[0].geometry.location.latitude;     var longitude = results[0].geometry.location.longitude;     alert(latitude);     }  });  </script> 
like image 508
Ash Avatar asked May 12 '11 20:05

Ash


People also ask

How do I find the latitude and longitude of a geocoder address?

The short story is you need to do: Geocoder geocoder = new Geocoder(this, Locale. getDefault()); List<Address> addresses = geocoder. getFromLocation(lat, lng, 1);

Which is the best method to use to geocode a static list of address?

Use the Geocoding API web service. Recommend automated systems use the Geocoding API web service.

What is the difference between geocoding and reverse geocoding?

Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address.


2 Answers

Try using this instead:

var latitude = results[0].geometry.location.lat(); var longitude = results[0].geometry.location.lng(); 

It's bit hard to navigate Google's api but here is the relevant documentation.

One thing I had trouble finding was how to go in the other direction. From coordinates to an address. Here is the code I neded upp using. Please not that I also use jquery.

$.each(results[0].address_components, function(){     $("#CreateDialog").find('input[name="'+ this.types+'"]').attr('value', this.long_name); }); 

What I'm doing is to loop through all the returned address_components and test if their types match any input element names I have in a form. And if they do I set the value of the element to the address_components value.
If you're only interrested in the whole formated address then you can follow Google's example

like image 124
Skadlig Avatar answered Sep 21 '22 07:09

Skadlig


You're accessing the latitude and longitude incorrectly.

Try

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript">  var geocoder = new google.maps.Geocoder(); var address = "new york";  geocoder.geocode( { 'address': address}, function(results, status) {    if (status == google.maps.GeocoderStatus.OK) {     var latitude = results[0].geometry.location.lat();     var longitude = results[0].geometry.location.lng();     alert(latitude);   }  });  </script> 
like image 24
Dancrumb Avatar answered Sep 20 '22 07:09

Dancrumb