This question demonstrates how to control the overlay zoom extents and pan extents in order to prevent wrapping by simply not allowing the user to see the whole world at once.
Prevent horizontal world-wrapping of overlays in GMap v3
So the meat of the problem is where the OverlayView is getting the pixel coordinates. If we could figure out how to improve that part to keep the shapes together, it might solve our problem. This may help to test for wrapping, but it may not work since your issue seems to happen even when we are zoomed in.
var proj = overlay.getProjection();
var wwidth = 0;
if (proj) wwidth = proj.getWorldWidth();
var mapsWrapsAround=false;
if (__wwidth > 0 && __wwidth < domContainer.width()) mapsWrapsAround = true;
I realized that testing whether the longitude was way different from the start longitude might not work because the longitude might be correct, but map to multiple div locations on the screen. So it's the div pixel coordinate we have to test. But maybe you can test for flipping in x by testing whether it's in the opposite direction compared to the difference in geographic coordinates.
// UNTESTED
// dimensioned somewhere outside
var start;
var startgeo;
// inside the overlay.onAdd content I think.
// Turn the overlay projection into a d3 projection
var googleMapProjection = function (coordinates) {
var googleCoordinates = new google.maps.LatLng(coordinates[1], coordinates[0]);
var pixelCoordinates = overlayProjection.fromLatLngToDivPixel(googleCoordinates);
var proj = overlay.getProjection();
var worldwidth = proj.getWorldWidth();
var finalX = pixelCoordinates.x;
if(typeof(start) === "undefined"){
start = pixelCoordinates;
startgeo = googleCoordinates;
}
else {
if(pixelCoordinates.x > start.x && googleCoordinates.lng() < startgeo.lng()){
finalX -= worldwidth;
}
else if(pixelCoordinates.x < start.x && googleCoordinates.lng() > startgeo.lng()){
finalX += worldwidth;
}
}
return [finalX, pixelCoordinates.y];
}
So what this is doing is assuming that google maps has picked the screen location closest to North America for each coordinate. If the shape is held together correctly, then if the end longitude is greater than the start longitude, then the end pixel.X should also be greater than the start pixel.X. If, however, the projection has returned a point on the opposite side of the world, then the new pixel, relative to the start pixel, should be in the wrong direction. That is, the end.x now be less than the start.x. So to fix it, we just adjust the pixel by the world width in the direction that would fix the problem. Hopefully.