How to implement travel navigation and route planning in uniapp
With the improvement of people’s living standards, travel navigation and route planning have become indispensable in modern society a part of. Implementing travel navigation and route planning in uniapp is not complicated. This article will introduce the specific steps to implement these functions through uniapp and related plug-ins, and provide code examples.
1. Introduce map components and navigation plug-ins
First, we need to introduce map components and navigation plug-ins into uniapp. The current mainstream navigation plug-ins include Baidu Maps and Amap. In uniapp, we can use the two plug-ins uni-app-baidumap and uni-app-amap to implement navigation and route planning functions.
1.1 Introducing the Baidu map plug-in
In the root directory of the uniapp project, install the uni-app-baidumap plug-in through npm:
npm install uni-app-baidumap
After that, in the main.js file in the root directory Introduce the plug-in:
import baiduMap from 'uni-app-baidumap'; Vue.use(baiduMap, { ak: 'your baidu map ak' });
Among them, 'your baidu map ak' needs to be replaced with your own AK (key) of Baidu Map API. In this way, the Baidu map plug-in was successfully introduced.
1.2 Introducing the Gaode map plug-in
In the root directory of the uniapp project, install the uni-app-amap plug-in through npm:
npm install uni-app-amap
After that, in the main.js in the root directory Introducing the plug-in in the file:
import amap from 'uni-app-amap'; Vue.use(amap);
This way the Amap plug-in is successfully introduced.
2. Implement location positioning and map display
Under the premise that the map plug-in has been introduced, we can display the map and achieve position positioning through the map component provided by uniapp.
2.1 Introduce the map component on the page
In the .vue file of the page, introduce the map component and set the related properties:
<template> <view class="map"> <map :longitude="longitude" :latitude="latitude" :scale="scale" style="width: 100%; height: 100%"></map> </view> </template>
Among them, longitude and latitude represent the longitude and latitude of the map respectively. Latitude, scale represents the zoom level of the map.
2.2 Get the current location and render the map
In the