Kal*_*nin 64 javascript google-maps coordinates google-maps-static-api
我有莫斯科的城市地图.我们使用一些艺术元素修改了Google地图图像,但GPS坐标和像素之间的关系保持不变.
问题:如何将GPS坐标从我们拥有的各种数据点转换为图像中的像素坐标?
理想情况下,我可以在Javascript中执行此操作,但PHP可以.
我知道在小尺度上(例如在城市尺度上)它可以做得足够简单(有必要了解哪个地理坐标有一个图像角点,然后在轴上的图片上学习地理坐标中一个像素的"价格" OX和OY分开).
但是在大尺度(国家规模)上,一个像素的"价格"将不是常数,并且将足够强烈地变化并且不能应用上述方法.
如何解决国家规模的问题?
更新:
我不使用API谷歌地图,我只有:对象的地理坐标(它们来自谷歌地图),我仍然在我的网站上有一个简单的图片*.gif,我必须在其中绘制一个相应的地理坐标点.
fma*_*ark 70
所有这一切的关键是理解地图预测.正如其他人所指出的那样,失真的原因是球形(或更准确地说是椭球形)地球被投射到一个平面上.
为了实现您的目标,您首先必须了解有关数据的两件事:
我假设你的数据在这些坐标系中.
球形墨卡托投影定义了一个以米为单位的坐标对,用于地球表面.这意味着,对于每个纬度/经度坐标,存在匹配的仪表/仪表坐标.这使您可以使用以下过程进行转换:
为了从WGS84点到图像上的像素,现在的过程如下:
您可以像这样使用proj4js库:
// include the library
<script src="lib/proj4js-combined.js"></script> //adjust the path for your server
//or else use the compressed version
// creating source and destination Proj4js objects
// once initialized, these may be re-used as often as needed
var source = new Proj4js.Proj('EPSG:4326'); //source coordinates will be in Longitude/Latitude, WGS84
var dest = new Proj4js.Proj('EPSG:3785'); //destination coordinates in meters, global spherical mercators projection, see http://spatialreference.org/ref/epsg/3785/
// transforming point coordinates
var p = new Proj4js.Point(-76.0,45.0); //any object will do as long as it has 'x' and 'y' properties
Proj4js.transform(source, dest, p); //do the transformation. x and y are modified in place
//p.x and p.y are now EPSG:3785 in meters
Run Code Online (Sandbox Code Playgroud)
Jad*_*ias 16
您必须以您的语言实施Google Maps API投影.我有这个C#源代码:
public class GoogleMapsAPIProjection
{
private readonly double PixelTileSize = 256d;
private readonly double DegreesToRadiansRatio = 180d / Math.PI;
private readonly double RadiansToDegreesRatio = Math.PI / 180d;
private readonly PointF PixelGlobeCenter;
private readonly double XPixelsToDegreesRatio;
private readonly double YPixelsToRadiansRatio;
public GoogleMapsAPIProjection(double zoomLevel)
{
var pixelGlobeSize = this.PixelTileSize * Math.Pow(2d, zoomLevel);
this.XPixelsToDegreesRatio = pixelGlobeSize / 360d;
this.YPixelsToRadiansRatio = pixelGlobeSize / (2d * Math.PI);
var halfPixelGlobeSize = Convert.ToSingle(pixelGlobeSize / 2d);
this.PixelGlobeCenter = new PointF(
halfPixelGlobeSize, halfPixelGlobeSize);
}
public PointF FromCoordinatesToPixel(PointF coordinates)
{
var x = Math.Round(this.PixelGlobeCenter.X
+ (coordinates.X * this.XPixelsToDegreesRatio));
var f = Math.Min(
Math.Max(
Math.Sin(coordinates.Y * RadiansToDegreesRatio),
-0.9999d),
0.9999d);
var y = Math.Round(this.PixelGlobeCenter.Y + .5d *
Math.Log((1d + f) / (1d - f)) * -this.YPixelsToRadiansRatio);
return new PointF(Convert.ToSingle(x), Convert.ToSingle(y));
}
public PointF FromPixelToCoordinates(PointF pixel)
{
var longitude = (pixel.X - this.PixelGlobeCenter.X) /
this.XPixelsToDegreesRatio;
var latitude = (2 * Math.Atan(Math.Exp(
(pixel.Y - this.PixelGlobeCenter.Y) / -this.YPixelsToRadiansRatio))
- Math.PI / 2) * DegreesToRadiansRatio;
return new PointF(
Convert.ToSingle(latitude),
Convert.ToSingle(longitude));
}
}
Run Code Online (Sandbox Code Playgroud)
资源:
那么你想要获取纬度/经度坐标并找出该位置图像上的像素坐标?
主GMap2类提供与显示的地图上的像素的转换和纬度/经度坐标:
Gmap2.fromLatLngToContainerPixel(latlng)
Run Code Online (Sandbox Code Playgroud)
例如:
var gmap2 = new GMap2(document.getElementById("map_canvas"));
var geocoder = new GClientGeocoder();
geocoder.getLatLng( "1600 Pennsylvania Avenue NW Washington, D.C. 20500",
function( latlng ) {
var pixel_coords = gmap2.fromLatLngToContainerPixel(latlng);
window.alert( "The White House is at pixel coordinates (" +
pixel_coodrs.x + ", " + pixel_coords.y + ") on the " +
"map image shown on this page." );
}
);
Run Code Online (Sandbox Code Playgroud)
因此,假设您的地图图像是Google地图显示的屏幕抓取,那么这将为您提供纬度/经度坐标图像上的正确像素坐标.
如果您正在抓取平铺图像并将它们自己拼接在一起,那么事情会变得更加棘手,因为完整平铺集的区域将位于显示的地图区域之外.
在这种情况下,您需要使用左上角图像拼贴的左侧和顶部值作为fromLatLngToContainerPixel(latlng:GLatLng)给出的坐标的偏移量,从x坐标和顶部的坐标减去左边坐标. y坐标.因此,如果左上角的图像位于(-50,-122)(左侧,顶部),而且fromLatLngToContainerPixel()告诉您lat/long位于像素坐标(150,320),则将图像拼接在一起瓦片,坐标的真实位置是(150 - ( - 50),320 - ( - 122)),即(200,442).
也可能有类似的GMap2坐标转换功能:
GMap2.fromLatLngToDivPixel(latlng:GLatLng)
Run Code Online (Sandbox Code Playgroud)
将为您提供正确的纬度/长度到像素平移的情况 - 我没有对此进行测试,也没有从API文档中100%清楚.
有关详情,请参阅此处:http: //code.google.com/apis/maps/documentation/reference.html#GMap2.Methods.Coordinate-Transformations
您要解决的翻译与Map Projection有关,Map Projection是我们世界的球面转换为二维渲染的方式.在二维曲面上渲染世界有多种方式(投影).
如果您的地图仅使用特定投影(墨卡托很流行),您应该能够找到方程,一些示例代码和/或某些库(例如,一个墨卡托解决方案 - 将纬度/长度转换为X/Y坐标)如果不这样做,我相信你可以找到其他样品 - https://stackoverflow.com/search?q=mercator.如果你的图像不是使用墨卡托投影的地图,你'我需要确定它用于找到正确的平移方程的投影.
如果你试图支持多个地图投影(你想支持许多使用不同投影的不同地图),那么你肯定想要使用像PROJ.4这样的库,但我又不知道你会发现什么是Javascript或PHP.
归档时间: |
|
查看次数: |
95759 次 |
最近记录: |