更新记录

1.0.0(2026-03-27) 下载此版本

首次更新


平台兼容性

uni-app

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
- - - - - - - - -
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
× × × × × × × × × × × ×

y-mapservice

uni-app 引入 js即可使用,支持 android 、ios 和 h5

使用介绍

  1. 使用 HBuilderx 导入sdk,在相关文件中引入,示例代码:
<template>
    <view class="container">
        <map id="map" :latitude="latitude" :longitude="longitude" :markers="markers" :polyline="polyline"
            :style="{width: '100%',height: '100vh'}">
        </map>
    </view>
</template>

<script>
    import { calculateDrivingRoute } from '@/js_sdk/y-mapservice/mapservice.js';
    export default {
        data() {
            return {
                latitude: 39.908823, // 默认纬度(北京)
                longitude: 116.397470, // 默认经度
                //地图
                mapContext: undefined,
                mapPlus: undefined,
                //标记
                markers: [],
                //路线
                polyline: [],
                //起点
                startLon: 0,
                startLat: 0,
                //终点
                endLon: 0,
                endLat: 0
            }
        },
        onLoad(opt) {
            // 可根据自己需求获取,接口获取/定位获取
            this.startLon = opt.startLon;
            this.startLat = opt.startLat;
            this.endLon = opt.endLon;
            this.endLat = opt.endLat;
        },
        onReady() {
            this.mapContext = uni.createMapContext("map", this);
            this.mapPlus = this.mapContext.$getAppMap();
            this.refreshMap();
        },
        methods: {
            refreshMap() {
                // 定位到地图中间位置
                this.mapContext.moveToLocation({
                    latitude: this.startLat,
                    longitude: this.startLon
                });
                // 添加标记
                this.markers = [{
                        id: 0,
                        latitude: this.startLat,
                        longitude: this.startLon,
                        iconPath: '/static/index/map-ta.png', // 标记图标
                    },
                    {
                        id: 1,
                        latitude: this.endLat,
                        longitude: this.endLon,
                        iconPath: '/static/index/map-wo.png', // 标记图标
                    }
                ];

                // 计算并显示路线
                this.calculateRoute();
            },
            async calculateRoute(){
                try {
                    console.log('开始计算路线');
                    //填写高德开发者申请的《web服务》的 key
                    const apiKey = '';
                    const path = await calculateDrivingRoute(
                        apiKey,
                        parseFloat(this.startLon),
                        parseFloat(this.startLat),
                        parseFloat(this.endLon),
                        parseFloat(this.endLat),
                    );
                    // 设置路线
                    this.polyline = [{
                        points: path,
                        color: '#4dbe7c',
                        width: 10,
                        arrowLine: true
                    }];
                    console.log('路线已设置到地图');
                } catch (error) {
                    console.error('路线计算失败:', error);
                    // 失败时不显示路线
                    this.polyline = [];
                }
            }
        },
    }
</script>
  1. mapservice.js 参数说明
字段 描述
apiKey 高德开发者后台申请的web服务的key
startLon 起点的longitude
startLat 起点的latitude
endLon 终点的longitude
endLat 终点的latitude

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT协议