在 Ionic 4 中,尝试一下。
1) npm i 传单。
2) npm i 传单路由机。
3)在“index.html”中导入 js 和 css 。
<!-- Leaflet CDN JS and Css-->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin="" />
<script src="https://unpkg.com/leaflet@1.4.0/dist/leaflet.js" integrity="sha512-QVftwZFqvtRNi0ZyCtsznlKSWOStnDORoefr1enyq5mVL4tmKB3S/EnC3rRJcxCPavG10IcrVGSmPh6Qw5lwrg==" crossorigin=""></script>
<!-- Leaflet Machine. path: 'node_modules/leaflet-routing-machine/dist/leaflet-routing-machine.js' the same to css-->
<link rel="stylesheet" href="./assets/css/leaflet-routing-machine.css">
<script src="./assets/js/leaflet-routing-machine.js"></script>
4) 在 your_component.ts
import Leaflet from 'leaflet';
import 'leaflet-routing-machine';
// Add this line to remove typescript errors
declare var L: any;
export class FullScreenMapPage implements OnInit {
// mapa
mapa: any;
ionViewDidEnter() {
this.drawMap();
}
drawMap(): void {
// marcador del mapa
const markerGroup = Leaflet.featureGroup();
const marker = Leaflet.marker([lat, lng]);
this.mapa = Leaflet.map('map').setView([lat, lng], 11);
Leaflet.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© Open Street Map',
zoom: 8,
maxZoom: 18,
minZoom: 4,
minResolution: 4891.96981025128,
maxResolution: 39135.75848201024,
doubleClickZoom: true,
center: [lat, lng]
}).addTo(this.mapa);
Leaflet.Routing.control({
waypoints: [
Leaflet.latLng(PointA_lat, PointB_lng),
Leaflet.latLng(PointB_lat, PointB_lng)
], routeWhileDragging: true
}).addTo(this.mapa);
}
}