码迷,mamicode.com
首页 > Web开发 > 详细

LBS上传到百度地图

时间:2016-01-24 11:42:31      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

准备:

第一步:注册百度地图开发者账号

第二步:使用开发者账号注册相对应秘钥

在LBS后台添加所需要上传的字段

1,添加WebClientHelper帮助类

2,LBS帮助类

  private string ak = "WsjYicnWeleiuu8bmAYLy08m";//用户的访问权限key      

   private string petLocationId = "80305";//(位置数据表的标示)    

  private string petHospitalId = "90736";//(位置数据表的标示)

 WebClientHelper clientHelper = new WebClientHelper();       

  /// <summary> /// 生成文件上传到LBS(丢失、拾获、领养)/// </summary>        

/// <param name="title">公告标题</param>        

/// <param name="lng">经度</param>       

  /// <param name="lat">纬度</param>       

  /// <param name="address">地址(标准的地址信息,如北京市XX区XX街(路)XX号,或名胜古迹、标志性建筑物,或“XX路与XX路交叉口”描述方式)</param>         /// <param name="petpicture">图片(互联网图片)</param>        

 /// <param name="sex">性别</param>      

 /// <param name="noticedate">日期(或发布日期):2014-03-20</param>        

/// <param name="state">状态标记:1,2,3,4</param>        

/// <param name="filePath">上传文件的存放路径:写成Server.MapPath("/tempfile/"),存放到服务器</param>        

/// <param name="customPoiId">自定义id(便于更新LBS里的数据信息)</param>        

/// <returns></returns>        

public bool UploadLoacationToLBS(string title, string lng, string lat, string address, string sex, string noticedate, int state, string filePath, string customPoiId)       

  {          

   if (string.IsNullOrEmpty(lng) || string.IsNullOrEmpty(lat))     

        {          

       LogHelper.WriteLog("地址解析失败:经纬度为空");        

         return false;        

     }          

   string fileName = string.Format("{0:yyyy-MM-dd}", DateTime.Now).Replace("-", "") + RandomCodeHelper.GetNumberRandomCode(4) + ".csv";             string fullPath = filePath + fileName;     //重置csv文件名称

        if (File.Exists(fullPath))        //判断该csv文件名称是否重复

     {            

     File.Delete(fullPath);    //删除重复文件   

      }             try             {       

       //将内容写入csv文件,存入服务器

          FileStream fs = new FileStream(fullPath, FileMode.Create, FileAccess.Write);    

             StreamWriter sw = new StreamWriter(fs, Encoding.Default);      

           string fieldTilte = "title,address,longitude,latitude,coord_type,,CustomPoiId,state,noticedate,sex,variety,feature,petpicture";   

              sw.WriteLine(fieldTilte);             

    //sw.WriteLine(title + "," + address + "," + lng + "," + lat + ",1,," + petpicture + "," + sex + "," + noticedate + "," + state);             

    sw.WriteLine(title + "," + address + "," + lng + "," + lat + ",1,," + customPoiId + "," + state + "," + noticedate + "," + sex +  "," + petpicture);          

       sw.Close();              

      fs.Close();          

       return UploadLoacationToLBS(fullPath, petLocationId); //文件内容写入完成,上传到LBS方法      

      }             catch (Exception ex)             {       

          LogHelper.WriteLog("上传文件到LBS出错:", ex);         

        return false;         

    }

        }

----------------------------------------------------------------------LBS上传坐标------------------------------------------------------------------------------------------

  /// <summary>
        /// 上传数据文件
        /// </summary>
        /// <param name="fullPath">文件路径</param>
        /// <param name="geotableId">位置数据表的标示(id)</param>
        /// <returns></returns>
        public bool UploadLoacationToLBS(string fullPath, string geotableId)
        {
            string result = clientHelper.UploadFile("http://api.map.baidu.com/geodata/v3/poi/upload", "ak=" + ak + "&geotable_id=" + geotableId + "", "poi_list=" + fullPath);
            LogHelper.WriteLog("上传到LBS返回值记录:" + result);
            if (result.Contains("\"status\": 0,"))
            {
                //File.Delete(fullPath);
                return true;
            }
            else
            {
                return false;
            }
        }

 

注:上传会有延迟,目前用后台异步解决,各位大神多多指教

 

LBS上传到百度地图

标签:

原文地址:http://www.cnblogs.com/LOVEJIEYING/p/5154771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!