WP7 gps emulator issue
I have an issue using the GPS emulator provided by microsoft. This is my code
public IGeoPositionWatcher<GeoCoordinate> Watcher { get; private set; }
public IObservable<GeoCoordinate> ObservableGeoCoordinate { get; set; }
private void InitializeGpsDevice()
{
try
{
if (Watcher == null)
{
Watcher = new GpsEmulatorClient.GeoCoordinateWatcher();
}
ObservableGeoCoordinate = CreateObservableGeoPositionWatcher();
Watcher.Start();
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Failed to initialize GPS device:{0}", ex.Message), "GPS Error", MessageBoxButton.OK);
}
}
private IObservable<GeoCoordinate> CreateObservableGeoPositionWatcher()
{
var observable = Observable.FromEvent<GeoPositionChangedEventArgs<GeoCoordinate>>(
e => Watcher.PositionChanged += e,
e =&开发者_StackOverflow社区gt; Watcher.PositionChanged -= e
).Select(e => e.EventArgs.Position.Location);
return observable;
}
After creating my IObservable object I use it this way:
public MainPage()
{
InitializeDefaults();
InitializeComponent();
Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
var observable = GpsHelper.Instance.ObservableGeoCoordinate;
observable
.ObserveOnDispatcher()
.Subscribe(OnPositionChanged);
}
private void OnPositionChanged(GeoCoordinate location)
{
Map.Center = location;
}
But the event OnPositionChanged is never raised. Anyone can suggest why?
If GPSEmulator running and still ...GPS status: NoData!
Try this!
GPSEmulator/MainWindow.xaml.cs/line: 391
string lat = currentPosition.X.ToString().Replace(",", ".");
string lon = currentPosition.Y.ToString().Replace(",", ".");
transmittedLocation = lat+","+lon;
Have you first subscribed the OnLoaded event in your View or Page constructor ?
public MainPage()
{
InitializeComponent();
Loaded += OnLoaded;
}
精彩评论