我的名字是 João,我正在使用 Leaflet.js 在巴西坎皮纳斯大学完成我的结论课程项目。要解决的问题是:marker需要留在路由里面,如果没有,需要返回一个false。有人能帮我吗?谢谢。
var map = L.map('map',18);
L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {
maxZoom: 20,
}).addTo(map);
var cont = 0;
var marker = null;
function refresh() {
$.ajax({
method: "GET",
url: "mapa/coordenada",
dataType: "json"
})
.done(function( msg ) {
registro_coordenada(msg.coordenada);
});
}
function select_cord() {
$.ajax({
method: "GET",
url: "mapa/coordenada",
dataType: "json"
})
.done(function( msg ) {
add_cord(msg.coordenada);
});
}
function add_cord(coordenada){
out = coordenada.split(",");
var control = L.Routing.control(L.extend(window.lrmConfig, {
waypoints: [
L.latLng(-22.5627235,-47.425501),
L.latLng([out[0],out[1]])
],
geocoder: L.Control.Geocoder.nominatim(),
routeWhileDragging: true,
reverseWaypoints: true,
showAlternatives: true,
altLineOptions: {
styles: [
{color: 'black', opacity: 0.15, weight: 9},
{color: 'white', opacity: 0.8, weight: 6},
{color: 'blue', opacity: 0.5, weight: 2}
]
}
})).addTo(map);
L.Routing.errorControl(control).addTo(map);
}
var icone = new L.Icon({
iconUrl: 'https://cdn.rawgit.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-red.png',
shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/images/marker-shadow.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
shadowSize: [41, 41]
});
function registro_coordenada(coordenada){
out = coordenada.split(",");
if (cont === 0 ){
marker = L.marker([out[0],out[1]], {icon: icone}).addTo(map).bindPopup("<b>Localização atual</b>").openPopup();
cont = 1;
return;
}
map.removeLayer(marker);
//var marker = L.marker([out[0],out[1]]).remove(map).bindPopup("<b>Localização atual</b>").openPopup();
marker = L.marker([out[0],out[1]], {icon: icone}).addTo(map).bindPopup("<b>Localização atual</b>").openPopup().closePopup();
}
$(document).ready(function(){
if ($('#map')){
self.setInterval(function () {
refresh()
}, 1000);
}
select_cord();
});
问题在GitHub 上无法解决
如果有人帮助我,我会非常感激!