模拟我当前的位置

时间:2011-11-21 04:36:57

标签: c# silverlight windows-phone-7 bing

如何在我的应用程序中模拟我在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();
}

2 个答案:

答案 0 :(得分:1)

您需要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变得不可用时,此事件会告诉您。

答案 1 :(得分:0)

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等提供您想要的任何值。