The geometric objects drawn by the client may need to be saved for later use in some application scenarios. You can consider converting the geometric objects to JSON and then retaining them in the sqllite database, when necessary, return the geometric object to the operator by parsing the JSON object. Note that in this example, while storing the geometric object, the type of the object is also stored, which lays the foundation for later JSON parsing. The core code is as follows:
The. h header file is defined as follows:
# Import <Foundation/Foundation. h>
# Import <ArcGIS/ArcGIS. h>
@ Interface convtergeometryjson:
Nsobject
-(Nsstring *) geometrytojson :( agsgeometry *) geometry;
-(Agsgeometry *) jsontogeometry :( nsstring *) JSON geometype :( nsstring *) type;
@ End
The. M file is implemented as follows:
# 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 = [[agspolympus line
Alloc] initwithjson: jsondic];
}
Else
{
GEO = [[agspolympus Gon alloc]
Initwithjson: jsondic];
}
Nslog (@ "% @", jsondic );
Return GEO;
}
@ End
The above method completes the mutual conversion between the space geometric object and the JSON string. In addition, it should be noted that when sqllite is used to write the converted geometric object string to the corresponding field of the database, special character problems may occur, resulting in an unpredictable error in the SQL statement, you can solve the problem by using the following methods:
Sqlite3_bind_text (statement, 1, [name
Cstringusingencoding: 1],-1,
Sqlite_transient );
For more information, see sqllite.