Reputation: 215
How retrieve latitude and longitude via Google Maps API?
Upvotes: 18
Views: 49952
Reputation: 248
I have found the result via the following method:
http://maps.googleapis.com/maps/api/geocode/json?address=woking&sensor=false
console.log(response.results[0].geometry.viewport.northeast.lat);
console.log(response.results[0].geometry.viewport.northeast.lng);
Upvotes: 4
Reputation: 21
How about ? Working fine ...
var map = new google.maps.Map(....);
var longitude = map.center.lng();
var latitude = map.center.lat();
or
var longitude = map.getCenter().lng();
var latitude = map.getCenter().lat();
Upvotes: 0
Reputation: 63
To add to RedBlueThing's answer you can do stuff like:
geocoded_by :address
def address
[street, city, state, country].compact.join(', ')
end
to help you narrow down the exact location, otherwise if you just send in the street the geocoder gem just returns the first instance of that street it finds which could be anywhere in the world
Upvotes: 0
Reputation: 463
With API v3
google.maps.event.addListener(map, 'click', function(event) {
alert( 'Lat: ' + event.latLng.lat() + ' and Longitude is: ' + event.latLng.lng() );
});
Likewise you can retrieve when a marker is dragged dropped :
google.maps.event.addListener(marker, 'dragstart', function(event) {
alert( 'Lat: ' + event.latLng.lat() + ' and Longitude is: ' + event.latLng.lng() );
});
Upvotes: 24
Reputation: 11493
more info here https://developers.google.com/maps/documentation/geocoding/
Upvotes: 1
Reputation: 4650
I am not sure if you are requesting a way of getting latitude and longitude via code(although you did say API :) ), If you are then there are a million posts out there which will show you how(including the ones above), but if you are interested in some kinda utility which just tells you where you are clicking , then Google-maps already comes with it. check out his post here on how to enable that functionality.
http://varunpant.com/posts/find-longitude-and-latitude-in-google-maps
Upvotes: 0
Reputation: 21794
http://code.google.com/apis/maps/documentation/services.html#Geocoding
Upvotes: 0
Reputation: 42532
If you need to find out the latitude and longitude of an address using the Google Maps API, you need to use the Google Maps Geocoding Service:
var map = new GMap2(document.getElementById("map_canvas"));
var geocoder = new GClientGeocoder();
var address = "1600 Amphitheatre Parkway, Mountain View";
geocoder.getLatLng(address, function(point) {
var latitude = point.y;
var longitude = point.x;
// do something with the lat lng
});
If you would like to get the latitude and longitude of a position clicked on your Google map, you can do this in the click event:
GEvent.addListener(map, "click", function(marker,point) {
var latitude = point.y;
var longitude = point.x;
// do something with the lat/lng
});
Upvotes: 14