iOS地图坐标系转换

1,iOS百度地图坐标系转换----百度To谷歌
<span style="word-wrap: normal; word-break: normal; line-height: 19.5px; color: rgb(122, 72, 47);">UIKIT_EXTERN</span> <span style="word-wrap: normal; word-break: normal; line-height: 19.5px; color: rgb(115, 64, 163);">NSDictionary</span>* BMKBaiduCoorForWgs84(<span style="word-wrap: normal; word-break: normal; line-height: 19.5px; color: rgb(115, 64, 163);">CLLocationCoordinate2D</span> coorWgs84);
<span style="word-wrap: normal; word-break: normal; line-height: 19.5px; color: rgb(122, 72, 47);">UIKIT_EXTERN</span><span style="word-wrap: normal; word-break: normal; line-height: 19.5px; color: rgb(0, 0, 0);"> </span>NSDictionary<span style="word-wrap: normal; word-break: normal; line-height: 19.5px; color: rgb(0, 0, 0);">* BMKBaiduCoorForGcj(</span>CLLocationCoordinate2D<span style="word-wrap: normal; word-break: normal; line-height: 19.5px; color: rgb(0, 0, 0);"> coorGcj);</span>
<span style="word-wrap: normal; word-break: normal; line-height: 19.5px; color: rgb(122, 72, 47);">UIKIT_EXTERN</span> <span style="word-wrap: normal; word-break: normal; line-height: 19.5px; color: rgb(115, 64, 163);">CLLocationCoordinate2D</span> BMKCoorDictionaryDecode(<span style="word-wrap: normal; word-break: normal; line-height: 19.5px; color: rgb(115, 64, 163);">NSDictionary</span>* dictionary);
<span style="word-wrap: normal; word-break: normal; line-height: 19.5px; color: rgb(126, 72, 48);">#import </span>"GTMBase64.h"
 - (CLLocationCoordinate2D )getBaiduFromGoogle:(CLLocationCoordinate2D )locationCoord
{
    NSDictionary *baidudict =BMKBaiduCoorForGcj(CLLocationCoordinate2DMa<wbr>ke(locationCoord.latitude, locationCoord.longitude));
    NSLog(@"google坐标是:%f,%f",locationCoord.latitude,locationCoord.longitude);
    NSString *xbase64 =[baidudict objectForKey:@"x"];
    NSString *ybase64 = [baidudict objectForKey:@"y"];
    NSData *xdata = [GTMBase64 decodeString:xbase64];
    NSData *ydata = [GTMBase64 decodeString:ybase64];
    NSString *xstr = [[NSString alloc] initWithData:xdata encoding:NSUTF8StringEncoding];
    NSString *ystr = [[NSString alloc] initWithData:ydata encoding:NSUTF8StringEncoding];
    CLLocationCoordinate2D result;
    result.latitude =[ystr floatValue];
    result.longitude = [xstr floatValue];
    NSLog(@"百度坐标是:%f,%f",result.latitude,result.longitude);
    return result;
}
</wbr>

百度坐标和GPS坐标转换在很近的距离时偏差非常接近。 

假设你有百度坐标:x1=116.397428,y1=39.90923 
把这个坐标当成GPS坐标,通过接口获得他的百度坐标:x2=116.41004950566,y2=39.916979519873 
通过计算就可以得到GPS的坐标: 
x = 2*x1-x2,y = 2*y1-y2 
x=116.38480649434001 
y=39.901480480127 
2, 在WebGIS的开发中经常用到的地图投影为Web墨卡托和WGS84,谷歌地图,bingmaps,百度地图,mapabc,mapbar,以及 ArcGIS online上的大部分地图为Web墨卡托地图,ArcGIS online上最开始发布的地图投影为WGS84。
在开发过程中很多时候会遇到不同坐标系之间互转的问题,特别是底图使用Web墨卡托,定位(GPS,wifi等)信号坐标为WGS84坐标的时候,那么通 用解决方案就是写一个坐标参考系的转换库,类似于proj4,但一般情况下很少用到那么多的参考系之间的互转,并且在客户端实现或者调用proj4都是一 件很困难或者麻烦的事情,大多数情况下我们实现Web墨卡托坐标与WGS84坐标互转就可以了。
下面是使用objective-c实现的Web墨卡托坐标与WGS84坐标互转程序,当然也可以使用其他语言来实现,使用起来比较简单和方便。
//经纬度转墨卡托
-(CGPoint )lonLat2Mercator:(CGPoint ) lonLat
{
    CGPoint  mercator;
    double x = lonLat.x *20037508.34/180;
    double y = log(tan((90+lonLat.y)*M_PI/360))/(M_PI/180);
    y = y *20037508.34/180;
    mercator.x = x;
    mercator.y = y;
    return mercator ;
}
//墨卡托转经纬度
-(CGPoint )Mercator2lonLat:(CGPoint ) mercator
{
    CGPoint lonLat;
    double x = mercator.x/20037508.34*180;
    double y = mercator.y/20037508.34*180;
    y= 180/M_PI*(2*atan(exp(y*M_PI/180))-M_PI/2);
    lonLat.x = x;
    lonLat.y = y;
    return lonLat;
}
3, WGS 84 - SRID 4326    WGS 84 标准提供地球的球体参照面。这是全球定位系统(Global Positioning System,简称 GPS)使用的空间参照系。WGS 84 的坐标原点是地球的质心,精度可达到 ±1 米。WGS 表示世界坐标系 (World Geodetic System)。

