UNPKG

leaflet-search

Version:

Leaflet Control for searching markers/features by attribute on map or remote searching in jsonp/ajax

83 lines (61 loc) 2.55 kB
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.0/dist/leaflet.css" /> <link rel="stylesheet" href="../src/leaflet-search.css" /> <link rel="stylesheet" href="style.css" /> </head> <body> <h3><a href="../"><big></big> Leaflet.Control.Search</a></h3> <h4>GeoJSON Example: <em>search vector features in GeoJSON layer by property</em></h4> <div id="map"></div> <div id="post-it"> <b>Search US states name:</b> Alabama, Arizona, Colorado, Maryland, Michigan, North Carolina, Pennsylvania, Wyoming ... </div> <script src="https://unpkg.com/leaflet@1.3.0/dist/leaflet.js"></script> <script src="../src/leaflet-search.js"></script> <script src="data/us-states.js"></script> <script> //sample data values define in us-states.js var data = us_states; var map = new L.Map('map', {zoom: 5, center: new L.latLng([37.8, -96]) }); map.addLayer(new L.TileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png')); //base layer var featuresLayer = new L.GeoJSON(data, { style: function(feature) { return {color: feature.properties.color }; }, onEachFeature: function(feature, marker) { marker.bindPopup('<h4 style="color:'+feature.properties.color+'">'+ feature.properties.name +'</h4>'); } }); map.addLayer(featuresLayer); var searchControl = new L.Control.Search({ layer: featuresLayer, propertyName: 'name', marker: false, moveToLocation: function(latlng, title, map) { //map.fitBounds( latlng.layer.getBounds() ); var zoom = map.getBoundsZoom(latlng.layer.getBounds()); map.setView(latlng, zoom); // access the zoom } }); searchControl.on('search:locationfound', function(e) { //console.log('search:locationfound', ); //map.removeLayer(this._markerSearch) e.layer.setStyle({fillColor: '#3f0', color: '#0f0'}); if(e.layer._popup) e.layer.openPopup(); }).on('search:collapsed', function(e) { featuresLayer.eachLayer(function(layer) { //restore feature color featuresLayer.resetStyle(layer); }); }); map.addControl( searchControl ); //inizialize search control </script> <div id="copy"><a href="https://opengeo.tech/">Website</a> &bull; <a rel="author" href="https://opengeo.tech/stefano-cudini/">Stefano Cudini</a></div> <script type="text/javascript" src="/labs-common.js"></script> </body> </html>