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

时间:2014-11-19 19:27:22

标签: c# geolocation position bing-maps

我正在从Bing地图中移除图钉以响应图钉上的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;
}

...但是唉,&#34; GetPosition&#34;没有回归&#34;世界&#34;位置,但屏幕位置,IOW点而不是位置(当然,当你考虑它时,这当然是有道理的,但我真的不是)。正如热带雨林居民注意到here,RightTappedRoutedEventArgs&#39; GetPosition方法&#34;返回指针的x和y坐标&#34;:

public Point GetPosition(
  UIElement relativeTo
)

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

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

更新

根据宝马(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

使用宝马的想法,并根据需要更新代码,这似乎有效:

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;
}

1 个答案:

答案 0 :(得分:1)

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

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