[IOS] cllocationmanager and China shift)

Source: Internet
Author: User

The cllocationmanager class monitors GPS location messages. When the user's coordinates change, the following method is called for notification:

-(Void) locationmanager :( cllocationmanager *) manager didupdatetolocation :( cllocation *) newlocation fromlocation :( cllocation *) oldlocation;

The problem arises. When we map the coordinates in newlocation to the mkmapview control, we will find that this point is not in our position, but a place around us a hundred meters away.
As for the reason ,.

Then, we need to convert the original (real) positional coordinates into coordinates with Chinese characteristics.
This article summarizes two solutions to this problem:

1. Use the private class mklocationmanager of IOS for computation.
This approach is risky. Apple does not allow direct calls of private modules. In other words, your software may be deny.

Because it is a private module, we need to declare this class and the functions we need. The Code is as follows:

@ Interface mklocationmanager + (ID) sharedlocationmanager; // create and obtain the mklocationmanager instance-(bool) chinashiftenabled; // determine whether the IOS system supports calculating the offset-(cllocation *) _ applychinalocationshift :( cllocation *) ARG; // input the original position and calculate the offset position @ end

In the location listening function of cllocationmanager, we convert newlocation to the Chinese location.

-(Void) locationmanager :( cllocationmanager *) manager didupdatetolocation :( cllocation *) newlocation fromlocation :( cllocation *) oldlocation {If ([[mklocationmanager sharedlocationmanager] chinashiftenabled]) {newlocation = [[mklocationmanager sharedlocationmanager] _ applychinalocationshift: newlocation]; If (newlocation = nil) {// It is very important to calculate the location. It seems that the network is required, the NIL is returned several times before the software is started. Return ;}}
...}

In this way, the converted newlocation is already in China. When mapped to mkmapview, the correct location is displayed.

 

2. Enable the showsuserlocation function of mkmapview.

When initializing mkmapview, set showsuserlocation to yes. mkmapview starts the built-in Location listening service. When the user's location changes, call the delegate callback function:

-(Void) mapview :( mkmapview *) mapview didupdateuserlocation :( mkuserlocation *) userlocation {// The userlocation obtained here is already the location after the offset}

This method does not use IOS's private classes and functions, and has no risk of being destroyed. The disadvantage may be that you cannot perform rich configurations like cllocationmanager, at least I haven't found them yet.

 

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.