[英]Google maps static pixels to latitude longitude relative to zoom level
因此,我使用google maps static api v2从lat,lng @ zoom 9(例如)获取256x256图像。 我对图像像素进行了一些计算,得到了x,y点的数组。 为此,我想将图层绘制为折线。
我想将x,y像素点的数组转换为lat,lng。
我的伪代码是这样的:
var lat = 35.000,lng=33.000;
var TILE_SIZE = 256;
// google code
function degreesToRadians(deg) {
return deg * (Math.PI / 180);
}
function radiansToDegrees(rad) {
return rad / (Math.PI / 180);
}
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;
}
/** @constructor */
function MercatorProjection() {
this.pixelOrigin_ = new google.maps.Point(TILE_SIZE / 2,
TILE_SIZE / 2);
this.pixelsPerLonDegree_ = TILE_SIZE / 360;
this.pixelsPerLonRadian_ = TILE_SIZE / (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_;
// 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);
};
// google code
getLatLngFromXY = function(x,y,zoom){
var scale = Math.pow(2,zoom );
var projection = new MercatorProjection();
var worldCoordinateCenter = projection.fromLatLngToPoint(new google.maps.LatLng(lat,lng));
var pixelOffset = new google.maps.Point((x/scale) || 0,(y/scale) ||0);
var offest_x = (x > 128) ? worldCoordinateCenter.x + pixelOffset.x: worldCoordinateCenter.x - pixelOffset.x;
var offest_y = (y > 128) ? worldCoordinateCenter.y + pixelOffset.y :
worldCoordinateCenter.y - pixelOffset.y;
var worldCoordinateNewCenter = new google.maps.Point(
offest_x,
offest_y
);
var newCenter = projection.fromPointToLatLng(worldCoordinateNewCenter);
var ideal_lat = newCenter.lat(),
ideal_lng = newCenter.lng();
return {lat: ideal_lat, lng: ideal_lng};
}
var some_list = [{x: 129, y: 124},{x: 118, y: 52},...],
polyline = [];
for(var i=0; i < some_list.length; i++){
temp_latlng = getLatLngFromXY(some_list[i].x,some_list[i].y,9);
polyline.push(new google.maps.LatLng(temp_latlng.lat,temp_latlng.lng);
}
new google.maps.Polyline({
map: map,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 10,
path: polyline
});
我希望这是有道理的。 问题是:因为我要偏移(我猜),所以我到处都是标记,这与静态图像上的外观不同。 所以我所做的pixels-> mercator之间的转换是错误的。
谢谢
我能够解决这个问题。。。很难理解什么地方错了,需要做什么,基本上我们想成为ZOOM依赖者(上面的比例尺)
这意味着我们的代码应更改(“ Google代码”部分),如下所示:
MercatorProjection.prototype.fromLatLngToPoint = function(latLng, zoom, 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_;
// 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_;
var tiles_no = 1 << zoom;
point.x *= tiles_no;
point.y *= tiles_no;
return point;
};
MercatorProjection.prototype.fromPointToLatLng = function(point, zoom) {
var me = this;
var origin = me.pixelOrigin_;
var tiles_no = 1 << zoom;
point.x /= tiles_no;
point.y /= tiles_no;
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);
};
然后,当我们想从lat,lng转换为point时:
var worldCoordinateCenter = projection.fromLatLngToPoint(new google.maps.LatLng(lat,lng),zoom);
and also the offset changes (just for comfort and logic, its almost the same)
var offset_x = (x > (TILE_SIZE / 2)) ? x - (TILE_SIZE / 2): -Math.abs(x - (TILE_SIZE / 2));
var offset_y = (y > (TILE_SIZE / 2)) ? y - (TILE_SIZE / 2): -Math.abs(y - (TILE_SIZE / 2));
var newCenter = projection.fromPointToLatLng({x: worldCoordinateCenter.x + offset_x,y:worldCoordinateCenter.y + offset_y },zoom);
而已。
注意“ 1 << zoom”就像Math.pow(2,zoom);一样。 并且在fromLatLngToPoint中,我们乘以它(它是每个缩放级别的图块数);在fromPointToLatLng中,我们除以它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.