传单路由机器错误:未定义Geocoder

时间:2018-11-09 22:01:08

标签: javascript leaflet undefined

传单路由机出现问题。非常基本的代码

var routingtest = L.Routing.control({
        waypoints: [
            L.latLng(57.74, 11.94),
            L.latLng(57.6792, 11.949)
        ]
}).addTo(mymap);

运行正常。

但是,一旦我添加更多代码,如http://www.liedman.net/leaflet-routing-machine/tutorials/中所述,我就会收到错误消息。 例如,这不起作用:

var routingtest = L.Routing.control({
    waypoints: [
        L.latLng(57.74, 11.94),
        L.latLng(57.6792, 11.949)
    ],
    routeWhileDragging: true,
    geocoder: L.Control.Geocoder.nominatim()
}).addTo(mymap);

它将返回TypeError: L.Control.Geocoder is undefined。 我只是重新下载了插件,以确保没有意外修改任何内容,但仍然可以。 我的index.html看起来像这样:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Map</title>

<link type="text/css" rel="stylesheet" href="leaflet/leaflet.css" />
<link type="text/css" rel="stylesheet" href="plugins/Leaflet.fullscreen-gh-pages/dist/leaflet.fullscreen.css" />
<link type="text/css" rel="stylesheet" href="plugins/leaflet-routing-machine-3.2.7/dist/leaflet-routing-machine.css" />
<link type="text/css" rel="stylesheet" href="css/mycss.css" />

<script type="text/javascript" src="leaflet/leaflet.js"></script>
<script type="text/javascript" src="plugins/jquery/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="plugins/Leaflet.fullscreen-gh-pages/dist/Leaflet.fullscreen.js"></script>
<script type="text/javascript" src="plugins/leaflet-routing-machine-3.2.7/dist/leaflet-routing-machine.js"></script>
<script type="text/javascript" src="plugins/leaflet-routing-machine-3.2.7/dist/L.Routing.OpenRouteService.js"></script>
</head>
<body>
<!-- mapbox -->
<div id="map" class="map"></div>
<!-- custom script containing routing machine and other stuff (which works all fine)!-->
<script type="text/javascript" src="javascript/myscript.js"></script>
</body>
</html>

.js和.css文件的路径都很好。

我的带有底图之一的传单地图var看起来像这样:

var mymap = L.map('map', {
    center: [57.89, 12.02],
    zoom: 13.5,
    fullscreenControl: true
});

var osm = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png',
{ attribution: '&copy; <a href="http://osm.org/copyright" target = "_blank">OpenStreetMap</a>'
}).addTo(mymap);

什么可能导致TypeError: L.Control.Geocoder is undefined错误以及如何解决?

1 个答案:

答案 0 :(得分:0)

我安装了https://github.com/perliedman/leaflet-control-geocoder插件,现在可以使用了。它没有地方说这是先决条件...