百度地图api 跨域

百度地图调用参考:逆地理编码 rgc 反geo检索 | 百度地图API SDK

axios  ,jsonp 参考:使用VUE的axios解决调用百度地图api的跨域访问问题_樱小诺的博客-CSDN博客_百度地图api 跨域

直接请求会报跨域错误
        axios({
          url: "https://api.map.baidu.com/reverse_geocoding/v3/?ak=1hKv9tskwuH6cUZ4v7z1X2DDCpQdhGGw&output=json&coordtype=wgs84ll&location="+lat+","+lon,
          method: "get"
        }).then(res => {
          
          alert(res.result.formatted_address)
         
        });

改用 jsonp

 axios.jsonp = (url) => {
          if(!url){
              console.error('Axios.JSONP 至少需要一个url参数!')
              return;
          }
          return new Promise((resolve,reject) => {
              window.jsonCallBack =(result) => {
                  resolve(result)
              }
              var JSONP=document.createElement("script");
              JSONP.type="text/javascript";
              JSONP.src=`${url}&callback=jsonCallBack`;
              document.getElementsByTagName("head")[0].appendChild(JSONP);
              setTimeout(() => {
                  document.getElementsByTagName("head")[0].removeChild(JSONP)
              },500)
          })
        }

        axios.jsonp(
          "https://api.map.baidu.com/reverse_geocoding/v3/?ak=1hKv9tskwuH6cUZ4v7z1X2DDCpQdhGGw&output=json&coordtype=wgs84ll&location="+lat+","+lon,
        ).then(res => {
          
          alert(res.result.formatted_address)
         
        });

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值