How to Obtain Lat/Lng of Google Maps Query String?

大憨熊 提交于 2019-12-11 03:15:06

问题


I have this link:

http://www.google.com/maps?cid=0,0,612446611849848549&f=q&source=embed&hl=en&geocode=&q=Универзална+Сала+&sll=,&&ie=UTF8&hq=&hnear=Универзална+Сала+&ll=,&z=15&iwloc=near

What I want is to retrieve the Lat Lng of the pinpointed place. I have already tried to use the geocoding API:

http://maps.googleapis.com/maps/api/geocode/xml?q=Универзална+Сала+&sensor=false

but I am getting no results because the pinpoint refers to a place and not an address.

How do I obtain the Lat Lng of the pinpoint based on the link? (I need this through code, since I am scraping a site)


回答1:


You can get Lat Lng information from the response for this link. The response of this link would be an html file of google map. The position information is written somewhere in the javascript code inside the html. You can find the pattern "viewport:{center:{lat:xxx,lng:xxx}}" in the javascript code. For instance I found the lat and lng of your link is 41.999227 and 21.416496, which is the center of the map as well as the pinpointed place. Or you can find the markers position if you have more than one result:

codes for map center information:

viewport: {
    center: {
       lat: 41.999227,
       lng: 21.416496
    },
    span: {
        lat: 0.006295,
        lng: 0.006295
    },
    zoom: 15,
    mapType: 'm',
    source: 0
},
...

codes for the markers:

markers: [{
    id: 'A',
    cid: '612446611849848549',
    latlng: {
        lat: 41.999227,
        lng: 21.416496
    },
    ... 

In this case, the center of map and the marker has the same latlng information.



来源:https://stackoverflow.com/questions/6940131/how-to-obtain-lat-lng-of-google-maps-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!