I'm trying to get longitude and latitude values from the google maps api with an address, however it doesn't seem to be returning any value. Is there a problem with my syntax?
var point = GClientGeocoder.getLatLng('1600 Pennsylvania Avenue NW Washington, DC 20500');
alert(point);
The short story is you need to do: Geocoder geocoder = new Geocoder(this, Locale. getDefault()); List<Address> addresses = geocoder. getFromLocation(lat, lng, 1);
Getting latitude and longitude Wen the user has selected a place we can get the latitude and longitude by calling lat() and lng() on the place object.
It seems like you're using v2 of Google Maps; if you're interested in using v3 (since Google has officially deprecated version 2), you could do something like this:
var mygc = new google.maps.Geocoder();
mygc.geocode({'address' : '1600 Pennsylvania Avenue NW Washington, DC 20500'}, function(results, status){
console.log( "latitude : " + results[0].geometry.location.lat() );
console.log( "longitude : " + results[0].geometry.location.lng() );
});
It's similar to the other examples except:
The Google API docs have more detail for v3. Cheers!
This works (provided you have the correct API key, subject to the 2500 requests/day rate limit):
address_string = '1600 Pennsylvania Avenue NW Washington, DC 20500';
geocoder = new GClientGeocoder();
geocoder.getLatLng(
address_string,
function(point) {
if (point !== null) {
alert(point); // or do whatever else with it
} else {
// error - not found, over limit, etc.
}
}
);
You seem to be calling a function of GClientGeocoder
, where you need to create a new GClientGeocoder()
and call its function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With