0

我的代码不起作用,它显示了整个世界地图。我只想获取当前位置并在其上放置一个图钉。我已经通过谷歌,没有一个例子有效。

这是我的代码。

GeoCoordinateWatcher watcher;

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }


    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        if (watcher == null)
        {
            Console.WriteLine(watcher);
            watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
            watcher.MovementThreshold = 20;
            watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
            watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);

        }
        watcher.Start();
    }

    private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
    {
        Pushpin pin = new Pushpin();
        pin.Template = this.Resources["currentPushPin"] as ControlTemplate;
        pin.Location = watcher.Position.Location;
        mapControl.Items.Add(pin);
        myMap.SetView(watcher.Position.Location, 15.0);
        watcher.Stop();
    }

    void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
    {
        if (e.Position.Location.IsUnknown)
        {
            MessageBox.Show("Please wait while your prosition is determined....");
            return;
        }
        Pushpin pin = new Pushpin();
        myMap.Center = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);
        pin.Template = this.Resources["currentPushPin"] as ControlTemplate;
        pin.Location = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);
        myMap.SetView(watcher.Position.Location, 18.0);
    }
4

1 回答 1

0

您在 PhoneApplicationPage_Loaded 中启动 GeoCordinateWatcher,但永远不会调用此方法。添加

Loaded+=PhoneApplicationPage_Loaded 

在构造函数的末尾。

于 2012-12-23T10:30:51.010 回答