高德地图Android自定义路线规划的简单实现

想把人的图标去掉、再把终点的图标样式换一下,最后还可以设置一下路线的颜色粗细什么的。摸索了几个小时,摸了一个解决办法:
重写WalkRouteOverlay的类,覆盖getBuslineWidth() 、getWalkColor()、getEndBitmapDescriptor()、 getStartBitmapDescriptor() 、getWalkBitmapDescriptor() 。
直接上代码吧,很简单的一个类:(在这里想说一下,不知道startMarker、endMarker和stationMarkers这些有什么用,摸索过程中被这些混淆视听了)

public class RouteTool extends WalkRouteOverlay{
        public int color;//路线颜色
        public float lineWidth;//路线宽度
        
/*修改路线宽度*/
       @Override
        protected float getBuslineWidth() {
                return lineWidth;
        }


/*修改路线颜色*/
        @Override
        protected int getWalkColor() {
                return color;
        }


/*
修改终点marker样式,这里的R.drawable.none是我自己画的一个PNG图片,图片什么都看不到,而这么修改就等于是把这些marker都去掉了,只留下一条规划的路线,当然可以把BitmapDescriptor 的起点、终点等做成域封装起来供别的类修改,现在我比较懒,就用汉字说明就好了
*/
        @Override
        protected BitmapDescriptor getEndBitmapDescriptor() {
                BitmapDescriptor reBitmapDescriptor=new BitmapDescriptorFactory().fromResource(R.drawable.none);
                return reBitmapDescriptor;
        }
/*修改起点marker样式*/
        @Override
        protected BitmapDescriptor getStartBitmapDescriptor() {
                BitmapDescriptor reBitmapDescriptor=new BitmapDescriptorFactory().fromResource(R.drawable.none);
                
                return reBitmapDescriptor;
        }
/*修改中间点marker样式*/
        @Override
        protected BitmapDescriptor getWalkBitmapDescriptor() {
                BitmapDescriptor reBitmapDescriptor=new BitmapDescriptorFactory().fromResource(R.drawable.none);
                
                return reBitmapDescriptor;
        }
/*一个无聊的构造*/
        public RouteTool(Context arg0 AMap arg1 WalkPath arg2 LatLonPoint arg3
                        LatLonPoint arg4) {
                super(arg0 arg1 arg2 arg3 arg4);
        }
/*一个工具方法,修改颜色和宽度*/
        public void setView(int colorfloat width) {
                this.color=color;
                lineWidth=width;
        }

}

然后我直接在ACTIVITY里面实现了路径规划接口OnRouteSearchListener,在onWalkRouteSearched()回调方法中新建了这个类,并调用了setView()方法。运行后,随便整俩点,做路径规划搜索,如果跨度大了可能步行的路径数据会有点延迟,下面给出一部分效果截图,这个绿色气泡是自己在ACTIVITY类里面加的一个marker,至于里面还有一条红线,那是地图的地铁来的。
这里写图片描述

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
要在UniApp中实现高德地图功能和路线规划功能,你需要以下步骤: 1.注册高德地图开发者账号,获取高德地图SDK的App Key。 2.在UniApp项目中安装并引入高德地图SDK插件。 3.创建一个地图页面,并在页面中引入高德地图组件,设置地图的初始化参数和样式。 4.在页面中使用高德地图API提供的功能,比如标记点、搜索位置、定位等。 5.实现路线规划功能,包括输入起点和终点,调用高德地图API计算路线,并在地图上显示路线。 以下是一个简单的示例代码: ``` <template> <view> <map :longitude="longitude" :latitude="latitude" :markers="markers" :polyline="polyline" style="width:100%;height:100%;"></map> </view> </template> <script> import { AMapPlugin } from '@dcloudio/uni-amap'; export default { data() { return { longitude: 0, latitude: 0, markers: [], polyline: [], start: '', end: '' } }, mounted() { // 初始化地图 AMapPlugin.initAMapApiLoader({ key: 'your_app_key', plugin: ['AMap.Geolocation'] }, () => { const map = new AMap.Map('mapContainer', { zoom: 16, center: [this.longitude, this.latitude], resizeEnable: true }); // 添加定位控件 map.addControl(new AMap.Geolocation()); // 在地图上添加标记点 const marker = new AMap.Marker({ position: [this.longitude, this.latitude], title: '我的位置' }); this.markers.push(marker); // 调用搜索API AMap.plugin('AMap.PlaceSearch', () => { const placeSearch = new AMap.PlaceSearch({ city: '全国', pageSize: 1, pageIndex: 1 }); placeSearch.search(this.end, (status, result) => { if (status === 'complete' && result.info === 'OK') { // 在地图上添加标记点 const endMarker = new AMap.Marker({ position: result.poiList.pois[0].location, title: this.end }); this.markers.push(endMarker); // 调用路线规划API AMap.plugin('AMap.Driving', () => { const driving = new AMap.Driving({ map: map, policy: AMap.DrivingPolicy.LEAST_DISTANCE }); driving.search([this.longitude, this.latitude], result.poiList.pois[0].location, (status, result) => { if (status === 'complete' && result.info === 'OK') { // 在地图上显示路线 const path = result.routes[0].path; const polyline = new AMap.Polyline({ path: path, strokeColor: '#3366FF', strokeWeight: 5 }); this.polyline.push(polyline); } }); }); } }); }); }); } } </script> ``` 这段代码实现了在地图上显示当前位置和输入的目的地,并计算并显示两点之间的路线。其中`your_app_key`需要替换为你的高德地图App Key。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

陈振阳

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

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

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

打赏作者

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

抵扣说明:

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

余额充值