我正在尝试在画布中显示GPS坐标。 我在法国和美国有400个城镇。我只是想在画布上显示它们。
对于每个城镇,我都有自己的纬度和经度(使用google api:https://developers.google.com/maps/documentation/geocoding/)。 我设法使用这个帖子获得了第一个结果:Converting from longitude\latitude to Cartesian coordinates
这是我的结果:
我的代码如下(假设“_liste”包含Towns列表):
private void Button_visualise_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
List<Town> _list = LibDAO.Towns_DAO.GetAllTown().Where(u=>u.IdPays == 2 || u.IdPays == 1).ToList<Town>();
this.my_canvas.Children.Clear();
double min_x = 0;
double max_x = 0;
double min_y = 0;
double max_y = 0;
for (int i = 0; i < _list .Count; i++)
{
Ellipse ell = new Ellipse() { Width = 30, Height = 30, Fill = Brushes.Blue };
Point p = ToCanvas(_list [i].Latitude, _list [i].Longitude);
Canvas.SetLeft(ell, p.X);
Canvas.SetTop(ell, p.Y);
if (p.X < min_x) min_x = p.X;
if (p.X > max_x) max_x = p.X;
if (p.Y < min_y) min_y = p.Y;
if (p.Y > max_y) max_y = p.Y;
this.my_canvas.Children.Add(ell);
}
SetCoordinateSystem(this.my_canvas, min_x, max_x, min_y, max_y);
watch.Stop();
}
public static Canvas SetCoordinateSystem(Canvas canvas, Double xMin, Double xMax, Double yMin, Double yMax)
{
var width = xMax - xMin;
var height = yMax - yMin;
var translateX = -xMin;
var translateY = height + yMin;
var group = new TransformGroup();
group.Children.Add(new TranslateTransform(translateX, -translateY));
group.Children.Add(new ScaleTransform(canvas.ActualWidth / width, canvas.ActualHeight / -height));
canvas.RenderTransform = group;
return canvas;
}
private const int earthRadius = 6367;
private Point ToCanvas(double lat, double lon)
{
lon = ConvertToRadians(lon);
lat = ConvertToRadians(lat);
double x = earthRadius * Math.Cos(lat) * Math.Cos(lon); //((lon * my_canvas.ActualWidth) / 360.0) - 180.0; ;// //
double y = earthRadius * Math.Cos(lat) * Math.Sin(lon);// ((lat * my_canvas.ActualHeight) / 180.0) - 90.0;
return new Point(x, y);
}
public double ConvertToRadians(double angle)
{
return (Math.PI / 180) * angle;
}
正如你在我的结果中所看到的那样,它几乎是完美的,我们可以识别这两个国家,但为什么他们在错误的地方呢? (我想让他们这样:http://geology.com/world/world-map.gif
我错过了什么吗?
根据此链接,我的结果可能会显示为“Azimuthal(投影到飞机上)”,而我更喜欢这样:http://en.wikipedia.org/wiki/Equirectangular_projection 所以我想改变我的功能“ToCanvas”,但我不知道组件“标准平行线(赤道的北部和南部),投影的比例是真的;”
红利问题:在GPS坐标系中获取国界边界的最佳方法是什么?我想绘制美国的边界,所以我猜我可以获得边界的坐标来绘制国家。 我试过几个网站,设法从那里获得一个.shp文件:http://www.naturalearthdata.com/downloads/110m-cultural-vectors/,但我没有设法从那里检索边框坐标。
谢谢
编辑使用此代码看起来更好:
private Point ToCanvas(double lat, double lon)
{
// Equirectangular projection
double x1 = lon * Math.Cos(ConvertToRadians(lat));
double y1 = lat;
//lon = ConvertToRadians(lon);
//lat = ConvertToRadians(lat);
//double x = earthRadius * Math.Cos(lat) * Math.Cos(lon); //((lon * my_canvas.ActualWidth) / 360.0) - 180.0; ;// //
//double y = earthRadius * Math.Cos(lat) * Math.Sin(lon);// ((lat * my_canvas.ActualHeight) / 180.0) - 90.0;
return new Point(x1 * 10, y1 * 10);
}