2011-11-21 71 views
2

如何模擬我在我的應用程序中的位置,如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(); 
} 
+4

之前沒有聽說過'Bing Phone 7';) –

回答

1

你需要一個GeoCoordinateWatcher監聽GPS位置。之後,當獲得第一個位置時,用事件參數給出的座標初始化LabeledMapLocation並啓動地圖任務。

例子:

(首先,添加System.Device您project`s引用。)

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

而且在仿真器,你可以按一下週圍的兵地圖上,你想改變你的當前位置。

您還應註冊爲watcher.StatusChanged。例如,當GPS不可用時,此事件會告訴您。

0

GeoCoordinateWatcher是跟蹤用戶位置的正確類。如果您想模擬用戶移動以進行測試,您可以使用Mango模擬器中的新位置跟蹤功能。這裏有一個很好的文章:

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

如果你在運行時想要假冒用戶的位置,你可以創建會有地理座標類的新實例,並提供你想要的任何值,經緯度,海拔高度,等等。