使用谷歌地图时,将GPS坐标映射到屏幕位置的好算法是什么?

时间:2022-12-03 13:03:08

I need an algorithm that will convert a GPS coordinate to a screen location on a displayed google map. I would think this would be simple- get the coordinates for the four corners of the displayed map, find the differential and create a scaling factor for a pixel location on the screen. Is this correct or am I missing something. I'm know this has been done ad nauseum but I am hoping I can hear from someone who has implemented it successfully or has a resource for implementing it.

我需要一种算法,将GPS坐标转换为显示的谷歌地图上的屏幕位置。我认为这很简单 - 获取显示地图四个角的坐标,找到差分并为屏幕上的像素位置创建缩放系数。这是正确的还是我错过了什么。我知道这已经做了令人作呕但我希望我能听到成功实施它或有实施它的资源的人。

4 个解决方案

#1


Basically you need the code for Transverse Mercator projection (which is used by Google maps and others). Here's a C# snippet I used my Kosmos software:

基本上你需要横向墨卡托投影的代码(由谷歌地图和其他人使用)。这是我用过Kosmos软件的C#片段:

public Point2<int> ConvertMapToViewCoords (Point2<double> mapCoords)
{
    double x = (mapCoords.X - MapPosition.Longitude) / resolution;
    double y = Math.Log (Math.Tan (Math.PI*(0.25 + mapCoords.Y/360)))*u180dPiResolution;

    return new Point2<int> ((int)(x + viewWidthHalf), (int)((y0 - y) + viewHeightHalf));
}

variables used:

double resolution = 360.0 / (Math.Pow (2, MapPosition.ZoomFactor) * 256);
double u180dPiResolution = 40.7436654315252 * Math.Pow(2, MapPosition.ZoomFactor);
double y0 = Math.Log(Math.Tan(Math.PI * (0.25 + MapPosition.Latitude / 360))) * u180dPiResolution;
float viewWidthHalf = ViewWidth / 2.0f;
float viewHeightHalf = ViewHeight / 2.0f;

ZoomFactor is Google zoom level (see http://laudontech.com/GISBlog/?p=28). BTW the same code works for OpenStreetMap, Yahoo Maps etc., since they all use the same projection and tiling system.

ZoomFactor是Google缩放级别(请参阅http://laudontech.com/GISBlog/?p=28)。 BTW相同的代码适用于OpenStreetMap,Yahoo Maps等,因为它们都使用相同的投影和平铺系统。

#2


The Google Maps API lets you do stuff like this.

Google Maps API可让您执行此类操作。

Here is some JS code I've written using the APIs that does something similar:

这里有一些我用类似的API编写的JS代码:

var map = new GMap2(document.getElementById("map"));

//...

var location = new GLatLng(37.771008, -122.41175);
map.setCenter(location);

var marker = new GMarker(location);
var overlay_caption = "Our location!";

GEvent.addListener(marker, "click", function() {
  marker.openInfoWindowHtml(overlay_caption);
});

map.addOverlay(marker);
marker.openInfoWindowHtml(overlay_caption);

#3


You can also redirect the page to a new map with a URL like this:

您还可以使用以下URL将页面重定向到新地图:

http://maps.google.com/maps?q=37.771008,+-122.41175+(You+can+insert+your+text+here)&iwloc=A&hl=en

#4


If you need the pixel coordinate of a latitude/longitude position of a current instance of Google Maps you may use the fromLatLngToDivPixel() function.

如果您需要Google地图当前实例的纬度/经度位置的像素坐标,则可以使用fromLatLngToDivPixel()函数。

Assuming map is an instance of an initialized GMap2:

假设map是初始化GMap2的实例:

var location = new GLatLng(37.771008, -122.41175);
var point = map.fromLatLngToDivPixel(location);

alert("X: " + point.x + ", Y: " + point.y);

Depending on your needs, see also fromLatLngToContainerPixel.

根据您的需要,另请参阅fromLatLngToContainerPixel。

#1


Basically you need the code for Transverse Mercator projection (which is used by Google maps and others). Here's a C# snippet I used my Kosmos software:

基本上你需要横向墨卡托投影的代码(由谷歌地图和其他人使用)。这是我用过Kosmos软件的C#片段:

public Point2<int> ConvertMapToViewCoords (Point2<double> mapCoords)
{
    double x = (mapCoords.X - MapPosition.Longitude) / resolution;
    double y = Math.Log (Math.Tan (Math.PI*(0.25 + mapCoords.Y/360)))*u180dPiResolution;

    return new Point2<int> ((int)(x + viewWidthHalf), (int)((y0 - y) + viewHeightHalf));
}

variables used:

double resolution = 360.0 / (Math.Pow (2, MapPosition.ZoomFactor) * 256);
double u180dPiResolution = 40.7436654315252 * Math.Pow(2, MapPosition.ZoomFactor);
double y0 = Math.Log(Math.Tan(Math.PI * (0.25 + MapPosition.Latitude / 360))) * u180dPiResolution;
float viewWidthHalf = ViewWidth / 2.0f;
float viewHeightHalf = ViewHeight / 2.0f;

ZoomFactor is Google zoom level (see http://laudontech.com/GISBlog/?p=28). BTW the same code works for OpenStreetMap, Yahoo Maps etc., since they all use the same projection and tiling system.

ZoomFactor是Google缩放级别(请参阅http://laudontech.com/GISBlog/?p=28)。 BTW相同的代码适用于OpenStreetMap,Yahoo Maps等,因为它们都使用相同的投影和平铺系统。

#2


The Google Maps API lets you do stuff like this.

Google Maps API可让您执行此类操作。

Here is some JS code I've written using the APIs that does something similar:

这里有一些我用类似的API编写的JS代码:

var map = new GMap2(document.getElementById("map"));

//...

var location = new GLatLng(37.771008, -122.41175);
map.setCenter(location);

var marker = new GMarker(location);
var overlay_caption = "Our location!";

GEvent.addListener(marker, "click", function() {
  marker.openInfoWindowHtml(overlay_caption);
});

map.addOverlay(marker);
marker.openInfoWindowHtml(overlay_caption);

#3


You can also redirect the page to a new map with a URL like this:

您还可以使用以下URL将页面重定向到新地图:

http://maps.google.com/maps?q=37.771008,+-122.41175+(You+can+insert+your+text+here)&iwloc=A&hl=en

#4


If you need the pixel coordinate of a latitude/longitude position of a current instance of Google Maps you may use the fromLatLngToDivPixel() function.

如果您需要Google地图当前实例的纬度/经度位置的像素坐标,则可以使用fromLatLngToDivPixel()函数。

Assuming map is an instance of an initialized GMap2:

假设map是初始化GMap2的实例:

var location = new GLatLng(37.771008, -122.41175);
var point = map.fromLatLngToDivPixel(location);

alert("X: " + point.x + ", Y: " + point.y);

Depending on your needs, see also fromLatLngToContainerPixel.

根据您的需要,另请参阅fromLatLngToContainerPixel。