新版高德地图首页上下滑动实现

       新版的高德APP首页布局作了较大的调整,显示如下:,一共两个部分,滑动时搜索框可以停留在上中下三个位置,默认中部,可以上下滑动。

        我自己的实现效果如下:,页面布局结构大致如下:

<div id="map_z"></div>

<div id="search1" class="search1" @touchstart.capture="touchStart" @touchend.capture="touchEnd"></div>

<ul id="data1"> </div>

 

       分为三个部分,地图模块、搜索框模块和数据列表模块,思路是上下滑动时控制各个模块的高度和margin-top来实现滑动效果,主要是用到了vue封装的touch事件。

 

首先,给search1添加touch捕获事件touchstart和touchend:

<div id="search1" class="search1" @touchstart.capture="touchStart" @touchend.capture="touchEnd"></div>

 

然后,事件函数如下:

事件函数:

// 滑动开始

touchStart(e) {

// 记录初始位置

this.startY = e.touches[0].clientY;

// console.log(this.startY);

//禁止数据列表滚动

// e.preventDefault();

console.log(e);

},

 

// 滑动结束

touchEnd(e) {

// 当前滑动的父级元素

// 记录结束位置

this.endY = e.changedTouches[0].clientY;

// console.log(e);

var moveY = this.endY - this.startY;

// 设置search1.marginTop顶部/中部/底部的值分别是0/48/90vh,高度为7vh

var marginTop1 = 0;

var marginTop2 = 48;

var marginTop3 = 90;

var search1Height = 7;

search1Move(moveY, marginTop1, marginTop2, marginTop3, search1Height);

},

//滑动事件

function search1Move(moveY, marginTop1, marginTop2, marginTop3, search1Height) {

var map_z = document.getElementById("map_z").style;

var search1 = document.getElementById("search1").style;

var data1 = document.getElementById("data1").style;

var myLocation = document.getElementById("myLocation").style;

var myDistance = document.getElementById("myDistance").style;

 

// 设置过渡效果

map_z.transition = 'height 0.4s';

search1.transition = 'margin-top 0.4s';

data1.transition = 'margin-top 0.4s';

myLocation.transition = 'top 0.4s';

myDistance.transition = 'top 0.4s';

 

//中部下滑

if (moveY > 0 && (search1.marginTop == marginTop2 + 'vh')) {

map_z.height = marginTop3 + 'vh';

search1.marginTop = marginTop3 + 'vh';

// data1.marginTop = '100vh';

//不用data1.display = 'none'而用data1.marginTop = '100vh'会触发data1的滚动事件(列表项上移)

data1.display = 'none';

myLocation.top = marginTop3 - search1Height + 1 + 'vh';//字符串/数字?

myDistance.top = marginTop3 - search1Height + 'vh';

}

//中部上滑

else if (moveY < 0 && (search1.marginTop == marginTop2 + 'vh')) {

map_z.height = marginTop1 + 'vh';

// map_z.display = 'none';

search1.marginTop = marginTop1 + 'vh';

data1.display = 'block';

data1.marginTop = search1Height + 'vh';

myLocation.top = '-10vh';

myDistance.top = '-10vh';

}

//顶部下滑

else if (moveY > 0 && (search1.marginTop == marginTop1 + 'vh')) {

// map_z.display = 'block';

map_z.height = marginTop2 + 'vh';

search1.marginTop = marginTop2 + 'vh';

data1.marginTop = marginTop2 + search1Height + 'vh';

myLocation.top = marginTop2 - search1Height + 1 + 'vh';

myDistance.top = marginTop2 - search1Height + 'vh';

}

//底部上划

else if (moveY < 0 && (search1.marginTop == marginTop3 + 'vh')) {

map_z.height = marginTop2 + 'vh';

search1.marginTop = marginTop2 + 'vh';

data1.display = 'block';

data1.marginTop = marginTop2 + search1Height + 'vh';

myLocation.top = marginTop2 - search1Height + 1 + 'vh';

myDistance.top = marginTop2 - search1Height + 'vh';

} else {

// console.log(search1.style.marginTop);

}

}

 

下方数据列表会在search1上划时跟着滑动,响应事件冲突,解决方法:

/* 参考地图中leaflet样式,点击地图没有滑动 */

touch-action: none;

 

  • 8
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Vue高德地图定位按钮功能的实现可以分为以下几个步骤: 1. 引入高德地图的JavaScript SDK库。在Vue的项目中,可以通过npm安装amap-jsapi-loader库,并在入口文件(如main.js)中导入并使用该库加载高德地图的JS API。 2. 在Vue组件中创建地图容器。可以使用AMap.Map()方法创建一个地图的实例并指定容器的id,例如:new AMap.Map('map-container')。 3. 创建定位按钮并绑定相应的点击事件。在Vue组件的template中添加一个按钮元素,并为其添加一个@click事件监听器,用来触发定位功能的实现。 4. 实现定位功能。在点击定位按钮时,触发点击事件的处理函数,在该函数中调用高德地图的定位服务API,如AMap.Geolocation()来获取当前位置的经纬度。 5. 将定位的经纬度设置为地图的中心点,并在地图上显示一个标记点。可以使用AMap.Marker()创建一个标记点实例,然后设置其position为定位的经纬度,并添加到地图上。 6. 可以根据需要,将经纬度转换为具体的地址信息,并在页面中显示。可以使用AMap.Geocoder()的逆地理编码功能,将经纬度转换为地址。 具体代码示例: ```vue <template> <div> <button @click="getLocation">定位</button> <div id="map-container"></div> <div>{{location}}</div> </div> </template> <script> import AMapLoader from 'amap-jsapi-loader'; export default { data() { return { map: null, location: '' } }, mounted() { AMapLoader.load({ key: 'your-amap-api-key', version: '2.0' }).then(() => { this.map = new AMap.Map('map-container'); }); }, methods: { getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition((position) => { const { latitude, longitude } = position.coords; this.map.setCenter([longitude, latitude]); new AMap.Marker({ position: [longitude, latitude], map: this.map }); const geocoder = new AMap.Geocoder(); geocoder.getAddress([longitude, latitude]).then((result) => { this.location = result.regeocode.formattedAddress; }); }); } else { alert('浏览器不支持定位功能'); } } } }; </script> ``` 需要注意的是,代码中的'your-amap-api-key'需要替换为你自己申请的高德地图API的秘钥。另外,在使用定位功能时,需要用户授权浏览器获取其位置信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值