问题描述
如何获取已返回的谷歌静态地图度数的边界,例如,用于以下请求
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?
正如 google 所说center 定义地图的中心,与地图的所有边缘等距.正如我所理解的等距像素(或度数?).每个后续的缩放级别都会使水平和垂直维度的精度加倍.所以,我可以找到每个缩放值的地图经度的增量值:
As google sayscenter 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 和 HorizontalMapSizeInPixels 是 URL 中地图大小的参数.
VerticalMapSizeInPixels and HorizontalMapSizeInPixels are parameters of map size in URL.
计算经度的增量值很好,但对于纬度来说是错误的.我找不到 Latitude 的 delta 值,存在一些 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.
推荐答案
是的.
这意味着 n 个垂直像素包含 x 度,但 n 个水平像素像素包含 2x 度.对吗?
没有.一个像素将代表不同数量的纬度,具体取决于纬度.赤道上的一个像素比两极附近的一个像素代表的纬度要小.
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>
加载上述文件后,以下函数计算给定大小和给定缩放比例的地图的西南角和东北角.
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);
这篇关于如何获得谷歌静态地图的边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!