Add map.extent and use in geocoder

This commit is contained in:
Ansis Brammanis
2013-01-14 15:36:52 -05:00
parent 72afc6dcb9
commit a0072bfa18
2 changed files with 23 additions and 3 deletions

View File

@@ -243,8 +243,27 @@ iD.Map = function() {
}, 20);
};
map.extent = function() {
return [projection.invert([0, 0]), projection.invert(dimensions)];
map.extent = function(tl, br) {
if (!arguments.length) {
return [projection.invert([0, 0]), projection.invert(dimensions)];
} else {
var TL = projection(tl),
BR = projection(br);
// Calculate maximum zoom that fits extent
hFactor = (BR[0] - TL[0]) / dimensions[0];
vFactor = (BR[1] - TL[1]) / dimensions[1];
hZoomDiff = Math.log(Math.abs(hFactor)) / Math.LN2;
vZoomDiff = Math.log(Math.abs(vFactor)) / Math.LN2;
zoomDiff = map.zoom() - Math.max(hZoomDiff, vZoomDiff);
// Calculate center of projected extent
midPoint = [(TL[0] + BR[0]) / 2, (TL[1] + BR[1]) / 2];
midLoc = projection.invert(midPoint);
map.zoom(zoomDiff).center(midLoc);
}
};
map.flush = function () {

View File

@@ -15,7 +15,8 @@ iD.ui.geocoder = function() {
.select('.content')
.text('No location found for "' + resp.query[0] + '"');
}
map.center([resp.results[0][0].lon, resp.results[0][0].lat]);
var bounds = resp.results[0][0].bounds;
map.extent([bounds[0], bounds[3]], [bounds[2], bounds[1]]);
});
}