在中国,是不允许使用真实的坐标的,所以每个地图服务商都会对真实的经纬度进行编码(也可以称之为加密吧)。举个例子,比如人民广场的真实的经纬度室(LON, LAT),那么百度提供的经纬度为(LON_B, LAT_B),google提供的经纬度为(LON_G, LAT_G)。在地图上的显示结果如下:
LON, LAT | LON_B, LAT_B | LON_G, LAT_G | |
百度地图 | 显示位置不是人民广场 | 显示位置是人民广场 | 显示位置不是人民广场 |
google地图 | 显示位置不是人民广场 | 显示位置不是人民广场 | 显示位置是人民广场 |
所以,只能在百度地图上使用百度的经纬度,在谷歌地图上使用谷歌的经纬度,否则显示的位置不正确。在使用相应地图时,比如要放置一个POI Annotation,那么这个点的坐标必须是该地图的坐标。反过来,比如百度地图自己搜索出来了POI,那么这些POI都是百度坐标,所以用户此时获取到的坐标是百度坐标。
百度提供了接口,可以将真实的经纬度和谷歌经纬度转换成百度经纬度(也称之为将真实坐标和谷歌坐标转换成百度坐标)。可以在http://dev.baidu.com/wiki/static/map/API/examples/?v=1.3&0_6#0&6上进行测试。
下面是从google坐标转换成百度坐标的代码。
//同步请求
+ (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.longitude, googleCoordinate.latitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//如果转换GPS到百度则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:NSUTF8StringEncoding]; 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 lengthOfBytesUsingEncoding:NSUTF8StringEncoding]]; [[stringValue dataUsingEncoding:NSUTF8StringEncoding] getBytes:inputData]; size_t inputDataSize = (size_t)[stringValue length]; size_t outputDataSize = EstimateBas64DecodedDataSize(inputDataSize); Byte outputData[outputDataSize];//prepare a Byte[] for the decoded data Base64DecodeData(inputData, inputDataSize, outputData, &outputDataSize); NSData *theData = [[NSData alloc] initWithBytes:outputData length:outputDataSize]; NSString *resultX = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]; CLLocationCoordinate2D newCoordinate; newCoordinate.longitude=[resultX doubleValue]; [theData release]; [resultX release]; stringValue = Y; Byte inputData1[[stringValue lengthOfBytesUsingEncoding:NSUTF8StringEncoding]]; [[stringValue dataUsingEncoding:NSUTF8StringEncoding] getBytes:inputData1]; inputDataSize = (size_t)[stringValue length]; outputDataSize = EstimateBas64DecodedDataSize(inputDataSize); Byte outputData1[outputDataSize]; Base64DecodeData(inputData1, inputDataSize, outputData1, &outputDataSize); theData = [[NSData alloc] initWithBytes:outputData1 length:outputDataSize]; resultX = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]; newCoordinate.latitude=[resultX doubleValue]; return newCoordinate; }
//异步请求:
- (void)shiftCoordinateFromGoogleToBaidu:(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.longitude, googleCoordinate.latitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"GET"]; NSOperationQueue* operationQueue=[[NSOperationQueue alloc]init]; [NSURLConnection sendAsynchronousRequest: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 lengthOfBytesUsingEncoding:NSUTF8StringEncoding]]; [[stringValue dataUsingEncoding:NSUTF8StringEncoding] getBytes:inputData]; size_t inputDataSize = (size_t)[stringValue length]; size_t outputDataSize = EstimateBas64DecodedDataSize(inputDataSize); Byte outputData[outputDataSize];//prepare a Byte[] for the decoded data Base64DecodeData(inputData, inputDataSize, outputData, &outputDataSize); NSData *theData = [[NSData alloc] initWithBytes:outputData length:outputDataSize]; NSString *resultX = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]; newCoordinate.longitude=[resultX doubleValue]; [theData release]; [resultX release]; stringValue = Y; Byte inputData1[[stringValue lengthOfBytesUsingEncoding:NSUTF8StringEncoding]]; [[stringValue dataUsingEncoding:NSUTF8StringEncoding] getBytes:inputData1]; inputDataSize = (size_t)[stringValue length]; outputDataSize = EstimateBas64DecodedDataSize(inputDataSize); Byte outputData1[outputDataSize]; Base64DecodeData(inputData1, inputDataSize, outputData1, &outputDataSize); theData = [[NSData alloc] initWithBytes:outputData1 length:outputDataSize]; resultX = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]; newCoordinate.latitude=[resultX doubleValue]; NSLog(@"Coordinate:%f,%f",newCoordinate.longitude,newCoordinate.latitude); //在这里根据转换后的经纬度进行后续逻辑处理 } ]; [operationQueue release]; }
iOS基于百度地图的开发(1)——用户当前位置
iOS基于百度地图的开发(2)——获取POI
iOS基于百度地图的开发(3)——地址搜索
iOS基于百度地图的开发(4)——路径搜索
iOS基于百度地图的开发(5)——核心问题汇总
iOS基于百度地图的开发(6)——google坐标转换成百度坐标
iOS基于百度地图的开发(7)——百度坐标转换成Google坐标