简体   繁体   English

从地图javascript中的标记链接到网址页面

[英]link to an url page from a marker in a map javascript

I am writing a blog with html, php and javascript about my travels. 我正在用有关旅行的html,php和javascript写博客。 I have a classical menu in which I choose a country. 我有一个经典的菜单,可以选择一个国家。 That links me to a new page with an article about this country. 那将我链接到一个新页面,上面有关于该国家的文章。

I would like to change it like this : I have a map with a marker for each country I went to. 我想这样更改:我有一张地图,上面有我去过的每个国家的标记。 I would like to click on the marker to move to the corresponding page. 我想单击标记以移至相应页面。

In my javascript I have my map and my markers : 在我的JavaScript中,我有地图和标记:

var map = L.map('mapid').setView([25, 22], defaut_zoom);
var carte="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}"

L.tileLayer(carte, {
    attribution:'Tiles © Esri — Source: Esri, DeLorme, NAVTEQ, USGS, Intermap, iPC, NRCAN, Esri Japan, METI, Esri China (Hong Kong), Esri (Thailand), TomTom, 2012'}).addTo(map);      
    var finlande = L.marker([60.1698,24.93837],{icon: satIcon}).addTo(map);
    var pouce = L.marker([47.50075,9.742309999999975]).addTo(map);

    //I tried to use an EventListener :

    finlande.addEventListener('click',function (event) {
        event.preventDefault();
        document.getElementById("lien");
    });
})

it links to my php file : 它链接到我的php文件:

<img id=lien><a href="finlande.html">

But it does not work. 但这行不通。 I have some basics in javascript but I have to admit that I get confused with what I can do in js and what I should do in html or php. 我在javascript中有一些基础知识,但是我不得不承认我对我在js中可以做什么以及在html或php中应该做什么感到困惑。 Thank you in advance for help ! 预先感谢您的帮助!

You can add some action in your event listener, in example to go to url: 您可以在事件监听器中添加一些操作,例如转到url:

finlande.addEventListener('click',function (event) {
    event.preventDefault();
    window.location = "finlande.html"; 
});

Bienvenue sur SO! SO Bienvenue!

The low level addEventListener method is for Elements . 低级的addEventListener方法用于Elements

Leaflet L.marker factory (and the like) return a JavaScript object , which internally manage the Element that is displayed on the page. Leaflet L.marker工厂(等等)返回一个JavaScript 对象 ,该对象内部管理页面上显示的Element。

Leaflet provides you with its own mechanism to listen to events on those objects, but the API is similar to the low level one (or actually, similar to other libraries like jQuery): Leaflet为您提供了自己的机制来侦听那些对象上的事件,但是该API与低级API相似(或者实际上与其他类似jQuery的库相似):

 var map = L.map("map").setView([48.85, 2.35], 12); var marker = L.marker([48.85, 2.35]).addTo(map); marker.on('click', function(leafletEvent) { alert(leafletEvent.latlng); }); L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' }).addTo(map); 
 <link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css"> <script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet-src.js"></script> <div id="map" style="height: 200px"></div> 

See also that question on GIS Stack Exchange: Add an event listener on a Marker in Leaflet 另请参阅有关GIS Stack Exchange的问题: 在Leaflet中的标记上添加事件侦听器

Then if you want the user to be redirected to some page when he/she clicks on the marker, instead of the above alert example, simply use window.location = "finlande.html" for example, like shown in @d324223's answer. 然后,如果您希望用户在单击标记时将其重定向到某个页面,而不是上面的alert示例,只需使用例如window.location = "finlande.html" ,如@ d324223的答案所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM