Skip to content

Instantly share code, notes, and snippets.

@netconstructor
Created May 1, 2012 08:57
Show Gist options
  • Save netconstructor/2566553 to your computer and use it in GitHub Desktop.
Save netconstructor/2566553 to your computer and use it in GitHub Desktop.
Leaflet Stroke
<html>
<script src='./leaflet/leaflet.js'></script>
<link rel="stylesheet" href="./leaflet/leaflet.css" />
<body>
<div id='map' style="width: 600px; height: 400px"></div>
</body>
<script>
// Set up the map.
var map = new L.Map('map');
var tileUrl = 'http://otile1.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.jpg';
var tileAttrib = '(c) OSM contributors, CC-BY-SA, '+
'Tiles Courtesy of MapQuest';
var tiles = new L.TileLayer(
tileUrl, {maxZoom:19, attribution: tileAttrib}
);
map.setView(
new L.LatLng(51.509,-0.08), 13)
.addLayer(tiles);
var p1 = new L.LatLng(51.509, -0.08),
p2 = new L.LatLng(51.503, -0.06),
p3 = new L.LatLng(51.51, -0.047),
polygonPoints = [p1, p2, p3];
// Create polygon. Note stroke is set to false.
var polygon = new L.Polygon(polygonPoints, {stroke: false});
map.addLayer(polygon);
// Create cricle. Note stroke is set to false.
var circleLocation = new L.LatLng(51.508, -0.11),
circleOptions = {
color: 'red',
stroke: false,
fillColor: '#f03',
fillOpacity: 0.5
};
var circle = new L.Circle(circleLocation, 500, circleOptions);
map.addLayer(circle);
// when the polygon is clicked, turn its stroke on, and the
// circle's off.`
polygon.on('click', function() {
alert('polygon stroke: ' + polygon['options']['stroke']);
alert('circle stroke: ' + circle['options']['stroke']);
polygon.setStyle({stroke: true});
circle.setStyle({stroke: false});
alert('polygon stroke: ' + polygon['options']['stroke']);
alert('circle stroke: ' + circle['options']['stroke']);
});
// When the circle is clicked, turn its stroke on, and the polygon's
// off.
circle.on('click', function() {
alert('polygon stroke: ' + polygon['options']['stroke']);
alert('circle stroke: ' + circle['options']['stroke']);
polygon.setStyle({stroke: false});
circle.setStyle({stroke: true});
alert('polygon stroke: ' + polygon['options']['stroke']);
alert('circle stroke: ' + circle['options']['stroke']);
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment