I am currently working on developing a d3 map inspired by the codepen created by Andy Barefoot: https://codepen.io/nb123456/pen/zLdqvM?editors=0010. My goal is to adjust the initiateZoom()
function in a way that setting specific lat/lon coordinates for a box around Ohio will result in the map panning over Ohio upon initialization.
function initiateZoom() {
minZoom = Math.max($("#map-holder").width() / w, $("#map-holder").height() / h);
maxZoom = 20 * minZoom;
zoom
.scaleExtent([minZoom, maxZoom])
.translateExtent([[0, 0], [w, h]])
;
midX = ($("#map-holder").width() - minZoom * w) / 2;
midY = ($("#map-holder").height() - minZoom * h) / 2;//Original values
var swlat = 32;
var swlon = -82;
var nelat = 42;
var nelon = -72;
var projectCoordinates = projection([(swlat+nelat)/2, (swlon+nelon)/2]);
/*This did not work
var midX = minZoom*(w-(swlat+nelat)/2) - ($("#map-holder").width()-(swlat+nelat)/2);
var midY = minZoom*(h-(swlon+nelon)/2) - ($("#map-holder").height()-(swlon+nelon)/2);*/
/*Neither did this
var midX = minZoom*(w-projectCoordinates[0])-($("#map-holder").width()-projectCoordinates[0]);
var midY = minZoom*(h-projectCoordinates[1])-($("#map-holder").height()-projectCoordinates[1]);*/
svg.call(zoom.transform, d3.zoomIdentity.translate(midX, midY).scale(minZoom));
}
The initial concept was:
1: Obtain the current pixel display of the map
2: Calculate the new pixel distance from the corner of the map to the specified point after zooming
3: Determine the pixel distance from the center of the container to the top
4: Subtract the values obtained from steps 2 and 3
The original intention was to shift the map so that it initializes with zoom and pan centered over the map. I attempted to modify this process initially by directly applying the lat/lon values into the equations provided above. Additionally, I tried transforming the lat/lon values using projections before substituting them in, but encountered difficulties. Can you guide me on what needs to be done to achieve the desired outcome?