Detailed explanation of GPS application programming on Windows Phone 7

Source: Internet
Author: User

Although Windows Phone 7 has not been officially released yet, Microsoft's support for third parties is still in place. In the previous article, we will take a look at the detailed description of the WP7 development environment, now let's discuss the LBS development on WP7.

In general, LBS is generally two layers at a level. First, the GPS engine is used to obtain the geographical location. After the location is obtained, the UI provides the corresponding navigation prompt or Liang map.

The GPS engine on the WP7 platform can be said to give me a fresh feeling, no longer requiring programmers to encapsulate it themselves. Microsoft is doing well in this regard. Let's take a look at it with code.

Private void startlocationservice (geopositionaccuracy accuracy)
{
Watcher = new geocoordinatewatcher (accuracy );
Watcher. movementthreshold = 20;

Watcher. statuschanged + = new eventhandler <geopositionstatuschangedeventargs> (watcher_statuschanged );
Watcher. positionchanged + = new eventhandler <geopositionchangedeventargs <geocoordinate> (watcher_positionchanged );

// Start data acquisition
Watcher. Start ();
}

The above is the code for initializing a geocoordinatewatcher object. First, Watcher = new geocoordinatewatcher (accuracy). When initializing this object, you need to pass in the precision. This precision can be geopositionaccuracy. high, geopositionaccuracy. low, geopositionaccuracy. although medim does not see a real machine, I think the higher the accuracy, the higher the battery consumption.

Watcher. movementthreshold = 20; this is to take the latitude and longitude interval,

Watcher. statuschanged + = new eventhandler <geopositionstatuschangedeventargs> (watcher_statuschanged );
This is a statuschanged event that is triggered when the GPS engine status changes.

Watcher. positionchanged + = new eventhandler <geopositionchangedeventargs <geocoordinate> (watcher_positionchanged );

This positionchanged event will be triggered when the location changes.

Let's take a look at these two events.

Void watcher_statuschanged (Object sender, geopositionstatuschangedeventargs E)
{
Deployment. Current. Dispatcher. begininvoke () => mystatuschanged (e ));

}

So why do we use begininvoke instead of invoke in this function? The reason is simple: the invoke means that the synchronization function will block the user's UI thread. In other words, if invoke is used for this function, it may cause the user interface to crash, begininvoke is an asynchronous function that will be called when the time slice is idle, which is reasonable.

Void mystatuschanged (geopositionstatuschangedeventargs E)
{
Switch (E. Status)
{
Case geopositionstatus. Disabled:
Statustextblock. Text = "location is unsupported on this device ";
Break;
Case geopositionstatus. Initializing:

Statustextblock. Text = "Initializing location service," + accuracytext;
Break;
Case geopositionstatus. nodata:
Statustextblock. Text = "Data unavailable," + accuracytext;
Break;
Case geopositionstatus. Ready:

Statustextblock. Text = "sorting data," + accuracytext;
Break;

}
}

Which of the following parameters will the geopositionstatuschanged Parameter Pass to us? Yes, that is, geopositionstatuschangedeventargs, E. status has the following statuses: geopositionstatus. disabled, geopositionstatus. initializing, geopositionstatus. nodata and geopositionstatus. ready: we can see from the name that disabled is unavailable, that is, GPS information cannot be received, initializing is the status in engine initialization, and nodata is not obtained, while ready is in normal engine status.

Void mypositionchanged (geopositionchangedeventargs <geocoordinate> E)
{
Latitudetextblock. Text = E. position. Location. Latitude. tostring ("0.000 ");
Longitudetextblock. Text = E. position. Location. longpolling. tostring ("0.000 ");
}

Geopositionchangedeventargs the parameter geopositionchangedeventargs <geocoordinate> E passed by this event is also easy to use. Longitude is E. position. Location latitude is E. position. Location. longpolling.

I remember that it was quite troublesome to build a GPS engine in the WM era, but now everything has become simple. The geocoordinatewatcher class is all done, so I look forward to the performance of LBS applications on WP7, with such a powerful engine, the application of GPS should be well developed on WP7.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/BEYONDMA/archive/2010/10/01/5917898.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.