Google maps link from greasemonkey default zoom

房东的猫 提交于 2019-12-11 10:45:01

问题


Something seems to have changed on google maps recently that changed the default zoom. I've tried adding zoom=17 and z=17, but nothing seems to change.

I'm just creating a hyperlink on page using the following url & query

"https://maps.google.com/?zoom=17&q=" +myAddress.replace(/ /g, '+');

In the past it's always worked fine no matter what address I used in the query. Just wondering what may have changed and if I need to work around this another way.


回答1:


Google maps, now, will not zoom past z=13 unless Latitude and Longitude are also supplied. EG: &ll=98.414257,-21.727585.

As a scripter, this makes your task a bit more involved; here is a pseudocode approach that will work:

  1. Open the map page as before, but flag it (custom URL param or GM_setValue) as first opened by your script.

  2. Set the script to also run on Google maps pages.

  3. On flagged Google maps pages:

    1. Grab the share link.
    2. Change the z parameter only, EG &z=17.
    3. Make sure any flag is cleared.
    4. location.replace() to the modified share link


Alternatively, a more durable strategy is to switch to using the Google Maps API.




回答2:


If you access Google Maps and click and share link button you will see something like this:

https://maps.google.com/?ll=53.944771,-0.854187&spn=0.890705,2.694397&t=m&z=9

So i think you should worry about some required field...




回答3:


Here's what I ended up using to solve the problem. I can specify zoom by z=16 provided latitude and longitude as Brock explained.

addMapLL('900+w+900+s+clearfield+UT');  // just a random address formatted

function addMapLL(address) {
    var api = "https://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=" + address;

     GM_xmlhttpRequest(
    {
        method: 'GET',
        url: api,
        onload: function(resp) {
            var obj = JSON.parse(resp.responseText);

            if (obj.status == "OK") {
                var lat = obj.results[0].geometry.location.lat;
                var lng = obj.results[0].geometry.location.lng;
                var ll  = "&ll=" + lat + "," + lng;

                // .href is already set as follows
                // https://maps.google.com/?z=16&q=900+w+900+s+clearfield+UT
                document.getElementById('googlemap').href += ll;
            }
        }
    });
}


来源:https://stackoverflow.com/questions/15842315/google-maps-link-from-greasemonkey-default-zoom

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