Вам нужно GeoCoordinateWatcher
для прослушивания GPS-координат. Позже, получая первую позицию, вы инициализируете LabeledMapLocation
с координатами, заданными аргументами события, и запускаете задачу карты.
Пример:
(Сначала добавьте System.Device
к ссылкам вашего проекта.)
GeoCoordinateWatcher watcher;
// this receives the current GPS position (or the simulated one in the emulator)
private void HandleGeoPositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
// we only need one coordinate - stop watching
watcher.Stop();
// initialize task and location
BingMapsDirectionsTask bingMapsDirectionsTask = new BingMapsDirectionsTask();
GeoCoordinate spaceNeedleLocation = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);
LabeledMapLocation spaceNeedleLML = new LabeledMapLocation("Space Needle", spaceNeedleLocation);
bingMapsDirectionsTask.End = spaceNeedleLML;
// If bingMapsDirectionsTask.Start is not set, the user's current location is used as the start point.
bingMapsDirectionsTask.Show();
}
// this starts watching for GPS coordinates, the Bing task will be invoked later
// when we receive our first coordinate
private void button1_Click(object sender, RoutedEventArgs e)
{
// prepare for coordinate watching
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default) { MovementThreshold = 10 };
// register for position changes
watcher.PositionChanged += HandleGeoPositionChanged;
// start watching
watcher.Start();
}
И в эмуляторе вы можете щелкнуть по карте Bing, чтобы изменить текущую позицию, как вам нравится.
Вы также должны зарегистрироваться на watcher.StatusChanged
. Это событие сообщает вам, например, когда GPS становится недоступным.