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

发布于 2024-11-24 08:42:01 字数 162 浏览 0 评论 0原文

我在工作环境中为客户使用静态地图。我喜欢 17 的缩放级别(0 表示整个地球,21 表示最接近的缩放),但根据图像位置的来源,Google 可能无法提供缩放级别 17 的图像。是一种方法吗? ,告诉 Maps API,动态选择可能的最高缩放,例如 17?我还没有找到一种方法来做到这一点,但我非常感谢任何建议。

I use Static maps in my work inviroment for customers. 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? I have not seen a way to do this, but i sure appreciate any suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆夏时光 2024-12-01 08:42:02

据我所知,静态地图必须在网址的查询字符串中设置缩放,例如

&zoom=17&z=17

如果您如果使用非静态地图,您可以轻松找到和/或设置最大缩放:

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

鉴于您使用的是静态,我认为您有两个选择:

  1. 不要在 url 查询上设置缩放,并让 Google 地图确定“最佳”缩放级别。
  2. 将缩放设置为 17 或更小的缩放(14 或 15),然后允许用户使用使用不同 URL 字符串刷新地图的链接进行放大。 (您需要使用 JavaScript 来完成此操作)

编辑

这是实现缩放 js 的一种方法。 (我很感兴趣,并将其一起破解。我确信有更好/更优雅的方法)

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" />

确保缩放级别位于查询字符串的末尾。

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/

As far as I know, static maps must have the zoom set in the query string of the url, something like

&zoom=17 or &z=17

If you were using a non-static map, you could easily find and/or set the max zoom:

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

Given that you are using static though, I think you have two options:

  1. do not set a zoom on the url query and let Google Maps determine the "best" zoom level.
  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. (You need to do this with javascript)

EDIT

Here's one way to implement the js for the zoom. (I was interested and hacked this together. I am sure there is a better/more elegant way)

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

$('#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/

枫林﹌晚霞¤ 2024-12-01 08:42:02

首先,我要感谢 Jason Gennaro 提出了一个令人惊叹的 JS 替代方案!不幸的是,这个想法假设您在每个地图上设置了“缩放”级别。如果像我的情况一样,我让谷歌(原文如此)本身调整地图大小,我就会对 Jason 的代码进行修改/添加。如果您想使用他的想法,但希望地图在 2 个位置周围正确调整大小,使用每个位置的纬度/经度,使用下面的代码计算距离,您可以估计要添加到字符串的缩放级别。这些距离是通过大量测试来估计的。我已经安装并测试了这段代码。作品完美。享受。

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

调用此函数...

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

ps 将 $td2 添加到原始地图字符串的末尾。请原谅变量名称。有时它们是有道理的:)这就是 PHP。

First I want to THANK Jason Gennaro for an amazing idea with his JS alternative! Unfortunately, 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, 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, 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...

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

p.s. Add $td2 to the end of your original map string. Pardon the variable names. Sometimes they make sense :) This is PHP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文