From a0072bfa187c6c09da7877aa6cda5509a6319f68 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Mon, 14 Jan 2013 15:36:52 -0500 Subject: [PATCH] Add map.extent and use in geocoder --- js/id/renderer/map.js | 23 +++++++++++++++++++++-- js/id/ui/geocoder.js | 3 ++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/js/id/renderer/map.js b/js/id/renderer/map.js index 5ebc88ca5..da1a38fe9 100644 --- a/js/id/renderer/map.js +++ b/js/id/renderer/map.js @@ -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 () { diff --git a/js/id/ui/geocoder.js b/js/id/ui/geocoder.js index fd061e619..87eaa7d74 100644 --- a/js/id/ui/geocoder.js +++ b/js/id/ui/geocoder.js @@ -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]]); }); }