Zoe
using MPS.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AIMSTools.Application.Helper
{
   public  static  class CallGADInterfaceHelper
    {
        /// <summary>
        /// 调用高德地理接口
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        public  static  string   GetGaoDeAreaCode(string address)
        {
            var geoParam = new GeoParam() { Key = "c01bebef144b090a9a9b41f19d031039", Address = address };

            var param = new List<string>();
            if (geoParam != null)
            {
                foreach (var item in geoParam.GetType().GetProperties())
                {
                    var value = item.Name.ToLower() + "=" + item.GetValue(geoParam);
                    param.Add(value);
                }
            }
            var content = string.Join("&", param);
            var geoParamStr = content;
            var geoJsonResult = HttpResquestHelper.RequestData("http://restapi.amap.com/v3/geocode/geo ", geoParamStr);
            //已经拿到数据,不需要下面的重复拿去数据
            //解析json数据
            return DeserializeObject(geoJsonResult);

        }

        //=======================================================================
        /// <summary>
        /// 调用高德-逆-地理接口
        /// </summary>
        /// <param name="location"></param>
        /// <returns></returns>
      public   static string NGetGaoDeAreaCode(string location)
        {
            var geoParamn = new GeoParamN() { Key = "c01bebef144b090a9a9b41f19d031039", Location = location, poitype = "", radius = 1000, extensions = "all", batch = true, roadlevel = 0 };
            var param = new List<string>();
            if (geoParamn != null)
            {
                foreach (var item in geoParamn.GetType().GetProperties())
                {
                    var value = item.Name.ToLower() + "=" + item.GetValue(geoParamn);
                    param.Add(value);
                }
            }
            var content = string.Join("&", param);
            var ngeoParamStr = content;
            var ngeoJsonResult = HttpResquestHelper.RequestData("http://restapi.amap.com/v3/geocode/regeo", ngeoParamStr);
            //return ngeoJsonResult;
            return NDeserializeObject(ngeoJsonResult);
        }


//===============================================================

        //解析jsonobject 到GodeAreaCodeResult  (高德地理接口调用解析)
        public static string DeserializeObject(string JsonData)
        {


            // GaoDeAreaCode jobInfoList = JsonConvert.DeserializeObject<GaoDeAreaCode>(JsonData);

            JObject jo = (JObject)JsonConvert.DeserializeObject(JsonData);
            JArray jarray = null;
            if (jo["status"].ToString().Equals("1") && !jo["count"].Equals("0"))
            {

                jarray = (JArray)JsonConvert.DeserializeObject(jo["geocodes"].ToString());

            }
            try
            {
                if (jarray != null)

                { return jarray[0]["adcode"].ToString(); }
                else
                {
                    return "";
                }
            }
            catch (Exception xx)
            { return ""; }
        }

        //===========================================================================

        //解析jsonobject 到NGodeAreaCodeResult  (高德逆地理接口调用解析)
        public static string NDeserializeObject(string JsonData)
        {
            // GaoDeAreaCode jobInfoList = JsonConvert.DeserializeObject<GaoDeAreaCode>(JsonData);
            //NDGaoDeRet ret =JsonConvert.DeserializeObject<NDGaoDeRet>(JsonData);
            JObject jo = (JObject)JsonConvert.DeserializeObject(JsonData);
            
            string adcode = "";
           
            //现在regeocode 的内容是  把中间去掉  { } 就是这个所以这里应该是 joobect
            //然后现在取 adcode
            if (jo["status"].ToString().Equals("1") && jo["info"].ToString().Equals("OK"))
            {

                JObject regeocode = (JObject)JsonConvert.DeserializeObject(jo["regeocode"].ToString());
                JObject addressComponent = (JObject)JsonConvert.DeserializeObject(regeocode["addressComponent"].ToString());
                adcode = addressComponent["adcode"].ToString();
            }
            try
            { 
                if (adcode != null)
                {

                    return adcode;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception xx)
            { return ""; }
        }

        //=========================================================================================
        public class GeoParam
        {
            public string Key { get; set; }


            public string Address { get; set; }
        }

        public class GeoParamN
        {
            public string Key { get; set; }

            public string Location { get; set; }

            public string poitype { get; set; }

            public double radius { get; set; }

            public string extensions { get; set; }
            public bool batch { get; set; }
            public int roadlevel { get; set; }

        }
    }
}

 

posted on 2018-01-16 15:07  口袋里的SKY  阅读(4015)  评论(0编辑  收藏  举报