简体   繁体   English

获得GPS协调Windows Phone 7

[英]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. 我今天一直在寻找从Windows Phone 7设备收集GPS坐标的时间 - 但是由于我没有实际的测试设备,我试图设置一些我想要返回的虚拟数据而不是真正的GPS数据。 ..然而,这不是很好:这段代码部分是我在这里找到的一个例子。 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? 这是我尝试访问数据的另一个类 - 但是我总是将NaN作为lat1Rad和long1Rad ...你能帮助我吗?

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. 我希望这个例子在模拟器(具有固定的GPS坐标)和手机7设备上起作用 - 它实际上抓住了这个值。

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. 我基本上只想编写一个返回CURRENT GPS Position的类。

Why are you deriving frmo GeoCoordinateWatcher ? 你为什么要获得frmo GeoCoordinateWatcher That's a mistake, IMO. 这是一个错误,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. 目前,您正在委派观察者上设置坐标,但是随后直接从GetGPS实例中请求坐标。

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 . 我建议您使用自己的“固定”位置观察器实现IGeoPositionWatcher<GeoCoordinate> - 然后在执行时决定是使用它还是使用真正的GeoCoordinateWatcher Obviously this means your client code should only depend on IGeoPositionWatcher<GeoCoordinate> instead of GeoCoordinateWatcher directly. 显然,这意味着您的客户端代码应仅依赖IGeoPositionWatcher<GeoCoordinate>而不是直接依赖GeoCoordinateWatcher This should also help for unit testing purposes. 这也应该有助于单元测试目的。

Where is your example code from? 您的示例代码来自哪里?

Have you tried using the sample on MSDN ? 您是否尝试过在MSDN上使用该示例

Alternatively, there's a greate simulator available from http://phone7.wordpress.com/2010/08/02/no-device-no-gps-no-matter-with-code/ 或者,可以从http://phone7.wordpress.com/2010/08/02/no-device-no-gps-no-matter-with-code/获得一个greate模拟器。

当然,Jon的回答非常合适 - 我认为你没有做过合理的界面抽象,但即使你得到了这个,对于模拟数据,你看看Kevin Wolf的Windpws Phone GPS模拟器吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM