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.