高德地图、谷歌地图的图层切换

针对业务需求,需要在 web 端、H5 显示高德地图与谷歌地图的底图图层切换;

  1. 可找到高德地图的底图图层切换 API,高德地图 API 只有 1.4 版本以及以下版本才能设置英文底图;
  • 先在 index.html 页面引入高德地图的 API 地址
<script src="https://webapi.amap.com/maps?v=1.4.15&key=xxxxxxxxxxxxx&&plugin=AMap.Scale,AMap.OverView,AMap.MarkerCluster,AMap.ToolBar,AMap.PolyEditor,AMap.MouseTool,AMap.MapType,AMap.RangingTool,AMap.CitySearch,AMap.Weather,AMap.DistrictSearch,AMap.Geocoder"></script>
  • 高德地图的功能代码
const langStr = g_i18n.global.locale.value === 'en' ? 'en' : 'zh_cn'
// 初始化地图
const map = new AMap.Map('map-wrap', {
  resizeEnable: true,
  expandZoomRange: true,
  center: [114.03281, 22.612002], // 深圳市龙华区
  zoom: 15,
  zooms: [3, 20],
  mapStyle: 'amap://styles/fresh',
  lang: langStr
})
initMapLayer()

function initMapLayer() {
  map &&
    map.setLayers([
      props.mapType === 'AMAP' ? getAMapLayer() : getGoogleMapLayer()
    ])
}

function getAMapLayer() {
  return props.enableSatellite
    ? // 卫星地图
      new AMap.TileLayer.Satellite({
        zIndex: 1
      })
    : // 普通地图
      new AMap.TileLayer({
        zIndex: 1
      })
}
  1. 谷歌官方提供的图层是存在访问限制的,需要通过服务器代理访问,以下是谷歌地图的底图图层切换代码
function getGoogleMapLayer() {
  /**
   * Google 的切片图层中,lyrs 参数可以是
   * h = 路网
   * m = 标准地图
   * p = 地形图
   * r = 某种改变的地图
   * s = 卫星图(无路网)
   * t = 地形图(无路网)
   * y = 混合地图(带路网的卫星图)
   */
  const lyrs = props.enableSatellite ? 's' : 'm'
  const hl = g_i18n.global.locale.value === 'en' ? 'en-us' : 'zh-CN'
  return new AMap.TileLayer({
    tileUrl: `https://googlemap.xxxxx.com/maps/vt/lyrs=${lyrs}@142&hl=${hl}&gl=cn&x=[x]&y=[y]&z=[z]&s=Galile`,
    // tileUrl: `http://www.google.com/maps/vt/lyrs=${lyrs}@142&hl=${hl}&gl=cn&x=[x]&y=[y]&z=[z]&s=Galile`,
    zIndex: 1
  })
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值