1

我正在使用 C# 制作一个 Windows 应用商店应用程序,并且需要一些帮助来了解当点击地图上的图钉时的方法。到目前为止,我创建了一个名为 currentPin 的 Pushpin,它是在 XAML 代码中创建的。我还为这里的 Tapped 事件提供了一个参考,称为 pushpin_Tapped。

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <bm:Map x:Name="MyMap" Credentials="{StaticResource BingMapsApiKey}" Holding="map_Held">

            <bm:Map.Children>
                <bm:Pushpin x:Name="pin" Tapped="pushpinTapped">

                </bm:Pushpin>
            </bm:Map.Children>
        </bm:Map>
    </Grid>

然后,我将 currentPin 放在 MainPage.xaml.cs 代码中 OnNavigatedTo 方法中的当前位置。我还创建了点击此图钉时的方法。在这种方法中,我显示了一个对话框,用于显示点击 currentPin 的时间。

private async void pushpinTapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
            {

                var x = MapLayer.GetPosition(pin);
                MessageDialog dialog = new MessageDialog("You are here " + x.Latitude + " " + x.Longitude);
                await dialog.ShowAsync();
            }

然后,我有一种方法可以在用户持有地图时在某个位置放置一个新的图钉,代码如下:

private void map_Held(object sender, HoldingRoutedEventArgs e)        
{            
    Debug.WriteLine("You held at" + DateTime.Now.ToString() + "" + e.GetPosition(MyMap));            
    var pos = e.GetPosition(MyMap);            
    Location location;            
    MyMap.TryPixelToLocation(pos, out location);                          
    Pushpin pin = new Pushpin();            
    MyMap.Children.Add(pin);                        
    MapLayer.SetPosition(pin, location);                       
}

我知道它可能正盯着我看,但我对如何获取消息框或对话框或点击每个新引脚时发生的任何事情一无所知。有人可以解释一下吗?谢谢,艾梅

4

2 回答 2

2
private void map_Held(object sender, HoldingRoutedEventArgs e)        
{            
    Debug.WriteLine("You held at" + DateTime.Now.ToString() + "" + e.GetPosition(MyMap));            
    var pos = e.GetPosition(MyMap);            
    Location location;            
    MyMap.TryPixelToLocation(pos, out location);                          
    Pushpin pin = new Pushpin();
    pin.Tapped += pushpinTapped;  // <<<<<<=====LOOK AT THIS
    MyMap.Children.Add(pin);                        
    MapLayer.SetPosition(pin, location);                       
}
于 2012-10-23T16:09:55.033 回答
1

在 Map_Held 中,为点击的事件添加处理程序...

private void map_Held(object sender, HoldingRoutedEventArgs e)        
{            
    Debug.WriteLine("You held at" + DateTime.Now.ToString() + "" + e.GetPosition(MyMap));            
    var pos = e.GetPosition(MyMap);            
    Location location;            
    MyMap.TryPixelToLocation(pos, out location);                          
    Pushpin newpin = new Pushpin();            
    newpin.Tapped += pushpin_Tapped;
    MyMap.Children.Add(newpin);                        
    MapLayer.SetPosition(newpin, location);                       
}

并在您点击的事件处理程序中,将对您的 pin 的引用从pin以下更改。

private async void pushpinTapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
    PushPin tappedpin = sender as PushPin;  // gets the pin that was tapped
    if(null == tappedpin) return;           // null check to prevent bad stuff if it wasn't a pin.
    var x = MapLayer.GetPosition(tappedpin);
    MessageDialog dialog = new MessageDialog("You are here " + x.Latitude + " " + x.Longitude);
    await dialog.ShowAsync();
}
于 2012-10-23T16:31:32.207 回答