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

5
我将尝试使用leaflet在我的地图上获取标记之间的路线。 我使用了以下代码:
L.Routing.control({
waypoints: [
L.latLng(36.9009882, 10.3009531),
L.latLng(36.9922751, 10.1255164)
]}).addTo(map);

但是我的地图上没有任何新内容。 是否有什么缺失?我该怎么办?


1
我使用你的代码创建了一个演示,它运行良好:https://plnkr.co/edit/2S5ZW7BNb9B9vzOJ5FBF?p=preview -- 你能分享更多的代码吗?也许你有其他插件与之冲突?你使用的是哪个版本的L.Routing和Leaflet? - chrki
现在对我也起作用了,问题出在插件上。 - Ben Mustapha Sabrine
1个回答

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>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接