2つのアドレス間の距離を取得する(高徳API)

4267 ワード

 string startLonLat = SiteHelper.GetLonLat("  "); //         
 string endLonLat = SiteHelper.GetLonLat("  "); //         
 int distance = SiteHelper.GetDistance(startLonLat, endLonLat); //  2      (    )
        /// 
        ///       
        /// 
        ///   
        /// 
        public static string GetLonLat(string address)
        {
            #region       
            //{
            //    "status":"1",
            //    "info":"OK",
            //    "infocode":"10000",
            //    "count":"1",
            //    "geocodes":[
            //        {
            //            "formatted_address":"         ",
            //            "province":"   ",
            //            "citycode":"0571",
            //            "city":"   ",
            //            "district":"   ",
            //            "township":Array[0],
            //            "neighborhood":Object{...},
            //            "building":Object{...},
            //            "adcode":"330106",
            //            "street":Array[0],
            //            "number":Array[0],
            //            "location":"120.130203,30.259324",
            //            "level":"  "
            //        }
            //    ]
            //}

            //{
            //    "status":"0",
            //    "info":"INVALID_USER_KEY",
            //    "infocode":"10001"
            //}
            #endregion

            string queryUrl = "http://restapi.amap.com/v3/geocode/geo?key=6119e85de0fa6a97be90a0af41f0613c7&address=" + address; //    
            string queryResult = Utils.HttpGet(queryUrl); //      
            string location = string.Empty; //    
            JsonData jd = JsonMapper.ToObject(queryResult);//      json  
            int status = Utils.ObjToInt(jd["status"], 0);//     1:   0:   
            if (status == 1) //  
            {
                JsonData list = jd["geocodes"];
                if (list.Count > 0)
                {
                    location = Utils.ObjectToStr(list[0]["location"]); //      
                }
            }
            return location;
        }
	/// 
        ///   2      
        /// 
        ///        
        ///        
        /// 
        public static int GetDistance(string startLonLat, string endLonLat)
        {
            #region       
            //{
            //    "status":"1",
            //    "info":"OK",
            //    "infocode":"10000",
            //    "results":[
            //        {
            //            "origin_id":"1",
            //            "dest_id":"1",
            //            "distance":"936631",
            //            "duration":"37140"
            //        }
            //    ]
            //}
            #endregion

            int duration = 0;  //            
            string queryUrl = "http://restapi.amap.com/v3/distance?key=6119e85defa6a97be090a0af41f0613c7&origins=" + startLonLat + "&destination=" + endLonLat; //    
            string queryResult = Utils.HttpGet(queryUrl); //      
            JsonData jd = JsonMapper.ToObject(queryResult);//      json  
            int status = Utils.ObjToInt(jd["status"], 0);//     1:   0:   
            if (status == 1) //  
            {
                JsonData list = jd["results"];
                if (list.Count > 0)
                {
                    duration = Convert.ToInt32(Convert.ToInt64(list[0]["distance"].ToString()) / 1000);
                }
            }
            return duration;
        }