WGS 84 坐标单位是度,其中,第一个坐标是经度,范围是 -180 到 180;第二个坐标是纬度,范围是 -90 到 90。

WGS 84 的缺省测量单位是米,是球形地球类型的空间参照系。

4,GPS to ArgGIS实例(ArcGIS for iOS)

 

// 定位

@property (nonatomic, retain) CLLocationManager *LocationMan;

@property (nonatomic, retain) AGSGraphic *LocationPin;

 

- (IBAction)MapLocationBtnTouched:(id)sender 

{

    UIButton *btn = (UIButton *)sender;

    if (LocationMan == nil) {

        LocationMan = [[CLLocationManager allocinit];

        LocationMan.delegate = self;

    }

    if (!btn.isSelected) {

        [LocationMan startUpdatingLocation];

        [btn setSelected:YES];

    }else{

        [LocationMan stopUpdatingLocation];

        [self.graphicsLayer removeGraphic:self.LocationPin];

        self.LocationMan = nil;

        self.LocationPin = nil;

        [btn setSelected:NO];

        [self.graphicsLayer dataChanged];

    }

}

 

#pragma mark - CLLocationDelegate Methods


// (__MAC_10_6, __MAC_NA, __IPHONE_2_0, __IPHONE_6_0)

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

{

    [self createLocationPin:newLocation];

    

}


// __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

    [self createLocationPin:[locations lastObject]];

}


- (CGPoint)lonLat2Mercator:(CGPoint)lonLat

{

    CGPoint  mercator;

    double x = lonLat.x *20037508.34/180;

    double y = log(tan((90+lonLat.y)*M_PI/360))/(M_PI/180);

    y = y *20037508.34/180;

    mercator.x = x;

    mercator.y = y;

    

    return mercator;

}


- (void)createLocationPin:(CLLocation *)location

{

    AGSGeometryEngine *GeometryEngine = [AGSGeometryEngine defaultGeometryEngine];


    // 经纬度对应xy

    CGPoint point = CGPointMake(location.coordinate.longitude, location.coordinate.latitude);

    point = [self lonLat2Mercator:point];

    

    // 102100是墨卡托空间参考系 WKID可以参照此网址:http://gis.jl.gov.cn/Portal/api/JS/help/jsapi/spatialreference.htm

 

    AGSSpatialReference *sPrf = [AGSSpatialReferencespatialReferenceWithWKID:102100];

    AGSPoint *mappoint = [AGSPoint pointWithX:point.x y:point.y spatialReference:sPrf];

    // 3857就是GIS服务的空间参考系, 可以在网页中打开GIS服务查看此参考系

    AGSPoint *mappoint2 = (AGSPoint *)[GeometryEngine projectGeometry:mappointtoSpatialReference:[AGSSpatialReference spatialReferenceWithWKID:3857]];

    if (LocationPin == nil) {

        AGSPictureMarkerSymbol *pt = [AGSPictureMarkerSymbol pictureMarkerSymbolWithImageNamed:@"POIRedPin_small.png"];

        self.LocationPin = [[AGSGraphic alloc] initWithGeometry:mappoint2 symbol:ptattributes:nil infoTemplateDelegate:nil];

        [self.graphicsLayer addGraphic:LocationPin];

    }else{

        self.LocationPin.geometry = mappoint2;

    }

    

    [self.graphicsLayer dataChanged];

}


- (void)locationManager:(CLLocationManager *)manager

       didFailWithError:(NSError *)error

{

    NSString *errMsg = [error localizedDescription];

}

GIS服务为:

NSString *TStr =@"http://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer";

// 如果TStr中有汉字的话, 要执行下面代码, 下面这个服务只是测试, 无此服务.

//        NSString *TStr = @"http://server.arcgisonline.com/ArcGIS/rest/services/Canvas/中国/MapServer";

//        TStr = [TStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        NSURL *mapUrl = [NSURL URLWithString:TStr];

        AGSTiledMapServiceLayer *tiledLyr = [AGSTiledMapServiceLayertiledMapServiceLayerWithURL:mapUrl];

        [self.BaseMapView addMapLayer:tiledLyr withName:@"TiledLayer"];

猜你喜欢

转载自blog.csdn.net/xiao19911130/article/details/50038537