获取有关单击哪个推针的信息

时间:2014-11-04 17:45:31

标签: c# geolocation maps windows-phone-8.1

我正在构建一个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;
    }

1 个答案:

答案 0 :(得分:0)

Image_Tapped处理程序中,发件人变量不是Geopoint,而是Image。但Geopoint应该是此图片的DataContext

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