简体   繁体   English

有没有办法根据图像的可用性设置 Google Static 地图的缩放级别?

[英]Is there a way to set the zoom level of Google Static Maps, based on the availability of imagery?

I use Static maps in my work inviroment for customers.我在工作环境中为客户使用 Static 地图。 I like the zoom level of 17, (0 for the entire earth, 21 for the closest zoom possible) but depending on where the image location is from, Google may not have the imagery available at the zoom level of 17. Is the a way, to tell the Maps API, to dynamically select the highest zoom possible, up to say 17?我喜欢 17 的缩放级别(0 表示整个地球,21 表示最接近的缩放)但根据图像位置的来源,Google 可能无法提供 17 缩放级别的图像。是一种方式,告诉地图 API,动态 select 可能的最高缩放,最多说 17? I have not seen a way to do this, but i sure appreciate any suggestions.我还没有看到这样做的方法,但我很感激任何建议。

As far as I know, static maps must have the zoom set in the query string of the url, something like据我所知,static 地图必须在 url 的查询字符串中设置缩放,类似于

&zoom=17 or &z=17 &zoom=17&z=17

If you were using a non-static map, you could easily find and/or set the max zoom:如果您使用的是非静态 map,您可以轻松找到和/或设置最大缩放:

V2: http://googlegeodevelopers.blogspot.com/2009/06/how-low-can-you-go-introducing.html V2: http://googlegeodevelopers.blogspot.com/2009/06/how-low-can-you-go-introducing.html

v3: http://code.google.com/apis/maps/documentation/javascript/services.html#MaxZoom v3: http://code.google.com/apis/maps/documentation/javascript/services.html#MaxZoom

Given that you are using static though, I think you have two options:鉴于您使用的是 static ,我认为您有两个选择:

  1. do not set a zoom on the url query and let Google Maps determine the "best" zoom level.不要在 url 查询上设置缩放,让 Google 地图确定“最佳”缩放级别。
  2. set the zoom to 17 or something even less zoomed in (14 or 15) and then allow the user to zoom in with a link that refreshes the map with a different URL string.将缩放设置为 17 或更小(14 或 15),然后允许用户使用一个链接进行放大,该链接使用不同的 URL 字符串刷新 map。 (You need to do this with javascript) (您需要使用 javascript 执行此操作)

EDIT编辑

Here's one way to implement the js for the zoom.这是实现 js 进行缩放的一种方法。 (I was interested and hacked this together. I am sure there is a better/more elegant way) (我很感兴趣并一起破解了这个。我相信有更好/更优雅的方式)

HTML HTML

<img src="http://maps.googleapis.com/maps/api/staticmap?center=55.516192,-87.39624&size=400x400&maptype=satellite&sensor=false&zoom=12" />

<input type="submit" id="in" value="Zoom in" />
<input type="submit" id="out" value="Zoom out" />

Make sure the zoom level is at the end of the query strings.确保缩放级别位于查询字符串的末尾。

JS JS

$('#in').click(function(){
    var map = $('img').attr('src');
    var zoomPat = /(.+)(zoom=)([\d]*)/;
    var zoomLevel = map.match(zoomPat);
    var zoomLevelNum = Number(zoomLevel[3]); 

    if(zoomLevelNum == 21){
       alert('Maximum level reached');
    } 
    else{    
    var newZoom = zoomLevel[1] + zoomLevel[2] + (zoomLevelNum + 1);
    $('img').attr('src', newZoom);           
    } 

});

$('#out').click(function(){
    var map = $('img').attr('src');
    var zoomPat = /(.+)(zoom=)([\d]*)/;
    var zoomLevel = map.match(zoomPat);
    var zoomLevelNum = Number(zoomLevel[3]); 

    if(zoomLevelNum == 0){
       alert('Minimum level reached');
    } 
    else{    
    var newZoom = zoomLevel[1] + zoomLevel[2] + (zoomLevelNum - 1);
    $('img').attr('src', newZoom);           
    } 

});

http://jsfiddle.net/jasongennaro/FFTKG/2/ http://jsfiddle.net/jasongennaro/FFTKG/2/

First I want to THANK Jason Gennaro for an amazing idea with his JS alternative, Unfortunately.首先,我要感谢 Jason Gennaro 用他的 JS 替代方案提出了一个惊人的想法,不幸的是。 this idea assumes you have a 'zoom' level set on each map, If, as in my case, I was letting google(sic) adjust the map size itself.这个想法假设您在每个 map 上设置了“缩放”级别,如果像我一样,我让谷歌(原文如此)调整 map 大小本身。 I came up with a modification/addition to Jason's code, If you want to use his idea, but want the maps to size properly around 2 locations, using the lat/long of each location, using the code below to calculate distance.我想出了对 Jason 代码的修改/添加,如果您想使用他的想法,但希望地图在 2 个位置周围适当调整大小,使用每个位置的纬度/经度,使用下面的代码计算距离。 you can estimate a zoom level to add to the string.您可以估计要添加到字符串的缩放级别。 These distances are somewhat estimated with a lot of testing.这些距离是通过大量测试估算出来的。 I have installed and tested this code.我已经安装并测试了这段代码。 Works perfect.完美运行。 Enjoy.享受。

function tom_distance2($lat1, $lng1, $lat2, $lng2, $miles = true)
{
  $pi80 = M_PI / 180;
  $lat1 *= $pi80;
  $lng1 *= $pi80;
  $lat2 *= $pi80;
  $lng2 *= $pi80;

  $r = 6372.797; // mean radius of Earth in km
  $dlat = $lat2 - $lat1;
  $dlng = $lng2 - $lng1;
  $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
  $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
  $km = $r * $c;
  $di9 = round(($km * 0.621371192));
  if ($di9 >= "5000") {$tom98 = "1"; // zoom level
  } else if ($di9 >= "4000") {$tom98 = "2";
  } else if ($di9 >= "1500") {$tom98 = "3";
  } else if ($di9 >= "800") {$tom98 = "4";
  } else if ($di9 >= "400") {$tom98 = "5";
  } else if ($di9 >= "180") {$tom98 = "6";
  } else if ($di9 >= "100") {$tom98 = "7";
  } else if ($di9 >= "0") {$tom98 = "10";
  } else {$tom98 = "8";} // just in case :)
  return $tom98;
}

Call this function with...将此 function 称为...

$td2 = "&amp;zoom=" . tom_distance2($tlat1, $tlong1, $tlat2, $tlong2, 1);

ps Add $td2 to the end of your original map string. ps 将 $td2 添加到原始 map 字符串的末尾。 Pardon the variable names.请原谅变量名。 Sometimes they make sense:) This is PHP.有时它们是有道理的:)这是 PHP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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