Getting GPS Coordinates Windows Phone 7

I've been looking quite some time today to gather GPS coordinates from a Windows Phone 7 device - however since I do not have an actual test device here I tried to set some dummy data which I want to have returned instead of real GPS Data ... that, however ist not working out too well: This code ist partially an example from so which I found here. However I tried to put it into a class which I can access later.

public class GetGPS : GeoCoordinateWatcher
{
    GeoCoordinateWatcher watcher;

    public GetGPS()
    {
        watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
        watcher.MovementThreshold = 20;

        watcher.PositionChanged += this.watcher_PositionChanged;
        watcher.StatusChanged += this.watcher_StatusChanged;
        watcher.Start();
    }

    private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
    {
        switch (e.Status)
        {
            case GeoPositionStatus.Ready:
                //plingpling
                break;
            case GeoPositionStatus.Disabled:
                // location is unsupported on this device
                break;
            case GeoPositionStatus.NoData:
                watcher.Position.Location.Latitude = 54.086369f;
                watcher.Position.Location.Longitude = 12.124754f;
                break;
        }
    }

    private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
    {
        var epl = e.Position.Location;

        // Access the position information thusly:
        epl.Latitude.ToString("0.000");
        epl.Longitude.ToString("0.000");
        epl.Altitude.ToString();
        epl.HorizontalAccuracy.ToString();
        epl.VerticalAccuracy.ToString();
        epl.Course.ToString();
        epl.Speed.ToString();
        e.Position.Timestamp.LocalDateTime.ToString();
    }
}

This is my other class in which I try to access the data - however I always get NaN as lat1Rad and long1Rad ... can you please help me?

I want that example to be functional on the emulator ( with a fixed GPS Coordinate ) and on a phone 7 device - where it actually grabs the value.

GetGPS location1= new GetGPS();

//GeoCoordinate myPosition = location1.getPosition();

//Radianten berechnen
double lat1Rad = GradZuRad(location1.Position.Location.Latitude);
double long1Rad = GradZuRad(location1.Position.Location.Longitude);

I basically just want to program a class which returns me the CURRENT GPS Position.

Answers


Where is your example code from?

Have you tried using the sample on MSDN?

Alternatively, there's a greate simulator available from http://phone7.wordpress.com/2010/08/02/no-device-no-gps-no-matter-with-code/


Why are you deriving frmo GeoCoordinateWatcher? That's a mistake, IMO. It makes it unclear when you're using the members of your own class and when you're using the delegated instance. At the moment you're setting the coordinates on the delegated watcher, but then asking for the coordinates from the GetGPS instance directly.

I suggest you implement IGeoPositionWatcher<GeoCoordinate> with your own "fixed" position watcher - and then decide at execution time whether to use that or the real GeoCoordinateWatcher. Obviously this means your client code should only depend on IGeoPositionWatcher<GeoCoordinate> instead of GeoCoordinateWatcher directly. This should also help for unit testing purposes.


Of course Jon's answer is very apropos - I think you've not done reasonable interface abstraction, but even when you get that, for simulated data have you taken a look at Kevin Wolf's GPS Simulator for Windpws Phone?


Need Your Help

Ignore Typescript Errors "property does not exist on value of type"

javascript typescript visual-studio-2013

In VS2013 building stops when tsc exits with code 1. This was not the case in VS2012.

Using TRY / CATCH to perform INSERT / UPDATE

sql-server exception try-catch upsert

I have this pattern in a number of stored procedures