获取有关单击了哪个图钉的信息

本文关键字:信息 单击 获取 | 更新日期: 2023-09-27 18:29:17

我正在构建一个windows phone 8.1应用程序,该应用程序读取一个文件(其中包含GPS点,每个点由例如纬度和经度字段组成)。对于每个点,我都有它,所以它在地图控件上显示一个小图钉图标,基于上面文件中的坐标。我想让图钉可以点击,最终目标是获得用户点击的点的纬度和经度,并根据点击的点进行适当的操作。我怎样才能得到这些信息?这是我尝试过的,但不起作用:

 private async void Image_Tapped(object sender, TappedRoutedEventArgs e)
    {
        var point = sender as Geopoint;
        MessageDialog msgbox = new MessageDialog("point tapped: " + point.Position.Latitude );
        await msgbox.ShowAsync();
    }

这是我的模板:

<Maps:MapControl x:Name="myMap">
        <Maps:MapItemsControl ItemsSource="{Binding }">
            <Maps:MapItemsControl.ItemTemplate>
                <DataTemplate>
                    <Image Source="Assets/pushpin.png" Tapped="Image_Tapped" Width="50" Height="50"  Maps:MapControl.Location="{Binding}"></Image>                     
                </DataTemplate>
            </Maps:MapItemsControl.ItemTemplate>
        </Maps:MapItemsControl>
     </Maps:MapControl>

这是我用来读取文件和显示图钉的东西(它显示它们很好)

 protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        Geoposition position = await App.DataModel.GetCurrentPosition();
        await myMap.TrySetViewAsync(position.Coordinate.Point, 16D);
        List<Geopoint> list = new List<Geopoint>();            
        bool exist; // used to check if JSON file exists
        exist = await App.DataModel.FileExistsAsync();
        if (exist == true)
        {
            // read the file and load points into a list
            await App.DataModel.ReadFile();

            foreach (var point in App.DataModel.notes)
            {
                Geopoint geo = new Geopoint(new BasicGeoposition()
                {
                    Latitude = point.Latitude,
                    Longitude = point.Longitude
                });
                list.Add(geo);
            }
        }
        myMap.DataContext = list;
    }

获取有关单击了哪个图钉的信息

Image_Tapped处理程序中,sender变量不是Geopoint,而是Image。但是Geopoint应该是该图像的DataContext

var point = (sender as Image).DataContext as Geopoint;