Forked from sridharraman/leaflet_polyline_marker.html
Created
October 4, 2020 08:43
-
-
Save Daniboi737/3dece6fd9a96e6542899d95fac6a3762 to your computer and use it in GitHub Desktop.
Extend leaflet.js marker to include polyline and display it like a toggling popup on clicking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Quick Start - Leaflet</title> | |
| <meta charset="utf-8" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" /> | |
| <link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" integrity="sha512-M2wvCLH6DSRazYeZRIm1JnYyh22purTM+FDB5CsyxtQJYeKq83arPe5wgbNmcFXGqiSH2XR8dT/fJISVA1r/zQ==" crossorigin=""/> | |
| <script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js" integrity="sha512-lInM/apFSqyy1o6s89K4iQUKg6ppXEgsVxT35HbzUupEVRh2Eu9Wdl4tHj7dZO0s1uvplcYGmt3498TtHq+log==" crossorigin=""></script> | |
| </head> | |
| <body> | |
| <div id="mapid" style="width: 100%; height: 600px;"></div> | |
| <script> | |
| var mymap = L.map('mapid').setView([51.505, -0.09], 13); | |
| L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', { | |
| maxZoom: 18, | |
| attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' + | |
| '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' + | |
| 'Imagery © <a href="http://mapbox.com">Mapbox</a>', | |
| id: 'mapbox.streets' | |
| }).addTo(mymap); | |
| // Custom Marker | |
| var PolyMarker = L.Marker.extend({ | |
| options: { | |
| polyline: L.polyline([], { color: 'red' }) | |
| } | |
| }); | |
| // Create custom markers (with polylines) | |
| new PolyMarker([51.5, -0.09], { polyline: L.polyline([[51.508147, -0.130881], [51.508248, -0.131879], [51.510131, -0.134175]], { color: 'red' }) }).addTo(mymap).on('click', onMarkerClick); | |
| new PolyMarker([51.5, -0.08], { polyline: L.polyline([[51.507559, -0.124229], [51.508294, -0.114874]], { color: 'blue' }) }).addTo(mymap).on('click', onMarkerClick); | |
| new PolyMarker([51.5, -0.07], { polyline: L.polyline([[51.502056, -0.130516], [51.500707, -0.126847]], { color: 'green' }) }).addTo(mymap).on('click', onMarkerClick); | |
| // Polylines layer | |
| var polylines = L.layerGroup().addTo(mymap); | |
| // Marker Click method | |
| function onMarkerClick(e) { | |
| // Clear polylines layer | |
| polylines.clearLayers(); | |
| // Add polyline corresponding to marker | |
| polylines.addLayer(e.target.options.polyline); | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment