内网走nginx代理访问高德webapi2.0

需求:客户的电脑都只能访问内,服务器可以访问外网,客户电脑使用的项目中用到了高德webapi2.0。
10.200.31.45:32100是我们的web服务器。

网上基本上都是对高德webapi1.4的配置方式,而web2.0有一些差别。

1.前端修改高德地图的js应用

如果是index.html引入,

修改之前的应用:

    <script type="text/javascript" crossorigin
        src="https://webapi.amap.com/maps?v=2.0&key=高德key&plugin=AMap.ToolBar, AMap.ControlBar,AMap.MoveAnimation,AMap.RangingTool,AMap.Geocoder"></script>
    <script src="https://webapi.amap.com/loca?v=2.0.0&key=高德key"></script>

修改之后的配置:

    <script type="text/javascript" crossorigin
    src="<%= BASE_URL %>maps?v=2.0&key=高德key&plugin=AMap.ToolBar, AMap.ControlBar,AMap.MoveAnimation,AMap.RangingTool,AMap.Geocoder"></script>
<script src="<%= BASE_URL %>loca?v=2.0.0&key=高德key"></script>

如果是js动态加载,代码如下:

/**
 * 创建高德秘钥
 * @param secret 
 */
export function createSecret(secret?:string){
         secret=secret||'高德秘钥';
         // 拼装脚本字符串
         const script = document.createElement('script');
          // 高德 Web API 初始化配置
         script.innerHTML = `
           window._AMapSecurityConfig = {
            securityJsCode: '${secret}',
          };`;
         // 将脚本插入到页面头部
         document.head.appendChild(script);
}
//获取地图地址
const getMapUrl=()=>{
  const {map_offline}=Local.getAmapConfig();
  let mapUrl='https://webapi.amap.com';
   if(map_offline){
    mapUrl='';
   }
  if (process.env.NODE_ENV === "development"){
    mapUrl='https://webapi.amap.com';
  }
  return mapUrl;
}
/**
 * 创建高德script引入脚本
 * @param key 
 * @param url 
 */
export function loadAMapScript(url:string,key?:string) {
  key=key||'高德key';
  const mapUrl=getMapUrl();
  const src =`${mapUrl}/maps?v=2.0&key=${key}${url}`;
  console.log('我的src',src);
  return new Promise((resolve, reject) => {
    const script = document.createElement('script');
    script.src = src;
    script.async = true;
    script.onerror = reject;
    script.onload = resolve;
    document.head.appendChild(script);
  });
}
/**
 * 创建高德local script引入脚
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邹琼俊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值