【发布时间】:2019-11-13 22:02:05
【问题描述】:
我正在使用 Mapquest API 和 Leaflet 在网页上显示带有标记的地图。 传单参考:https://leafletjs.com/examples/quick-start/
单击按钮时,该按钮应在指定坐标处添加一个标记。出于某种原因,它似乎不起作用。这是我的脚本:
var L;
window.onload = function() { //loads map, center etc.
L.mapquest.key = 'O5L5sKXrVY1AYIpiS2xVMvsLgAxUSw32';
var map = L.mapquest.map('map', {
center: [37.641313, -122.290785],
layers: L.mapquest.tileLayer('map'),
zoom: 10
});
//add markers and other map shapes here.
//trying to get it to add based on input.
document.getElementById('mrkbtn').onclick = function addMarker() {
L.marker(document.getElementById('inputText').value).addTo(map); }
}
这里是 HTML 按钮:
<div class="centerBtn">
<input id="inputText" placeholder="insert coordinates"></input>
<button id="mrkbtn" onclick="addMarker()">Add Marker to Map</button>
</div>
<br>
<div id="map"></div>
当L.marker()内部有坐标时,按钮会添加一个标记,如下:
L.marker([37.641313, -122.290785]).addTo(map);
但是,当我更改 L.marker() 中的内容以尝试获取输入值时,它不起作用。 这是我犯错误的脚本部分:
L.marker(document.getElementById('inputText').value).addTo(map);
我是新手,非常感谢您的帮助。
【问题讨论】:
标签: javascript api button leaflet maps