如何获得google静态地图的界限? [英] How to get bounds of a google static map?

查看:138
本文介绍了如何获得google静态地图的界限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得已经返回的谷歌静态地图的度数,例如,以下请求

How to get bounds in degrees of google static map which has been returned, for example, for following request

http://maps.googleapis.com/maps/api/staticmap?center=0.0,0.0&zoom=10&size=640x640&sensor=false

我知道,全地形地图是256x256的图像。这意味着n个垂直像素包含x度,但n个水平像素包含2x度。对吗?

As I know, full Earth map is 256x256 image. This means that n vertical pixels contain x degrees, but n horizontal pixels contain 2x degrees. Right?

如谷歌
中心定义地图的中心,与地图的所有边缘等距。当我明白像素(或以度为单位)等距时。并且每个后续的缩放级别使水平和垂直尺寸的精度都加倍。
所以,我可以找到每个缩放值的地图经度的delta值为:

As google says center defines the center of the map, equidistant from all edges of the map. As I understood equidistant in pixels (or in degrees?). And each succeeding zoom level doubles the precision in both horizontal and vertical dimensions. So, I can find delta value of Longitude of map for each zoom value as:

dLongitude = (HorizontalMapSizeInPixels / 256 ) * ( 360 / pow(2, zoom) );

纬度相同的计算:

dLatitude = (VerticalMapSizeInPixels / 256 ) * ( 180 / pow(2, zoom) );

VerticalMapSizeInPixels和Horizo​​ntalMapSizeInPixels是URL中地图大小的参数。

VerticalMapSizeInPixels and HorizontalMapSizeInPixels are parameters of map size in URL.

计算经度的delta值很好,但对于Latitude来说,这是错误的。我找不到Latitude的delta值,有一些 错误。

It's good to calculate delta value of Longitude, but for Latitude it is wrong. I cannot find delta value of Latitude, there is some delta error.

推荐答案


如我所知,全地形地图是256x256的图像。

As I know, full Earth map is 256x256 image.

是的。


这意味着n个垂直像素包含x度,但n个水平
像素包含2x度。对吗?

This means that n vertical pixels contain x degrees, but n horizontal pixels contain 2x degrees. Right?

否。一个像素将代表不同的纬度,取决于纬度。赤道上的一个像素比极点附近的一个像素的纬度低。

No. One pixel will represent varying amounts of latitude depending on the latitude. One pixel at the Equator represents less latitude than one pixel near the poles.

地图的角落将取决于中心,缩放级别和地图大小,你会需要使用墨卡托投影来计算它们。
如果你不想加载完整的API,这里是一个MercatorProjection对象:

The corners of the map will depend on center, zoom level and map size, and you'd need to use the Mercator projection to calculate them. If you don't want to load the full API, here's a MercatorProjection object:

var MERCATOR_RANGE = 256;

function bound(value, opt_min, opt_max) {
  if (opt_min != null) value = Math.max(value, opt_min);
  if (opt_max != null) value = Math.min(value, opt_max);
  return value;
}

function degreesToRadians(deg) {
  return deg * (Math.PI / 180);
}

function radiansToDegrees(rad) {
  return rad / (Math.PI / 180);
}

function MercatorProjection() {
  this.pixelOrigin_ = new google.maps.Point( MERCATOR_RANGE / 2, MERCATOR_RANGE / 2);
  this.pixelsPerLonDegree_ = MERCATOR_RANGE / 360;
  this.pixelsPerLonRadian_ = MERCATOR_RANGE / (2 * Math.PI);
};

MercatorProjection.prototype.fromLatLngToPoint = function(latLng, opt_point) {
  var me = this;

  var point = opt_point || new google.maps.Point(0, 0);

  var origin = me.pixelOrigin_;
  point.x = origin.x + latLng.lng() * me.pixelsPerLonDegree_;
  // NOTE(appleton): Truncating to 0.9999 effectively limits latitude to
  // 89.189.  This is about a third of a tile past the edge of the world tile.
  var siny = bound(Math.sin(degreesToRadians(latLng.lat())), -0.9999, 0.9999);
  point.y = origin.y + 0.5 * Math.log((1 + siny) / (1 - siny)) * -me.pixelsPerLonRadian_;
  return point;
};

MercatorProjection.prototype.fromPointToLatLng = function(point) {
  var me = this;

  var origin = me.pixelOrigin_;
  var lng = (point.x - origin.x) / me.pixelsPerLonDegree_;
  var latRadians = (point.y - origin.y) / -me.pixelsPerLonRadian_;
  var lat = radiansToDegrees(2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2);
  return new google.maps.LatLng(lat, lng);
};

//pixelCoordinate = worldCoordinate * Math.pow(2,zoomLevel)

您可以将其保存到单独的文件中,例如MercatorProjection.js,然后将其包含在应用程序中。

You can save that to a separate file, for example "MercatorProjection.js", and then include it in your application.

<script src="MercatorProjection.js"></script>

加载上述文件后,以下函数计算给定的地图的SW和NE角

With the above file loaded, the following function calculates the SW and NE corners of a map of a given size and at a given zoom.

function getCorners(center,zoom,mapWidth,mapHeight){
    var scale = Math.pow(2,zoom);
    var centerPx = proj.fromLatLngToPoint(center);
    var SWPoint = {x: (centerPx.x -(mapWidth/2)/ scale) , y: (centerPx.y + (mapHeight/2)/ scale)};
    var SWLatLon = proj.fromPointToLatLng(SWPoint);
    alert('SW: ' + SWLatLon);
    var NEPoint = {x: (centerPx.x +(mapWidth/2)/ scale) , y: (centerPx.y - (mapHeight/2)/ scale)};
    var NELatLon = proj.fromPointToLatLng(NEPoint);
    alert(' NE: '+ NELatLon);
}

您可以这样调用:

var proj = new MercatorProjection();
var G = google.maps;
var centerPoint = new G.LatLng(49.141404, -121.960988);
var zoom = 10;
getCorners(centerPoint,zoom,640,640);

这篇关于如何获得google静态地图的界限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