使用Leaflet Routing Machine查找两个标记之间的路径

时间:2016-07-21 12:05:39

标签: javascript leaflet openstreetmap

我试图通过传单在地图上的标记之间找到路线。 我用了那段代码:

L.Routing.control({
waypoints: [
L.latLng(36.9009882, 10.3009531),
L.latLng(36.9922751, 10.1255164)
]}).addTo(map);

但我的地图上没有新东西。 有什么遗失的吗?我该怎么办?

1 个答案:

答案 0 :(得分:0)

1 - 您是否将(Leaflet Routing Machine)包含在您的应用中? 2- 确保您的地图变量是 (map) 试试这个简单的例子

有关更多信息,请访问此页面 https://www.liedman.net/leaflet-routing-machine/

    var mymap = L.map('mapid').setView([51.505, -0.09], 5);

    L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
        maxZoom: 18,
        attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, ' +
            'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
        id: 'mapbox/streets-v11',
        tileSize: 512,
        zoomOffset: -1
    }).addTo(mymap);

    L.marker([51.5, -0.09]).addTo(mymap)
        .bindPopup("<b>Hello world!</b><br />I am a popup.").openPopup();

    L.marker([53.5, -0.09]).addTo(mymap)
        .bindPopup("<b>Hello world!</b><br />I am a popup.").openPopup();


// route the way leafet machine as I said AI


L.Routing.control({
   waypoints:   [
   L.latLng(51.5, -0.09),
   L.latLng(53.5, -0.09)
   ]
}).addTo(mymap);
<!DOCTYPE html>
<html>
<head>
    
    <title>Quick Start - Leaflet</title>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />

    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin=""/>
    <script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js" integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA==" crossorigin=""></script>

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" />
<link rel="stylesheet" href="https://unpkg.com/leaflet-routing-machine@latest/dist/leaflet-routing-machine.css" />
<script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js"></script>
<script src="https://unpkg.com/leaflet-routing-machine@latest/dist/leaflet-routing-machine.js"></script>
    
</head>
<body>



<div id="mapid" style="width: 600px; height: 400px;"></div>
<script>





</script>



</body>
</html>