Created
February 9, 2014 02:58
-
-
Save wboykinm/8893528 to your computer and use it in GitHub Desktop.
leaflet.d3.js demo
This file contains 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>Leaflet.D3.js demo</title> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" /> | |
<!--[if lte IE 8]> | |
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.ie.css" /> | |
<![endif]--> | |
<script src="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script> | |
<script src="leaflet.d3.js"></script> | |
<style type='text/css'> | |
#svg-streets path { | |
fill:none; | |
stroke: rgba(0,0,0,0.3); | |
} | |
#svg-streets path:hover { | |
stroke: #0c0; | |
cursor: pointer; | |
} | |
#svg-streets .highway-residential { stroke-width: 1px; } | |
#svg-streets .highway-primary { stroke-width: 4px; } | |
#svg-streets .highway-secondary { stroke-width: 3px; } | |
#svg-streets .highway-tertiary { stroke-width: 2px; } | |
#svg-buildings path { | |
fill: rgba(0,0,0,0.3); | |
} | |
#svg-buildings path:hover { | |
fill: #00c; | |
cursor: pointer; | |
} | |
#svg-places path { | |
fill: yellow; | |
} | |
</style> | |
<script type='text/javascript'> | |
window.onload = function() { | |
var map = L.map('map',{ | |
maxBounds: [[46.6412301,23.4324747],[46.9201137,23.8047838]] | |
}).fitBounds([[46.7412301, 23.53816],[46.76813, 23.59253]]); | |
L.tileLayer('http://a.tile.stamen.com/toner/{z}/{x}/{y}.png').addTo(map); | |
d3.json("streets.geojson", function(collection) { | |
var layer = new L.D3geoJSON(collection, { | |
id: 'svg-streets', | |
featureAttributes: { | |
'class': function(feature) { | |
return 'highway-' + feature.properties.highway; | |
} | |
}, | |
}).addTo(map).on('click', function(e) { | |
console.log(e.data.properties.name); | |
}); | |
}); | |
d3.json("buildings.geojson", function(collection) { | |
var layer = new L.D3geoJSON(collection, { | |
id: 'svg-buildings', | |
}).addTo(map).on('click', function(e) { | |
console.log(e); | |
}); | |
}); | |
// d3.json("../buildings/temp-data/pins.json", function(collection) { | |
// var layer = new L.D3geoJSON(collection, { | |
// id: 'svg-places', | |
// }).addTo(map).on('click', function(e) { | |
// console.log(e); | |
// }); | |
// }); | |
}; | |
</script> | |
</head> | |
<body> | |
<div id='map' style='position:absolute;top:0;left:0;right:0;bottom:0;'></div> | |
</body> | |
</html> |
This file contains 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
L.D3geoJSON = L.Class.extend({ | |
includes: L.Mixin.Events, | |
initialize: function(data, options) { | |
this.data = data; | |
this.options = options; | |
var that = this; | |
this._clickHandler = function(data, idx) { | |
that.fire('click', { | |
element: this, | |
data: data, | |
originalEvent: d3.event | |
}); | |
}; | |
}, | |
onAdd: function(map) { | |
this._map = map; | |
this._first = true; | |
this._initZoomLvl = map._zoom; | |
this._svg = d3.select(this._map.getPanes().overlayPane).append('svg'); | |
this._svg.attr('pointer-events', 'none'); | |
this._group = this._svg.append('g'); | |
this._group.attr('class', 'leaflet-zoom-hide ' + (this.options.className || '')); | |
if (this.options.id) { | |
this._svg.attr('id', this.options.id); | |
} | |
function latLngToPoint(latlng) { | |
return map.project(latlng)._subtract(map.getPixelOrigin()); | |
}; | |
var t = d3.geo.transform({ | |
point: function(x, y) { | |
var point = latLngToPoint(new L.LatLng(y, x)); | |
return this.stream.point(point.x, point.y); | |
} | |
}); | |
this.path = d3.geo.path().projection(t); | |
this._feature = this._group.selectAll('path') | |
.data(this.data.features) | |
.enter() | |
.append('path') | |
.on('click', this._clickHandler); | |
this._map.on('viewreset', this.reset, this); | |
this._feature.attr('pointer-events', this.options.pointerEvents || 'visible'); | |
if (this.options.featureAttributes) { | |
for (var i in this.options.featureAttributes) { | |
this._feature.attr(i, this.options.featureAttributes[i]); | |
} | |
} | |
this.reset(); | |
}, | |
onRemove: function(map) { | |
this._svg.remove(); | |
this._map.off('viewreset', this.reset, this); | |
}, | |
reset: function(e) { | |
if (!this._bounds) { | |
this._bounds = d3.geo.path().projection(null).bounds(this.data); | |
} | |
var topLeft = this._map.latLngToLayerPoint([this._bounds[0][1], this._bounds[0][0]]), | |
bottomRight = this._map.latLngToLayerPoint([this._bounds[1][1], this._bounds[1][0]]); | |
this._svg | |
.attr('width', bottomRight.x - topLeft.x) | |
.attr('height', topLeft.y - bottomRight.y) | |
.style('left', topLeft.x + 'px') | |
.style('top', bottomRight.y + 'px'); | |
if (this._first) { | |
this._group.attr('transform', 'translate(' + -topLeft.x + ',' + -bottomRight.y + ')'); | |
this._feature.attr('d', this.path); | |
this._initTopLeft = topLeft; | |
this._initBottomRight = bottomRight; | |
this._first = false; | |
} else { | |
var trans = d3.transform(this._group.attr('transform')), | |
oldScale = trans.scale; | |
trans.scale = [oldScale[0] * ((bottomRight.x - topLeft.x) / (this._oldBottomRight.x - this._oldTopLeft.x)), | |
oldScale[1] * ((topLeft.y - bottomRight.y) / (this._oldTopLeft.y - this._oldBottomRight.y)) | |
]; | |
trans.translate = [-this._initTopLeft.x, -this._initBottomRight.y]; | |
this._group.attr('transform', 'scale('+trans.scale[0]+ ','+trans.scale[1]+')translate('+trans.translate[0] +',' + trans.translate[1] +')'); | |
} | |
this._oldTopLeft = topLeft; | |
this._oldBottomRight = bottomRight; | |
this._svg.attr('class', 'zoom-' + this._map.getZoom()); | |
}, | |
addTo: function(map) { | |
map.addLayer(this); | |
return this; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment