c# - 是否可以将指针坐标转换为 map 坐标(位置/地理坐标)?

标签 c# geolocation position bing-maps

我正在从 Bing map 中删除图钉以响应图钉上的 RightTapped 事件:

private void PinRightTapped(object sender, RightTappedRoutedEventArgs args)
{
    Pushpin p = sender as Pushpin;
    if (null != p)
    {
        DataLayer.Children.Remove(p); // "DataLayer" is the name of the Bing Maps MapLayer
    }
}

...但我还想从我正在管理的位置列表 (photosetLocationCollection) 中删除相应的位置。我想到了这样的事情:

private void PinRightTapped(object sender, RightTappedRoutedEventArgs args)
{
    Pushpin p = sender as Pushpin;
    if (null != p)
    {
        DataLayer.Children.Remove(p);
        // Remove it from the location list, too
        int locToRemove = PhotraxUtils.GetIndexFor(args.GetPosition());
        if (locToRemove > -1)
        {
            App.photosetLocationCollection.RemoveAt(locToRemove);
        }
    }
}

private int GetIndexFor(Location _loc)
{
    int unfound = -1;
    Location loopLoc;
    for (int i; i < App.photosetLocationCollection.Count; i++)
    {
         loopLoc = App.photosetLocationCollection[i];
         if ((loopLoc.Latitude == _loc.Latitude) && (loopLoc.Longitude == _loc.Longitude))
         {
             return i;
         }
    }
    return unfound;
}

...但是,唉,“GetPosition”不返回“世界”位置,而是返回屏幕位置,IOW 是一个点而不是一个位置(当然,当你考虑它时,这是有道理的,但我是真的)。正如雨林居民所说 here , RightTappedRoutedEventArgs 的 GetPosition 方法“返回指针的 x 和 y 坐标”:

public Point GetPosition(
  UIElement relativeTo
)

...(不是图钉的地理坐标)。

那么:有没有办法确定图钉所在的地理坐标,或者以某种方式将屏幕坐标转换为 map 坐标?

更新

根据 BMW(Bing Map Whisperer 的)回答,这应该有效:

private void PinRightTapped(object sender, RightTappedRoutedEventArgs args)
{
    Pushpin p = sender as Pushpin;
    if (null != p)
    {
        DataLayer.Children.Remove(p);
        // Remove it from the location collection, too
        int locToRemove = MapLayer.GetPosition(p);
        int locCollIndex = PhotraxUtils.GetIndexFor(locToRemove);
        if (locCollIndex > -1)
        {
            App.photosetLocationCollection.RemoveAt(locCollIndex);
        }
    }
}

我明天试试。

更新 2

使用 BMW 的想法,并根据需要更新代码,这似乎可行:

private void PinRightTapped(object sender, RightTappedRoutedEventArgs args)
{
    Pushpin p = sender as Pushpin;
    if (p != null)
    {
        DataLayer.Children.Remove(p);
        // Remove it from the location list, too
        Location locToRemove = MapLayer.GetPosition(p);
        int locCollIndex = GetIndexFor(locToRemove);
        if (locCollIndex > -1)
        {
            App.photosetLocationCollection.RemoveAt(locCollIndex);
        }
    }
}

private int GetIndexFor(Location loc)
{
    int locIndex = -1;
    for (int i = 0; i < App.photosetLocationCollection.Count; i++)
    {
        Location l = App.photosetLocationCollection[i];
        //if ((loc.Latitude == l.Latitude) && (loc.Longitude == l.Longitude)) <= which way is better, "==" or ".Equals"?
        if ((loc.Latitude.Equals(l.Latitude)) && (loc.Longitude.Equals(l.Longitude)))
        {
            locIndex = i;
            return locIndex;
        }
    }
    return locIndex;
}

最佳答案

为什么不从图钉中获取坐标信息。这是您可能在收藏中使用的坐标。尝试这样的事情:

int locToRemove = MapLayer.GetPosition(p);
DataLayer.Children.Remove(p);

关于c# - 是否可以将指针坐标转换为 map 坐标(位置/地理坐标)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27025295/

相关文章:

c# - 为什么相同的代码大小会产生不同大小的exe文件

具有方法参数值或使用委托(delegate)的 c# 函数

java - Appium - 如何在 iOS 设备上设置地理位置?

c# - unity : 3D movement/Collision detection failure (AddForce, MovePosition, transform.localPosition)

c# - System.Diagnostics.Tracing.EventSource 与 System.Diagnostics.Trace

c# - 说丢弃任务的非异步方法位于异步等待链的 "top"是否正确?

android - 'dump' 方法在 android.location.Location 中做了什么,为什么没有文档?

json - 谷歌地理位置未发现错误

CSS 绝对位置对齐

html - 如果你有一个上边距的 child ,为什么 Body Tag 会移动