• Android GIS开发系列-- 入门季(8) Json与Geometry的相互转换


    在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数组等等,有兴趣的小伙伴可以研究研究。





  • 相关阅读:
    node 使用笔记
    体会 git 之优越性
    Notes for building gimp-print
    Selenium Books
    Using MongoDB in C#
    Learning coding online
    USING NHIBERNATE WITH MySQL
    Data Visualization Books
    Web Automation with Selenium (C#)
    Gearman In Action
  • 原文地址:https://www.cnblogs.com/arxive/p/7751932.html
Copyright © 2020-2023  润新知