如何使用Bing地图获取当前位置?

时间:2012-12-23 06:52:24

标签: windows-phone-7 geolocation bing-maps

我的代码无效,它显示的是整个世界地图。我只想获得当前位置并在其上放置一个图钉。我通过谷歌并没有一个例子有效。

这是我的代码。

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

1 个答案:

答案 0 :(得分:0)

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

Loaded+=PhoneApplicationPage_Loaded 

在构造函数的末尾。