模拟我现在的职位
本文关键字:模拟 | 更新日期: 2023-09-27 18:15:20
如何模拟我的位置在我的应用程序像GPS吗?我想在附加工具->位置
中设置在模拟器上使用的示例:
private void button2_Click(object sender, RoutedEventArgs e)
{
BingMapsDirectionsTask bingMapsDirectionsTask = new BingMapsDirectionsTask();
// You can specify a label and a geocoordinate for the end point.
// GeoCoordinate spaceNeedleLocation = new GeoCoordinate(47.6204,-122.3493);
// LabeledMapLocation spaceNeedleLML = new LabeledMapLocation("Space Needle", spaceNeedleLocation);
// If you set the geocoordinate parameter to null, the label parameter is used as a search term.
LabeledMapLocation spaceNeedleLML = new LabeledMapLocation("Space Needle", null);
bingMapsDirectionsTask.End = spaceNeedleLML;
// If bingMapsDirectionsTask.Start is not set, the user's current location is used as the start point.
bingMapsDirectionsTask.Show();
}
您需要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何时不可用。
GeoCoordinateWatcher是跟踪用户位置的正确类。如果您想模拟用户的移动以进行测试,您可以使用Mango模拟器中的新位置跟踪功能。这里有一篇很棒的文章:
http://www.jeffblankenburg.com/2011/11/01/31-days-of-mango-day-1-the-new-windows-phone-emulator-tools/如果你想在运行时伪造用户位置,你可以创建一个GeoCoordinate类的新实例,并为Latitude, Longitude, Altitude等提供你想要的任何值