百度地图帮助类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了百度地图帮助类相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Web;

namespace Test.Common
{
  public  class BaiDuMapHelper
  {
      public static string apiKey = "";
      public static string output = "json";
        /// <summary>
        /// 原始坐标转成百度坐标
        /// </summary>
        /// <param name="loc"></param>
        /// <returns></returns>
        public static string Geoconv(string lat, string lng)
        {
            return HttpRequestHelper.HttpGet(string.Format("http://api.map.baidu.com/geoconv/v1/?coords={0},{1}&from=1&to=5&ak={2}", lng, lat, apiKey));
        }
        /// <summary>
        /// 根据百度坐标取地址
        /// </summary>
        /// <param name="Loc"></param>
        /// <returns></returns>
        public static string Geocoding(string lat, string lng)
        {
            return HttpRequestHelper.HttpGet(string.Format("http://api.map.baidu.com/geocoder/v2/?ak={2}&location={0},{1}&output=json&pois=1", lat, lng, apiKey));
        }
        /// <summary>
        /// 根据GPS坐标取地址
        /// </summary>
        /// <param name="lat"></param>
        /// <param name="lng"></param>
        /// <returns></returns>
        public static string GetAddressByGps(string lat, string lng)
        {
            try
            {
                var loc =JsonConvert.DeserializeObject<dynamic>(Geoconv(lat, lng));
                if (loc["status"] != 0)
                {
                    return "";
                }
                string x = loc["result"][0]["x"];
                string y = loc["result"][0]["y"];
                return Geocoding(y, x);
            }
            catch (Exception e)
            {
                return "";
            }
        }
     
        /// <summary>
        /// 百度POI检索
        /// </summary>
        /// <returns></returns>
        public static string PoiSearch(string Key, string cityname)
        {
            try
            {
            
                string apiUrl = "http://api.map.baidu.com/place/v2/search?q={0}&region={1}&output={2}&ak={3}";

                apiUrl = string.Format(apiUrl, Key, cityname, output, apiKey);
                string result = string.Empty;
                result = Common.OtherFunHelper.HttpGet(apiUrl, "utf-8");
                return result;

            }
            catch
            {
                return ErrMsg(-100, "百度请求数据失败");
            }
        }
        /// <summary>
        /// 百度导航测距
        /// </summary>
        /// <returns></returns>
        public static string RouteMatrix(string start, string end, string mode)
        {
 
            try
            {
                string apiUrl = "http://api.map.baidu.com/direction/v1/routematrix?origins={0}&destinations={1}&output={2}&mode={3}&ak={4}";
                apiUrl = string.Format(apiUrl, start, end, output, mode, apiKey);
                string result = string.Empty;
                result = Common.OtherFunHelper.HttpGet(apiUrl, "utf-8");
                dynamic dy = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(result);
                int distance = 0;
                if (dy != null && dy.status == 0)
                {
                    distance = dy.result.elements[0].distance.value;
                }
                if (distance == 0)
                {
                    string[] s1 = start.Split(new char[] { , }, StringSplitOptions.RemoveEmptyEntries);
                    string[] z1 = end.Split(new char[] { , }, StringSplitOptions.RemoveEmptyEntries);
                    double lng1 = double.Parse(s1[1]);
                    double lat1 = double.Parse(s1[0]);
                    double lng2 = double.Parse(z1[1]);
                    double lat2 = double.Parse(z1[0]);
                    distance = (int)(Common.CommonFunc.GetDistance(lng1, lat1, lng2, lat2) * 1.414);
                }
                return ErrMsg(0, distance.ToString());
            }
            catch
            {
                return ErrMsg(-100, "距离计算失败");
            }
        }
        /// <summary>
        /// 百度坐标进行转化
        /// </summary>
        /// <returns></returns>
        public static string GeoConv(string xy)
        {
            try
            {
              
                string apiUrl = "http://api.map.baidu.com/geoconv/v1/?coords={0}&from=1&to=5&ak={1}";
                apiUrl = string.Format(apiUrl, xy, apiKey);
                string result = string.Empty;
                result = Common.OtherFunHelper.HttpGet(apiUrl, "utf-8");
                return result;

            }
            catch
            {
                return ErrMsg(-100, "百度请求数据失败");
            }
        }
        /// <summary>
        /// 百度坐标进行转化
        /// </summary>
        /// <returns></returns>
        public static string Geocoder(string xy)
        {
            try
            {
                
                string apiUrl = "http://api.map.baidu.com/geocoder/v2/?location={0}&output={1}&pois=0&ak={2}";
                apiUrl = string.Format(apiUrl, xy, output, apiKey);
                string result = string.Empty;
                result = Common.OtherFunHelper.HttpGet(apiUrl, "utf-8");
                return result;

            }
            catch
            {
                return ErrMsg(-100, "百度请求数据失败");
            }
        }
        /// <summary>
        /// GPS坐标转为百度坐标并返回地址
        /// </summary>
        /// <returns></returns>
        public static string ConvGeocoder(string xy)
        {
            try
            {
       
                string apiUrl = "http://api.map.baidu.com/geocoder/v2/?location={0}&output={1}&pois=0&coordtype=wgs84ll&ak={2}";
                apiUrl = string.Format(apiUrl, xy, output, apiKey);
                string result = string.Empty;
                result = Common.OtherFunHelper.HttpGet(apiUrl, "utf-8");
                return result;

            }
            catch
            {
                return ErrMsg(-100, "百度请求数据失败");
            }
        }
        public static string ErrMsg(int Status, string Message)
        {
            //String str = "{\"status\":{0},\"message\":\"{1}\"}";
            return "{\"status\":" + Status + ",\"message\":\"" + Message + "\"}";
        }

    }
}

 

以上是关于百度地图帮助类的主要内容,如果未能解决你的问题,请参考以下文章

带有标记的Android谷歌地图片段

百度地图_api

谷歌地图的“错误膨胀类片段”

百度地图API 判断点是否在圆形内

谷歌地图在Android的标签片段上显示为灰色

[工具向]__申请,下载,使用百度地图api