ArcGIS Runtime for IOS 几何对象与json的互相转换

0
分享 2013-09-05
用户通过客户端绘制的几何对象在有的应用场景下可能需要保存起来以便后期使用,那么你可以考虑将几何对象转换为json然后保持到sqllite数据库中,后期需要时候的时候在通过解析json对象的方式返回几何对象给操作者,需要注意的是在本例中,在存储几何对象的同时也存储了该对象的类型为后期json解析为几何对象奠定基础。废话不多少,核心代码如下:
.h头文件中的定义如下:

#import <Foundation/Foundation.h>
#import <ArcGIS/ArcGIS.h>
@interface ConvterGeometryJson : NSObject
- (NSString*) GeometryToJson:(AGSGeometry*)geometry;
-(AGSGeometry*)JsonToGeometry:(NSString*)json geometype:(NSString*)type;
@end
.m文件实现如下:

#import "ConvterGeometryJson.h"

@implementation ConvterGeometryJson
-(NSString*)GeometryToJson:(AGSGeometry *)geometry
{
if (geometry==nil) {
return nil;
}
else
{
NSDictionary *json = [geometry encodeToJSON];
NSString* jsonString = [json ags_JSONRepresentation];
return jsonString;
}
}
-(AGSGeometry*)JsonToGeometry:(NSString *)json geometype:(NSString *)type{
NSDictionary* jsondic = [json ags_JSONValue];
AGSGeometry *geo;
if ([type isEqual:@"Point"]) {
geo=[[AGSPoint alloc]initWithJSON:jsondic];
} else if([type isEqual:@"Polyline"])
{
geo=[[AGSPolyline alloc]initWithJSON:jsondic];
}
else
{
geo=[[AGSPolygon alloc] initWithJSON:jsondic];
}

NSLog(@"%@",jsondic);

return geo;
}
@end
通过以上方法就完成了空间几何对象和json字符串的互相转换工作。另外需要注意的是在使用sqllite把转换的几何对象字符串写入数据库对应字段的过程中可能会遇到特殊字符的问题导致sql语句出现不可预知的错误,可以考虑通过如下方法解决:

sqlite3_bind_text(statement, 1,[name cStringUsingEncoding:1] , -1, SQLITE_TRANSIENT);
具体的sqllite可以查询其他相关资料

文章来源:http://blog.csdn.net/esrichinacd/article/details/9762023

0 个评论

要回复文章请先登录注册