【发布时间】:2021-03-15 16:47:57
【问题描述】:
这与我的问题 here 有点相似/相关,但不同之处足以保证一个单独的问题。
当以编程方式将 GPS 坐标数组添加到 Bing 地图时,我想将地图的中心设置为数组中所有 GPS 坐标之间的中点。例如,如果我想展示 Hannibal、Independence、Columbia、Jefferson City 和 St. Louis(都在密苏里州),那么中心应该在哥伦比亚附近。
我想一种方法可能是添加所有纬度并除以标记的位置数(在上述情况下为 5),然后添加所有经度并除以该数字。这两个平均值可以作为中心。
这是计算中心坐标的明智方法,还是有更好的方法?
我想在代码隐藏(在 C# 中)中执行此操作。
更新
这是我对(伪)代码可能是什么的想法:
int locationCount = GetNumberOfLocationsOnMap(currentMap);
double totalLatitude = GetTotalLatitudesOfLocationsOnMap(currentMap);
double totalLongitude = GetTotalLongitudesOfLocationsOnMap(currentMap);
double avgLatitude = totalLatitude div locationCount;
double avgLongitude = totalLongitude div locationCount;
map.Center = avgLatitude, avgLongitude;
如果有更简单和/或更好的方法,我想知道。
更新 2
这行得通,只有一个位置(基于 Duncan Lawler 的回答和链接):
private void Page_Loaded(object sender, RoutedEventArgs e)
{
. . .
SetInitialScene();
}
private async void SetInitialScene()
{
// Set it to Monterey, California
BasicGeoposition location = new BasicGeoposition();
location.Latitude = 36.6002;
location.Longitude = -121.8947;
Geopoint geop = new Geopoint(location);
await map.TrySetSceneAsync(MapScene.CreateFromLocation(geop));
}
【问题讨论】:
标签: c# uwp gps bing-maps centering