Say I have a given a link to a map with a location marker , how can I find the latitude and longitude from that map marker using JavaScript and the Google API?
The trick is that I don't know the address. Is there a way to find out what the address on the page is elegantly, without having to use regular expressions on the HTML?
Specifically, I want to extract the address and latitude/longitude out of the given marker.
That's 'easy'. Google Maps api has a geolocation object, that is the same used by google maps. So you should use something like that:
var geocodeLocation = 'String of the adress you''re looking for';
var geocoder = new google.maps.ClientGeocoder();
geocoder.getLatLng(geocodeLocation, function(point) {
if (point) {
//Do something with point - //You have point.x and point.y
}
});
The problem of finding the address given the URL is actually way easier than I thought. In my case, the Google Maps URLs I am given are of the form:
https://maps.google.com/?q=loc:+ADDRESS+CITY+STATE+COUNTRY
So, I quickly get the address from the URL directly.
I don't know if that's the default for google maps URLS. If not, then things would be more difficult.
From here, it's a simple use of the geocoder, of which there are plenty of examples on the Google Maps API page .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.