简体   繁体   中英

Given a link with a location “loc” search extract latitude and longitude from marker using Google Maps API

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
    }
  });

instead of using the Google Maps API you can just use the geocoding web service directly and get a response in JSON or XML

Documentation here .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM