开发者

Use of GPS Classes Events in other classes

开发者 https://www.devze.com 2023-02-04 22:28 出处:网络
I have a GPS class for Windows Phone 7 which I got out of the MSDN. public class GetGPS : GeoCoordinateWatcher

I have a GPS class for Windows Phone 7 which I got out of the MSDN.

public class GetGPS : GeoCoordinateWatcher
    {
        GeoCoordinateWatcher watcher;
        public float longitude { get; set; }

        public GetGPS()
        {
            watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
            watcher.MovementThreshold = 20.0;
            watcher.PositionChanged += watcher_PositionChanged;
            watcher.StatusChanged += this.watcher_StatusChanged;
            watcher.Start();
        }

        public void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case GeoPositionStatus.Initializing:
                    break;
                case GeoPositionStatus.Ready:
                    //plingpling
                    break;
                case GeoPositionStatus.Disabled:
                    // location is unsupported on this device
                    break;
                case GeoPositionStatus.NoData:
                   //pling
                    break;
            }
        }
        public void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            Debug.WriteLine(String.Format("***POSITION: {0}, {1}", e.Position.Location.Latitude, e.Position.Location.Longitude));
            Helfer.myLocation = e.Position.Location;
        }

    }

When I try to use this class in my Program I never get the actual position in which I am - I get that position after the program was running for some开发者_StackOverflow社区 time - I guess I can hold the asynchronous execution accountable for that.

However, when I implement the GetGPS constructor directly in the class where I need the gps (and define the watcher in that class too...) I get what I want - but since I have to get the GPS position in some parts in my program I thought it would be best to outsource that functionality into a class (DRY-Principle).

How could I create an instance of GetGPS in a class "test" which implements - or observes the StatusChanged and PositionChanged objects?

If I could create an event listener in that class ... I could get my desired functionality :).


If you expect that the GPS position will change frequently during your applications execution, and you need real-time updates, what I would do, is simply add a public property called CurrentLocation to your class, and set its value in the PositionChanged event. Then you keep a global reference to an instance of your GetGPS class, and when ever you need to access the location, you use the CurrentLocation property.

0

精彩评论

暂无评论...
验证码 换一张
取 消