Silverlight 에 제 이 슨 을 Geometry 로 해석 하 는 방법 을 제공 합 니 다.

  SOE     ,               Json   ,  Silverlight                  ,         ,          ,      Json     Silverlight  Geometry    ,    ,     。
 /// <summary>
        ///     json   Geometry,       M Z,    ,     。 ArcObjects Geometry  json        。
        ///   :  
        ///   2012 
        /// </summary>
        /// <param name="jsonResponse"></param>
        /// <returns></returns>

        private ESRI.ArcGIS.Client.Geometry.Geometry ParsefromJson(string jsonResponse)
        {

            JsonObject jsonObject = JsonObject.Parse(jsonResponse.ToString()) as JsonObject;
            SpatialReference pSpatial = new SpatialReference();
            ESRI.ArcGIS.Client.Geometry.Geometry pGeo = null;

            if (jsonObject.ContainsKey("geometries"))
            {

               
                JsonObject jsonObjectGeo = jsonObject["geometries"] as JsonObject;
                //      
                if (jsonObjectGeo.ContainsKey("spatialReference"))
                {
                    pSpatial = this.myMap.SpatialReference;

                   
                 //JsonObject pSpatialJson =jsonObjectGeo["spatialReference"] as JsonObject;

                 //                          
                }
                //     ,   hasz hasM
                if (jsonObjectGeo.ContainsKey("points"))
                {
                    JsonValue JsonPoints = jsonObjectGeo["points"];

                    if (JsonPoints is JsonArray)
                    {
                        if (JsonPoints.Count == 1)
                        {
                            MapPoint pPoint = new MapPoint();

                            //     

                            string[] pStrPoints = JsonPoints[0].ToString().Substring(1, JsonPoints[0].ToString().Length - 2).Split(',');

                            pPoint.X = Convert.ToDouble(pStrPoints[0]);
                            pPoint.Y = Convert.ToDouble(pStrPoints[1]);

                            pGeo = pPoint;
                          

                        }
              
                    }
                }
                else if (jsonObjectGeo.ContainsKey("paths"))
                {
                    JsonValue JsonPoints = jsonObjectGeo["paths"];

                    ESRI.ArcGIS.Client.Geometry.Polyline pPolyline = new ESRI.ArcGIS.Client.Geometry.Polyline();


                    ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection> pPointCollection = new ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection>();
                    // pPolyline.Paths

                    if (JsonPoints is JsonArray)
                    {
                        for (int i = 0; i < JsonPoints.Count; i++)
                        {
                            if (JsonPoints[i] is JsonArray)
                            {
                                ESRI.ArcGIS.Client.Geometry.PointCollection pPointCollections = new ESRI.ArcGIS.Client.Geometry.PointCollection();

                                JsonArray pInnerPoints = JsonPoints[i] as JsonArray;
                                for (int j = 0; j < pInnerPoints.Count; j++)
                                {
                                    string pStr = pInnerPoints[j].ToString();

                                    string[] pStrPoints = pInnerPoints[j].ToString().Substring(1, pInnerPoints[j].ToString().Length - 2).Split(',');
                                    MapPoint pPoint = new MapPoint();
                                    pPoint.X = Convert.ToDouble(pStrPoints[0]);
                                    pPoint.Y = Convert.ToDouble(pStrPoints[1]);

                                    pPointCollections.Add(pPoint);
                                }

                                pPointCollection.Add(pPointCollections);

                            }
                        }

                        pPolyline.Paths = pPointCollection;

                        pGeo = pPolyline;
                    }
                }
                else if (jsonObjectGeo.ContainsKey("rings"))
                {
                     JsonValue JsonPoints = jsonObjectGeo["rings"];

                ESRI.ArcGIS.Client.Geometry.Polygon pPolygon = new ESRI.ArcGIS.Client.Geometry.Polygon();



                ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection> pPointCollection = new ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection>();
   

                if (JsonPoints is JsonArray)
                {
                    for (int i = 0; i < JsonPoints.Count; i++)
                    {
                        if (JsonPoints[i] is JsonArray)
                        {
                            ESRI.ArcGIS.Client.Geometry.PointCollection pPointCollections = new ESRI.ArcGIS.Client.Geometry.PointCollection();

                            JsonArray pInnerPoints = JsonPoints[i] as JsonArray;
                            for (int j = 0; j < pInnerPoints.Count; j++)
                            {
                                string pStr = pInnerPoints[j].ToString();

                                string[] pStrPoints = pInnerPoints[j].ToString().Substring(1, pInnerPoints[j].ToString().Length - 2).Split(',');
                                MapPoint pPoint = new MapPoint();
                                pPoint.X = Convert.ToDouble(pStrPoints[0]);
                                pPoint.Y = Convert.ToDouble(pStrPoints[1]);

                                pPointCollections.Add(pPoint);
                            }

                            pPointCollection.Add(pPointCollections);

                        }
                    }

                    pPolygon.Rings= pPointCollection;

                    pGeo = pPolygon;

                }
                }
            }



           pGeo.SpatialReference = pSpatial;

            return pGeo;
        }

좋은 웹페이지 즐겨찾기