微信小程序调用百度地图api遇到的小坑之renderReverse参数

Posted liuxiaoddd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信小程序调用百度地图api遇到的小坑之renderReverse参数相关的知识,希望对你有一定的参考价值。

http://api.map.baidu.com/geocoder/v2/?ak=xxx&coordtype=wgs84ll&callback=renderReverse&location=37.58585,118.8889&output=xml&pois=1

用这个api解析得到的json值中会多出

renderReverse&&renderReverse("status":0,"result":"location":"lng":119.95955778200993,"lat":31.745123739675667,
renderReverse&&renderReverse这是什么鬼,拿到值很开心的用上json_decode,结果刷新查看返回NULL,竟然是空,不甘心的我以特么刷新了几遍,还是空,这是逗我吗?又跑去检查代码,代码没错啊。最后通过百度找到了正确的解决办法,原来是多了callback=renderReverse这个javascript函数名,去掉这个就行了。另一种方法是通过正则来匹配出json格式数据,

preg_match('/\\(.*\\)/', $str, $result);

var_dump($result);

第三办法是用str_replace和substr和strlen解决,这里就不给出具体的写法了,有需要的可以去百度一下用法。

renderReverse&&renderReverse("status":0,"result":"location":"lng":116.44354999999993,"lat":39.92190007648824,"formatted_address":"北京市朝阳区吉祥里胡同206","business":"朝阳门,建国门,建外大街","addressComponent":"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"北京市","city":"北京市","city_level":2,"district":"朝阳区","town":"","adcode":"110105","street":"吉祥里胡同","street_number":"206","direction":"附近","distance":"5","pois":["addr":"东二环雅宝路7号","cp":"","direction":"内","distance":"0","name":"国星大厦","poiType":"房地产","point":"x":116.4434086604886,"y":39.921593848478497,"tag":"房地产","tel":"","uid":"631c4157bb0c036130f71d94","zip":"","parent_poi":"name":"","tag":"","addr":"","point":"x":0.0,"y":0.0,"direction":"","distance":"","uid":"","addr":"北京市朝阳区朝外西街万邦商贸大厦东50米(雅宝里旁)","cp":" ","direction":"西","distance":"82","name":"南下坡清真寺","poiType":"旅游景点","point":"x":116.44427103377818,"y":39.92203653047836,"tag":"旅游景点;文物古迹","tel":"","uid":"a5097bfa38d65563f13cbabb","zip":"","parent_poi":"name":"","tag":"","addr":"","point":"x":0.0,"y":0.0,"direction":"","distance":"","uid":"","addr":"北京市朝阳区外交部南街10号","cp":" ","direction":"南","distance":"95","name":"万邦商贸大厦","poiType":"房地产","point":"x":116.4433637452131,"y":39.922548377944469,"tag":"房地产;写字楼","tel":"","uid":"8fa14fc3e016c86ef6eafc94","zip":"","parent_poi":"name":"","tag":"","addr":"","point":"x":0.0,"y":0.0,"direction":"","distance":"","uid":"","addr":"北京市朝阳区雅宝路6号","cp":" ","direction":"北","distance":"104","name":"雅宝大厦","poiType":"房地产","point":"x":116.44357035548039,"y":39.92117883148359,"tag":"房地产;写字楼","tel":"","uid":"680fb703eb66829c8ab98894","zip":"","parent_poi":"name":"","tag":"","addr":"","point":"x":0.0,"y":0.0,"direction":"","distance":"","uid":"","addr":"北京市朝阳区雅宝路甲5号","cp":" ","direction":"西","distance":"137","name":"吉利大厦(北京儿研所店)","poiType":"酒店","point":"x":116.44473815264337,"y":39.922161034271038,"tag":"酒店;星级酒店","tel":"","uid":"d24e48eb034c52adaeee7ae4","zip":"","parent_poi":"name":"","tag":"","addr":"","point":"x":0.0,"y":0.0,"direction":"","distance":"","uid":"","addr":"北京市朝阳区雅宝路5号","cp":" ","direction":"西","distance":"73","name":"雅宝红城","poiType":"购物","point":"x":116.44419018628229,"y":39.92178060529899,"tag":"购物;购物中心","tel":"","uid":"5e53af1a5f49335f4051c082","zip":"","parent_poi":"name":"","tag":"","addr":"","point":"x":0.0,"y":0.0,"direction":"","distance":"","uid":"","addr":"北京市朝阳区雅宝路2号","cp":" ","direction":"西北","distance":"170","name":"天雅大厦","poiType":"房地产","point":"x":116.44460340681687,"y":39.92104049192182,"tag":"房地产;写字楼","tel":"","uid":"f60a15ed3de18ef5ba17e789","zip":"","parent_poi":"name":"","tag":"","addr":"","point":"x":0.0,"y":0.0,"direction":"","distance":"","uid":"","addr":"北京市朝阳区雅宝路8号","cp":" ","direction":"东北","distance":"189","name":"亚太大厦","poiType":"酒店","point":"x":116.44260018552962,"y":39.920812231029028,"tag":"酒店;公寓式酒店","tel":"","uid":"dec8feba50f0f36749fc8d41","zip":"","parent_poi":"name":"","tag":"","addr":"","point":"x":0.0,"y":0.0,"direction":"","distance":"","uid":"","addr":"北京市朝阳区雅宝路8号亚太大厦一二层","cp":" ","direction":"东北","distance":"178","name":"中国银行(雅宝路支行)","poiType":"金融","point":"x":116.44270798219081,"y":39.920846816062098,"tag":"金融;银行","tel":"","uid":"3f3f0f874fe99c5c32870a00","zip":"","parent_poi":"name":"亚太大厦","tag":"酒店;公寓式酒店","addr":"北京市朝阳区雅宝路8号","point":"x":116.44260018552962,"y":39.920812231029028,"direction":"东北","distance":"189","uid":"dec8feba50f0f36749fc8d41","addr":"北京市朝阳区朝阳门南大街14号","cp":" ","direction":"东南","distance":"285","name":"FESCO北京外企人力资源服务有限公司(总部)","poiType":"公司企业","point":"x":116.44222289721543,"y":39.923592811485089,"tag":"公司企业;公司","tel":"","uid":"ca68d85fb58c6f07ee74f842","zip":"","parent_poi":"name":"","tag":"","addr":"","point":"x":0.0,"y":0.0,"direction":"","distance":"","uid":""],"roads":[],"poiRegions":["direction_desc":"内","name":"E园EPARK(雅宝路店)","tag":"公司企业;公司","uid":"3609eebad1bc8116b1077d5b"],"sematic_description":"E园EPARK(雅宝路店)内,国星大厦内0米","cityCode":131)
// pages/main/main.js
Page(

  /**
   * 页面的初始数据
   */
  data: 

  ,

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) 
    this.loadInfo();
    this.loadCity();
  ,

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() 

  ,

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() 

  ,

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() 

  ,

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() 

  ,

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() 

  ,

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() 

  ,

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() 

  ,
  loadInfo: function() 
    var page =this;
    wx.getLocation(
      type: 'gcj02',
      success(res) 
        const latitude = res.latitude;
        const longitude = res.longitude;
        page.loadCity(latitude, longitude);
      
    )
  ,
  loadCity: function(la, lo) 
    var page = this;
    wx.request(
      url: 'http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location='+la+','+lo + '&output=json&pois=1&ak=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
      header: 
        "Content-Type": "application/json"
      ,
      success: function(res) 

        console.log('http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=' + la + ',' + lo + '&output=json&pois=1&ak=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
        console.log(res);
        console.log(res.data);

      
    )
  
)

--------------------- 
作者:xiaolong20081 
来源:CSDN 
原文:https://blog.csdn.net/xiaolong20081/article/details/79604476 
版权声明:本文为博主原创文章,转载请附上博文链接!

以上是关于微信小程序调用百度地图api遇到的小坑之renderReverse参数的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序自动定位,通过百度地图根据经纬度获取该地点所在城市信息

微信小程序怎么破定位

微信小程序实现城市定位:获取当前所在的国家城市信息

微信小程序wx.getLocation()获取经纬度及JavaScript SDK调用腾讯地图API获取某一类地址

微信小程序中怎么开发MAP地图 MAP地图开发方法

微信小程序怎么打开地理位置 很急 谢谢!!!