Skip to content

Instantly share code, notes, and snippets.

@phvaillant
Last active October 10, 2016 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phvaillant/f752a49119d6cac1283ad5c98c3c75eb to your computer and use it in GitHub Desktop.
Save phvaillant/f752a49119d6cac1283ad5c98c3c75eb to your computer and use it in GitHub Desktop.
Highlight neighborhood when hovering on names
<!DOCTYPE html>
<meta charset="utf-8">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css" />
<style>
#mapid {
width: 80%;
height: 100%;
float: left;
}
#neighborhood {
width: calc(20% - 40px);
height: 100%;
float: left;
overflow-y: scroll;
margin: 0px;
position: relative;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://unpkg.com/leaflet@1.0.0-rc.3/dist/leaflet.js"></script>
<script src="https://dl.dropboxusercontent.com/s/e6gge16nws1kuxl/LA.js"></script>
<div id="mapid"></div>
<ul id="neighborhood"></ul>
<script>
var mymap = L.map('mapid').setView([34, -118.5], 10);
L.tileLayer('https://api.mapbox.com/styles/v1/mtheisen/citei76iz005f2jpac8g7ilcl/tiles/256/{z}/{x}/{y}?access_token=pk.eyJ1IjoibXRoZWlzZW4iLCJhIjoiY2l0ZWk0NTBoMDVmMDJ0bXlyNmxzZ3RnMyJ9.fErHtcHVcTsHCLMPN1ZcQg', {
attribution: 'Map data &copy; <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>',
maxZoom: 18,
id: 'mtheisen',
accessToken: 'pk.eyJ1IjoibXRoZWlzZW4iLCJhIjoiY2l0ZWk0NTBoMDVmMDJ0bXlyNmxzZ3RnMyJ9.fErHtcHVcTsHCLMPN1ZcQg'
}).addTo(mymap);
geojson=L.geoJson(neighbData, {style: style,
onEachFeature: onEachFeature
}).addTo(mymap);
var highlightStyle = {
weight: 5,
color: '#666',
dashArray: '',
fillOpacity: 0.7
}
var normalStyle = {
weight: 2,
opacity: 1,
color: 'gray',
fillOpacity: 0.25
}
function highlightFeature(e) {
var layer = e.target;
layer.setStyle(highlightStyle);
}
function resetHighlight(e) {
geojson.resetStyle(e.target);
}
function style(feature) {
return {
fillColor: 'green',
weight: 2,
opacity: 1,
color: 'gray',
fillOpacity: 0.25
};
}
function onEachFeature(feature, layer) {
name = feature.properties.name;
$('#neighborhood').append('<li data-value="' + name + '">'+name+'</li>');
layer._leaflet_id = name;
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
});
}
var hovered_id, layer;
$('#neighborhood li').on('mouseenter', function(e){
hovered_id = $(e.target).data('value');
console.log(hovered_id);
layer = geojson.getLayer(hovered_id); //your feature id here
layer.setStyle(highlightStyle);
}).on('mouseout', function(e){
geojson.resetStyle(layer);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment