Real coordinates are not allowed in China, so each map service provider will encode the real longitude and latitude (or encrypt it ). For example, for the real longitude and latitude room (Lon, Lat) of People's Square, the latitude and longitude provided by Baidu is (lon_ B, lat_ B), and the latitude and longitude provided by Google is (lon_g, lat_g ). The results displayed on the map are as follows:
|
Lon, lat |
Lon_ B, lat_ B |
Lon_g, lat_g |
Baidu Map |
The display location is not People's Square |
The display location is People's Square. |
The display location is not People's Square |
Google Maps |
The display location is not People's Square |
The display location is not People's Square |
The display location is People's Square. |
Therefore, Baidu's latitude and longitude can only be used on Baidu maps, and Google's latitude and longitude can be used on Google Maps. Otherwise, the displayed position is incorrect. When using a map, for example, to place a POI annotation, the coordinates of the point must be the coordinates of the map. In turn, for example, if Baidu maps search for poi, these poi are Baidu coordinates, so the coordinates obtained by the user are Baidu coordinates.
Baidu provides an interface to convert the real latitude and longitude to the Google Latitude and longitude to a hundred degrees latitude and longitude (also known as converting the real coordinates and Google coordinates into hundred degrees coordinates ). Can be in http://dev.baidu.com/wiki/static/map/API/examples? V = 1.3 & 0_6 #0 & 6.
The following code converts a Google coordinate to a hundred-degree coordinate.
// Synchronous request
+ (Cllocationcoordinate2d) shiftcoordinatefromgoogletobaidu :( cllocationcoordinate2d) googlecoordinate {nsstring * urlstring = [[nsstring stringwithformat: @ "http://api.map.baidu.com/ag/coord/convert? From = 2 & to = 4 & X = % F & Y = % F ", googlecoordinate. longpolling, googlecoordinate. latitude] longitude: nsutf8stringencoding]; // If GPS is switched to Baidu, then from = 0 nsmutableurlrequest * request = [[[nsmutableurlrequest alloc] init] autorelease]; [Request seturl: [nsurl urlwithstring: urlstring]; [Request sethttpmethod: @ "get"]; nshttpurlresponse * urlresponse = nil; nserror * error = [[nserror alloc] init]; nsdata * responsedata = [nsurlconnection sendsynchronousrequest: Request returningresponse: & urlresponse error: & error]; nsstring * result = [[nsstring alloc] initwithdata: responsedata encoding: Response]; nslog (@ "Result: % @", result); sbjsonparser * parser = [[sbjsonparser alloc] init]; nsdictionary * memblist = [parser objectwithstring: Result error: nil]; nsstring * x = [[memblist objectforkey: @ "X"] retain]; nsstring * Y = [[memblist objectforkey: @ "Y"] retain]; nsstring * stringvalue = x; byte inputdata [[stringvalue encoding: bytes]; [[stringvalue datausingencoding: bytes] getbytes: inputdata]; size_t inputdatasize = (size_t) [stringvalue length] size_t outputdatasize = bytes (inputdatasize); byte outputdata [outputdatasize]; // prepare a byte [] for the decoded Data Partition (inputdata, inputdatasize, outputdata, & outputdatasize ); nsdata * thedata = [[nsdata alloc] Bytes: outputdata length: outputdatasize]; nsstring * resultx = [[nsstring alloc] initwithdata: thedata encoding: bytes]; then newcoordinate; newcoordinate. longpolling = [resultx doublevalue]; [thedata release]; [resultx release]; stringvalue = y; byte inputdata1 [[stringvalue encoding: nsutf8stringencoding]; [stringvalue datausingencoding: bytes] getbytes: inputdata1]; inputdatasize = (bytes) [stringvalue length]; outputdatasize = bytes (inputdatasize); byte outputdata1 [outputdatasize]; Combine (inputdata1, inputdatasize, outputdata1, & outputdatasize); thedata = [[nsdata alloc] initwithbytes: outputdata1 length: outputdatasize]; resultx = [[nsstring alloc] initwithdata: thedata encoding: Convert]; newcoordinate. latitude = [resultx doublevalue]; return newcoordinate ;}
// Asynchronous request:
-(Void) login :( cllocationcoordinate2d) googlecoordinate {static cllocationcoordinate2d newcoordinate; nsstring * urlstring = [[nsstring stringwithformat: @ "http://api.map.baidu.com/ag/coord/convert? From = 2 & to = 4 & X = % F & Y = % F ", googlecoordinate. longpolling, googlecoordinate. latitude] longitude: longitude]; nsmutableurlrequest * request = [[[nsmutableurlrequest alloc] init] autorelease]; [Request seturl: [nsurl urlwithstring: urlstring]; [Request sethttpmethod: @ "get"]; required * operationqueue = [[nsoperationqueue alloc] init]; [nsurlconnection failed: Request queue: operationqueue completionhandler: ^ (nsurlresponse * urlresponce, nsdata * responsedata, nserror * error) {If (error) {nslog (@ "error: \ n % @", error); return;} nsstring * result = [[nsstring alloc] initwithdata: responsedata encoding: nsutf8stringencoding]; sbjsonparser * parser = [[sbjsonparser alloc] init]; nsdictionary * memblist = [parser objectwithstring: Result error: Nil]; nsstring * x = [[memblist objectforkey: @ "X"] retain]; nsstring * Y = [[memblist objectforkey: @ "Y"] retain]; nsstring * stringvalue = X; byte inputdata [[stringvalue encoding: nsutf8stringencoding]; [[stringvalue datausingencoding: encoding] getbytes: inputdata]; size_t inputdatasize = (size_t) [stringvalue length]; size_t outputdatasize = bytes (inputdatasize); byte outputdata [outputdatasize]; // prepare a byte [] for the decoded Data Partition (inputdata, inputdatasize, outputdata, & outputdatasize ); nsdata * thedata = [[nsdata alloc] initwithbytes: outputdata length: outputdatasize]; nsstring * resultx = [[nsstring alloc] initwithdata: thedata encoding: bytes]; newcoordinate. longpolling = [resultx doublevalue]; [thedata release]; [resultx release]; stringvalue = y; byte inputdata1 [[stringvalue encoding: nsutf8stringencoding]; [stringvalue datausingencoding: bytes] getbytes: inputdata1]; inputdatasize = (bytes) [stringvalue length]; outputdatasize = bytes (inputdatasize); byte outputdata1 [outputdatasize]; Combine (inputdata1, inputdatasize, outputdata1, & outputdatasize); thedata = [[nsdata alloc] initwithbytes: outputdata1 length: outputdatasize]; resultx = [[nsstring alloc] initwithdata: thedata encoding: Convert]; newcoordinate. latitude = [resultx doublevalue]; nslog (@ "coordinate: % F, % F", newcoordinate. longpolling, newcoordinate. latitude); // perform subsequent logic processing based on the transformed longitude and latitude here}]; [operationqueue release];}
The JSON and base64transcode packages are used. You can search for them online.
IOS development based on Baidu map (1) -- current user location
IOS development based on Baidu map (2) -- getting poi
IOS development based on Baidu map (3) -- address search
IOS development based on Baidu map (4) -- path search
IOS development based on Baidu map (5)-Summary of core issues
IOS development based on Baidu map (6) -- Google coordinates to hundreds of coordinates
IOS development based on Baidu map (7) -- convert Baidu coordinates to Google coordinates