Simulate my current position

2019-07-18 13:42发布

问题:

How to simulate my position in my app like GPS does? I want to do this from Additional Tools -> Location

Example to use on your emulator:

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

回答1:

You need a GeoCoordinateWatcher to listen for GPS positions. Later, when getting the first position, you initialize the LabeledMapLocation with the coordinates given by the event arguments and start the map task.

Example:

(First, add System.Device to your project`s references.)

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

And in the emulator you can click around on the Bing map to change your current position as you like.

You should also register for watcher.StatusChanged. This event tells you for example when GPS becomes unavailable.



回答2:

GeoCoordinateWatcher is the correct class for tracking the users location. If you want to simulate the user moving around for testing purposes you can use the new location tracking features in the Mango emulator. Here's a great article:

http://www.jeffblankenburg.com/2011/11/01/31-days-of-mango-day-1-the-new-windows-phone-emulator-tools/

If you are wanting to fake the users location at runtime, you can just create a new instance of the GeoCoordinate class and provide whatever values you want for Latitude, Longitude, Altitude, etc.