This is just a simple demo on how to get these apps working together. For data, I took a GeoJSON USA polygon file. It has eight fields that I show in the popup. (STATE_NAME, SUB_REGION, STATE_ABBR, POP2010, POP10_SQMI, MALES, FEMALES, SQMI).
The purpose of the app is to enter a state name in the search and zoom to it. The JQuery app will allow you to start typing and it will self-populate with possible matches. The more you type the smaller the selection list. It is not case sensitive which helps users, who do not know how it is stored in the database. Finally select one and the map zooms to the selected state and highlights it yellow.
Ready state.
As you start typing in a state name notice the possible choices show up.
Once selected it zooms to the state polygon and fires off a click event which highlights the polygon yellow and opens a popup showing the database attributes.
<!DOCTYPE html> <html> <head> <title>Search</title> <link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" /> <script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"/> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<style> #autocomplete { z-index: 100; margin-bottom: 5px; } #map { width: 800px; height: 600px; border: 1px solid black; z-index: 0; }
</style> </head> <body> <div class="ui-widget" style="text-align:left;"> <input id="autocomplete" placeholder="Search for: State"> </div> <div id="map" > </div> <script> var url = 'usa.json'; // my GeoJSON data source, in this case a static file not a live PHP data feed. var arr = []; var arr1 = [];
// Initialize autocomplete with empty source. $( "#autocomplete" ).autocomplete(); var map = L.map('map').setView([47.7541, -107.05078], 3); var osm=new L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png',{ attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'}); var OpenStreetMap_BlackAndWhite = L.tileLayer('http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png', { maxZoom: 18, attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>' }); OpenStreetMap_BlackAndWhite.addTo(map);
// Set style function that sets fill color property function style(feature) { return { fillColor: 'green', fillOpacity: 0.5, weight: 2, opacity: 1, color: '#ffffff', dashArray: '3' }; } var highlight = { 'fillColor': 'yellow', 'weight': 2, 'opacity': 1 }; function forEachFeature(feature, layer) { // Tagging each state poly with their name for the search control. layer._leaflet_id = feature.properties.STATE_NAME; var popupContent = "<p><b>STATE: </b>"+ feature.properties.STATE_NAME + "</br>REGION: "+ feature.properties.SUB_REGION + "</br>STATE ABBR: "+ feature.properties.STATE_ABBR + "</br>POP2010: "+ feature.properties.POP2010.toLocaleString() + "</br>Pop 2010 per SQMI: "+ feature.properties.POP10_SQMI.toLocaleString() + "</br>Males: "+ feature.properties.MALES.toLocaleString() + "</br>Females: "+ feature.properties.FEMALES.toLocaleString() + "</br>SQ Miles: "+ feature.properties.SQMI.toLocaleString() +'</p>'; layer.bindPopup(popupContent); layer.on("click", function (e) { stateLayer.setStyle(style); //resets layer colors layer.setStyle(highlight); //highlights selected. }); }
// Null variable that will hold layer var stateLayer = L.geoJson(null, {onEachFeature: forEachFeature, style: style}); $.getJSON(url, function(data) { stateLayer.addData(data); for (i = 0; i < data.features.length; i++) { //loads State Name into an Array for searching arr1.push({label:data.features[i].properties.STATE_NAME, value:""}); } addDataToAutocomplete(arr1); //passes array for sorting and to load search control. }); stateLayer.addTo(map);
////////// Autocomplete search function addDataToAutocomplete(arr) { arr.sort(function(a, b){ // sort object by Name var nameA=a.label, nameB=b.label if (nameA < nameB) //sort string ascending return -1 if (nameA > nameB) return 1 return 0 //default return value (no sorting) }) // The source for autocomplete. https://api.jqueryui.com/autocomplete/#method-option $( "#autocomplete" ).autocomplete("option", "source", arr); $( "#autocomplete" ).on( "autocompleteselect", function( event, ui ) { polySelect(ui.item.label); //grabs selected state name ui.item.value=''; }); } ///////////// Autocomplete search end
// fire off click event and zoom to polygon function polySelect(a){ map._layers[a].fire('click'); // 'clicks' on state name from search var layer = map._layers[a]; map.fitBounds(layer.getBounds()); // zooms to selected poly } // END...fire off click event and zoom to polygon
var baseMaps = { "Open Street Map": osm, "OSM B&W":OpenStreetMap_BlackAndWhite }; var overlayMaps = { "USA":stateLayer }; //Add layer control L.control.layers(baseMaps, overlayMaps).addTo(map);
</script> </body> </html>