Key
和 安全密钥
; API
;jsloader
去加载高德地图的js文件,让它加载到我们的页面中;Vue3 + TS
为例写的;创建好应用之后,点击右边的 添加Key,按照要求填写信息;
❗❗ 注意:
pnpm add @amap/amap-jsapi-loader --save
;npm i @amap/amap-jsapi-loader --save
;Vue3
: onMounted()
;Vue2
: mounted()
;开始使用:
import { onMounted } from 'vue';
import AMapLoader from '@amap/amap-jsapi-loader';
// 开发环境:配置JsCode
// 如果是在 ts 的项目中,这一步会有类型错误,解决方案请移步 2.1.1
window._AMapSecurityConfig = {
securityJsCode:'「您申请的安全密钥」'
};
/** 初始化地图函数 */
onMounted(() => {
// AMapLoader => 加载器
// 资源加载完成后就会触发 then
AMapLoader.load({
"key": "上述步骤得到的key", // 申请好的Web端开发者Key,首次调用 load 时必填
"version": "2.0", // 指定要加载的 JS API 的版本,缺省时默认为 1.4.15
"plugins": [], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
}).then((AMap)=>{
// 初始化地图
const map = new AMap.Map('放置地图的div容器id', {
// 配置对象 - 配置地图的风格和缩放比例,请移步 2.2
});
}).catch(e => {
console.log(e);
});
});
效果展示:
这就是基本的地图;
window
扩展 全局属性;src/types/global.d.ts
;interface Window {
_AMapSecurityConfig: {
securityJsCode: string;
};
}
这里为什么使用 interface
?
Ctrl
,鼠标点击 window
,会跳转到lib.dom.d.ts
文件;Ctrl
,点击 window
; Window
的类型声明处; interface
,现在我们是想给Window
上添加类型,使用interface
的话,可以重复声明类型,这些重复的类型最后是会合并在一起的;const map = new AMap.Map('容器id', {
// 地图风格 - 可以修改地址的最后一个单词来使用不同的风格
mapStyle: 'amap://styles/whitesmoke',
// 缩放比例 - 缩放比例越大,地图就越大,展示的月清晰
zoom: 10
});
map/setMapStyle('amap://styles/whitesmoke')
mapStyle
属性值 的 最后一个单词 来 使用不同的风格;// 我们将接口将返回的经纬度数组赋值给 longitudeAndLatitudeList 响应式数据;
// 这里就是模拟一下
const logisticsInfoList = ref([
{latitude: '23.129152403638752', longitude: '113.42775362698366'},
{latitude: '30.454012', longitude: '114.42659'},
{latitude: '31.93182', longitude: '118.633415'},
{latitude: '31.035032', longitude: '121.611504'}
]);
const initMap = () => {
AMapLoader.load({
"key": "上述步骤得到的key", // 申请好的Web端开发者Key,首次调用 load 时必填
"version": "2.0", // 指定要加载的 JS API 的版本,缺省时默认为 1.4.15
"plugins": [], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
})
.then((AMap) => {
// 初始化地图
const map = new AMap.Map('map', {
// 地图风格
mapStyle: 'amap://styles/whitesmoke',
// 缩放比例
zoom: 12
});
// 加载插件 - AMap.Drivin
AMap.plugin('AMap.Driving', function () {
// 插件加载完成后,初始化 Driving 实例
const driving = new AMap.Driving({
// 使用上面得到的地图实例,表示,路径是画在我们当前初始化的这个地图上的
map
});
// 得到 Driving 实例之后,就可以使用 driving.search() 方法,通过起点和终点绘制路径
// 此处最好不要使用 logisticsInfoList 响应式数据进行地图绘制;
// 重新声明一个变量,复制一份经纬度数据
// 有可能我们的经纬度数据是没有的,所以在这块需要进行判断,并且必须 logisticsInfoList.value.length >= 2(必须要有起点 和 终点坐标)
if (logisticsInfoList?.value && logisticsInfoList.value.length >= 2) {
const list = [...logisticsInfoList.value];
// 取出 起点、终点、途径点
const start = list.shift();
const end = list.pop();
// 剩下的list就是途径点
// 该函数接收第四个参数
// 第一个参数:由起点经纬度组成的数组
// 第二个参数:由终点经纬度组成的数组
// 第三个参数:配置对象 - 请移步 3.3
// 第四个参数:函数 - 执行了该函数,就表示路径已经规划完成
driving.search(
[start?.longitude, start?.latitude],
[end?.longitude, end?.latitude],
() => {
// 规划完毕
}
);
};
});
})
.catch(e => {
console.log(e);
});
};
效果展示:
const driving = new AMap.Driving({
map,
// 关闭实时路况信息
showTraffic: false
});
效果展示:
driving.search(
[start?.longitude, start?.latitude],
[end?.longitude, end?.latitude],
// waypoints:该属性的属性值为由经纬度数组组成的数组,也就是说该属性的属性值是个二维数组
{ waypoints: list.map((item) => [item.longitude, item.latitude]) },
() => {
// 执行了这个函数,就表示路径已经规划完毕
}
);
效果展示:
const driving = new AMap.Driving({
// 使用上面得到的地图实例,表示,路径是画在我们当前初始化的这个地图上的
map,
// 关闭实时路况信息
showTraffic: false,
// 关闭途径点icon
hideMarkers: true
});
效果展示:
icon
的大小;import startImg from '@/assets/images/start.png';
// 取出 起点、终点、途径点
const start = list.shift();
// 创建起点
const marker = new AMap.Marker({
// 经纬度坐标
position: [start?.longitude, start?.latitude],
// 需要展示的图标
icon: startImg
});
// 将创建的标记点加到地图上(此处的map就是上述创建的地图实例)
map.add(marker);
size
,偏移imageOffset
等属性,比单纯设置URL更具灵活性;import startImg from '@/assets/images/start.png';
// 取出 起点、终点、途径点
const start = list.shift();
// 创建起点
const icon = new AMap.Icon({
// 图标尺寸 ===> new AMap.Size(宽, 高)
size: new AMap.Size(25, 30),
// Icon图片
image: startImg,
// 根据所设置的大小拉伸或压缩图片 ===> new AMap.Size(宽, 高)
imageSize: new AMap.Size(25, 30)
});
const marker = new AMap.Marker({
// 经纬度坐标
position: [start?.longitude, start?.latitude],
// 需要展示的图标
icon
});
// 将创建的标记点加到地图上(此处的map就是上述创建的地图实例)
map.add(marker);
效果展示:
const marker = new AMap.Marker({
// 经纬度坐标
position: [start?.longitude, start?.latitude],
// 需要展示的图标
icon,
// 设置偏移量 ===> new AMap.Pixel(X轴方向的偏移量, Y轴方向的偏移量)
offset: new AMap.Pixel(-12.5, -30)
});
效果展示:
// 文件路径:src/types/order.d.ts
export type Location = {
/** 经度 */
longitude: string;
/** 纬度 */
latitude: string;
};
AMapLoader.load
的 then
回调中;import type { Location } from '@/types/order';
import startImg from '@/assets/start.png';
/**
* 创建标记函数
* @param point 经纬度坐标
* @param image 图片
* @param width 图片宽度
* @param height 图片高度
*/
const createMarker = (
point: Location,
image: string,
width: number = 25,
height: number = 30
) => {
// 创建icon实例
const icon = new AMap.Icon({
// 图标尺寸 ===> new AMap.Size(宽, 高)
size: new AMap.Size(width, height),
// Icon图片
image,
// 根据所设置的大小拉伸或压缩图片 ===> new AMap.Size(宽, 高)
imageSize: new AMap.Size(width, height)
});
// 创建标记
const marker = new AMap.Marker({
// 经纬度坐标
position: [point?.longitude, point?.latitude],
// 需要展示的图标
icon,
// 图像相对展示区域的偏移量 ===> new AMap.Pixel(X轴方向的偏移量, Y轴方向的偏移量)
offset: new AMap.Pixel(-width / 2, -height)
});
return marker;
};
使用函数:
// 取出 起点、终点、途径点
const start = list.shift();
// 将创建的标记点加到地图上
// 这块会提示start可能不存在,使用 ! 进行非空断言
map.add(createMarker(start!, startImg, 25, 30));
效果展示:
// 当前位置坐标
// 我这里就是模拟,实际的项目中,也是通过接口返回的
// 绘制当前位置的时机,最好是在路径已经规划完毕之后再去绘制
import carImg from '@/assets/car.png';
const currentLocationInfo = ref({"latitude":"31.93182","longitude":"118.633415"});
driving.search(
[start?.longitude, start?.latitude],
[end?.longitude, end?.latitude],
{ waypoints: list.map((item) => [item.longitude, item.latitude]) },
() => {
// 执行了这个函数,就表示路径已经规划完毕
// 绘制当前位置,最好是在路径规划完成之后绘制
// 获取当前点位
const curr = currentLocationInfo.value;
map.add(createMarker(curr!, carImg, 25, 20));
}
);
效果展示:
setFitView()
;setRoom()
;driving.search(
[start?.longitude, start?.latitude],
[end?.longitude, end?.latitude],
{ waypoints: list.map((item) => [item.longitude, item.latitude]) },
() => {
// 执行了这个函数,就表示路径已经规划完毕
// 绘制当前位置,最好是在路径规划完成之后绘制
// 获取当前点位
const curr = logistics.value?.currentLocationInfo;
const currMarker = createMarker(curr!, carImg, 25, 20);
map.add(currMarker);
// 先展示整个路径,三秒后定位到地图的正中央
setTimeout(() => {
// 定位到地图正中央
// 第一个参数,是有 marker 组成的数组
map.setFitView([currMarker]);
// 设置缩放比例
map.setZoom(10);
}, 3000);
}
);
效果展示:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。