2017-03-04 6 views
2

Ich habe ein Problem, wenn ich versuche, Schaltflächen in einem Leaflet-Popup hinzuzufügen. Das Popup wird generiert, wenn Sie auf die Karte klicken.Hinzufügen von Schaltflächen innerhalb des Leaflet-Popup-Fensters

Im Idealfall möchte ich 2 Tasten zeigen popuo:

  • Start von hier
  • und gehen Sie zu diesem Standort

Diese Skizze ist ein Beispiel für das Ergebnis, das ich will:

________________________________________________ 
|You clicked the map at LatLng(XXXXX,XXXXX)  | 
| --------------- -------------------  | 
| |Start from here| |Go to this location|  | 
| --------------- -------------------  | 
|___________________ ___________________________| 
        \/ 

das ist, was ich in meinem PopUp bekomme: Sie klickten die Karte bei LatLng (XXXXX, XXXX) [Objekt HTMLButtonElement]

Ich versuche, die Tasten von hier mit L.domUtil

defineYourWaypointOnClick(e: any) { 

var choicePopUp = L.popup(); 
var container = L.DomUtil.create('div'), 
    startBtn = this.createButton('Start from this location', container), 
    destBtn = this.createButton('Go to this location', container); 

choicePopUp 
    .setLatLng(e.latlng) 
    .setContent('You clicked the map at ' + e.latlng.toString() + '<br>' + startBtn) 
    .openOn(this.map); 

L.DomEvent.on(startBtn, 'click',() => { 
    alert("toto"); 
}); 

L.DomEvent.on(destBtn, 'click',() => { 
    alert("tata"); 
}); 
} 

createButton(label: string, container: any) { 
    var btn = L.DomUtil.create('button', '', container); 
    btn.setAttribute('type', 'button'); 
    btn.innerHTML = label; 
    return btn; 
} 

Ich nenne meine Methode zu erstellen:

this.map.on('click', (e: any) => { 
    this.defineYourWaypointOnClick(e); 
}); 

Vielen Dank im Voraus für jede Hilfe Sie mir geben können:)

Antwort

1

Sie sollten innerHTML- werden mit den Tasten auf Ihrem Merkzettel hinzufügen, wie unten

defineYourWaypointOnClick(e: any) { 

var choicePopUp = L.popup(); 
var container = L.DomUtil.create('div'); 
////////////////////////////////////////////////////////////////////////////////////////////// 
///////////modified here 
startBtn = this.createButton('Start from this location', container), 
destBtn = this.createButton('Go to this location', container); 
div.innerHTML = ''+startBtn+ '&nbsp;&nbsp;&nbsp;&nbsp;' + destBtn ; 
////////////////////////////////////////////////////////////////////////////////////////////// 

choicePopUp 
    .setLatLng(e.latlng) 
    .setContent('You clicked the map at ' + e.latlng.toString() + '<br>' + startBtn) 
    .openOn(this.map); 

L.DomEvent.on(startBtn, 'click',() => { 
    alert("toto"); 
}); 

L.DomEvent.on(destBtn, 'click',() => { 
    alert("tata"); 
}); 
} 

createButton(label: string, container: any) { 
var btn = L.DomUtil.create('button', '', container); 
btn.setAttribute('type', 'button'); 
btn.innerHTML = label; 
return btn; 
} 
+1

'div' ist undefiniert. 'startBtn' und' destBtn' sind immer noch HTML Elemente, keine Zeichenketten. – ghybs

+0

Ich habe gerade herausgefunden, dass mein Code funktioniert alles, was ich tun musste, ist nur den Container in das PopUp: .setContent (Container) – Uness

+0

, so dass meine Antwort nutzlos wird? – Aravind

Verwandte Themen