首页
学习
活动
专区
圈层
工具
发布

Google地图API多标记单击动画

Google地图API多标记单击动画实现指南

基础概念

Google地图API中的标记(Marker)是地图上表示特定位置的图标。多标记单击动画是指在有多个标记的地图上,当用户单击某个标记时,该标记会执行特定的动画效果(如弹跳、颜色变化等),以提供视觉反馈。

实现方法

1. 基本实现

代码语言:txt
复制
// 初始化地图
function initMap() {
  const map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: {lat: 39.9042, lng: 116.4074} // 北京坐标
  });

  // 创建多个标记
  const locations = [
    {lat: 39.9042, lng: 116.4074, title: '北京'},
    {lat: 31.2304, lng: 121.4737, title: '上海'},
    {lat: 23.1291, lng: 113.2644, title: '广州'},
    {lat: 22.5431, lng: 114.0579, title: '深圳'}
  ];

  locations.forEach(location => {
    const marker = new google.maps.Marker({
      position: location,
      map: map,
      title: location.title
    });

    // 添加点击事件监听器
    marker.addListener('click', () => {
      animateMarker(marker);
    });
  });
}

// 标记动画函数
function animateMarker(marker) {
  // 停止之前的动画
  marker.setAnimation(null);
  
  // 开始弹跳动画
  marker.setAnimation(google.maps.Animation.BOUNCE);
  
  // 3秒后停止动画
  setTimeout(() => {
    marker.setAnimation(null);
  }, 3000);
}

2. 高级实现(带信息窗口和唯一动画)

代码语言:txt
复制
function initMap() {
  const map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: {lat: 39.9042, lng: 116.4074}
  });

  const infowindow = new google.maps.InfoWindow();
  let activeMarker = null;

  const locations = [
    {lat: 39.9042, lng: 116.4074, title: '北京', content: '中国首都'},
    {lat: 31.2304, lng: 121.4737, title: '上海', content: '中国经济中心'},
    {lat: 23.1291, lng: 113.2644, title: '广州', content: '广东省省会'},
    {lat: 22.5431, lng: 114.0579, title: '深圳', content: '科技创新城市'}
  ];

  locations.forEach(location => {
    const marker = new google.maps.Marker({
      position: location,
      map: map,
      title: location.title
    });

    marker.addListener('click', () => {
      // 如果已经有活动的标记,停止其动画
      if (activeMarker) {
        activeMarker.setAnimation(null);
      }
      
      // 设置当前标记为活动标记
      activeMarker = marker;
      
      // 显示信息窗口
      infowindow.setContent(`<h3>${location.title}</h3><p>${location.content}</p>`);
      infowindow.open(map, marker);
      
      // 执行动画
      animateMarker(marker);
    });
  });
}

function animateMarker(marker) {
  // 弹跳动画
  marker.setAnimation(google.maps.Animation.BOUNCE);
  
  // 3秒后停止动画
  setTimeout(() => {
    marker.setAnimation(null);
  }, 3000);
}

动画类型

Google地图API提供了几种内置动画:

  1. google.maps.Animation.DROP - 标记从顶部掉落
  2. google.maps.Animation.BOUNCE - 标记弹跳

你也可以创建自定义动画:

代码语言:txt
复制
// 自定义旋转动画
function rotateMarker(marker, angle) {
  const icon = marker.getIcon();
  if (icon) {
    icon.rotation = angle;
    marker.setIcon(icon);
  }
}

// 使用自定义动画
let rotationAngle = 0;
const rotationInterval = setInterval(() => {
  rotationAngle = (rotationAngle + 10) % 360;
  rotateMarker(marker, rotationAngle);
}, 50);

// 停止动画
setTimeout(() => {
  clearInterval(rotationInterval);
}, 3000);

常见问题及解决方案

1. 动画不工作

  • 原因:可能没有正确加载Google Maps API动画库
  • 解决:确保API加载时包含libraries=visualization参数
  • 解决:确保API加载时包含libraries=visualization参数

2. 多个标记同时动画

  • 原因:没有管理前一个标记的动画状态
  • 解决:使用变量跟踪当前活动标记,并在新标记动画前停止旧标记动画

3. 性能问题

  • 原因:同时动画太多标记
  • 解决:限制同时动画的标记数量,或使用更简单的动画效果

应用场景

  1. 旅游地图:突出显示用户点击的景点
  2. 物流追踪:显示当前选中的配送点
  3. 房地产应用:突出显示选中的房源
  4. 紧急服务:标记紧急事件位置
  5. 社交应用:显示朋友所在位置

最佳实践

  1. 动画持续时间不宜过长(通常2-3秒)
  2. 提供视觉反馈的同时不要过度干扰用户
  3. 考虑移动设备上的性能影响
  4. 对于密集标记区域,使用更微妙的动画效果
  5. 确保动画效果与整体应用风格一致
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的文章

领券