android arcgis入门(八)、Json与Geometry的相互转换

来源:互联网 发布:易达打印软件 编辑:程序博客网 时间:2024/06/08 17:54

在Android中json数据十分普遍,也很实用,在Arcgis中也同样支持Json数据,Json与Geometry可以相互转换,达到我们想要的数据。

一、Geometry转换成Json数据

这个实现十分简单,比如我们将一个点转换为Json,这时也同样用到GeometryEngine这个强大的类。

 Point point = new Point(113, 23);        String json = GeometryEngine.geometryToJson(SpatialReference.create(SpatialReference.WKID_WGS84), point);        Log.w("TAG", "json===" + json);

打印Log的结果为json==={"x":113.0,"y":23.0,"spatialReference":{"wkid":4326}},是不是很简单。

二、Json转换为Geometry

同样用到GeometryEngine类中的jsonToGeometry方法,我们将上面的json再转换回去。

     try {            String jsonStr = "{\"x\":113.0,\"y\":23.0,\"spatialReference\":{\"wkid\":4326}}";            JsonFactory jsonFactory = new JsonFactory();            JsonParser jsonParser = jsonFactory.createJsonParser(jsonStr);            MapGeometry mapGeometry = GeometryEngine.jsonToGeometry(jsonParser);            Point mPoint = (Point) mapGeometry.getGeometry();            Log.i("TAG","mPoint---"+mPoint.getX()+"==="+mPoint.getY());        } catch (IOException e) {            e.printStackTrace();        }

代码执行结果mPoint---113.0===23.0。注:jsonFactory.createJsonParser这个方法可带入的参数也是比较多的,比如:file、outputStream、byte数组等等,有兴趣的小伙伴可以研究研究。

0 0
原创粉丝点击