CameraOptions Map::cameraForLatLngs(const std::vector<LatLng>& latLngs, const EdgeInsets& padding) { CameraOptions options; if (latLngs.empty()) { return options; } // Calculate the bounds of the possibly rotated shape with respect to the viewport. PrecisionPoint nePixel = {-INFINITY, -INFINITY}; PrecisionPoint swPixel = {INFINITY, INFINITY}; for (LatLng latLng : latLngs) { PrecisionPoint pixel = pixelForLatLng(latLng); swPixel.x = std::min(swPixel.x, pixel.x); nePixel.x = std::max(nePixel.x, pixel.x); swPixel.y = std::min(swPixel.y, pixel.y); nePixel.y = std::max(nePixel.y, pixel.y); } double width = nePixel.x - swPixel.x; double height = nePixel.y - swPixel.y; // Calculate the zoom level. double scaleX = (getWidth() - padding.left - padding.right) / width; double scaleY = (getHeight() - padding.top - padding.bottom) / height; double minScale = ::fmin(scaleX, scaleY); double zoom = ::log2(getScale() * minScale); zoom = ::fmax(::fmin(zoom, getMaxZoom()), getMinZoom()); // Calculate the center point of a virtual bounds that is extended in all directions by padding. PrecisionPoint paddedNEPixel = { nePixel.x + padding.right / minScale, nePixel.y + padding.top / minScale, }; PrecisionPoint paddedSWPixel = { swPixel.x - padding.left / minScale, swPixel.y - padding.bottom / minScale, }; PrecisionPoint centerPixel = { (paddedNEPixel.x + paddedSWPixel.x) / 2, (paddedNEPixel.y + paddedSWPixel.y) / 2, }; options.center = latLngForPixel(centerPixel); options.zoom = zoom; return options; }
CameraOptions Map::cameraForLatLngs(std::vector<LatLng> latLngs, EdgeInsets padding) { CameraOptions options; if (latLngs.empty()) { return options; } // Calculate the bounds of the possibly rotated shape with respect to the viewport. vec2<> nePixel = {-INFINITY, -INFINITY}; vec2<> swPixel = {INFINITY, INFINITY}; for (LatLng latLng : latLngs) { vec2<> pixel = pixelForLatLng(latLng); swPixel.x = std::min(swPixel.x, pixel.x); nePixel.x = std::max(nePixel.x, pixel.x); swPixel.y = std::min(swPixel.y, pixel.y); nePixel.y = std::max(nePixel.y, pixel.y); } vec2<> size = nePixel - swPixel; // Calculate the zoom level. double scaleX = (getWidth() - padding.left - padding.right) / size.x; double scaleY = (getHeight() - padding.top - padding.bottom) / size.y; double minScale = ::fmin(scaleX, scaleY); double zoom = ::log2(getScale() * minScale); zoom = ::fmax(::fmin(zoom, getMaxZoom()), getMinZoom()); // Calculate the center point of a virtual bounds that is extended in all directions by padding. vec2<> paddedNEPixel = { nePixel.x + padding.right / minScale, nePixel.y + padding.top / minScale, }; vec2<> paddedSWPixel = { swPixel.x - padding.left / minScale, swPixel.y - padding.bottom / minScale, }; vec2<> centerPixel = (paddedNEPixel + paddedSWPixel) * 0.5; LatLng centerLatLng = latLngForPixel(centerPixel); options.center = centerLatLng; options.zoom = zoom; return options; }
void Map::fitBounds(AnnotationSegment segment, EdgeInsets padding, Duration duration) { if (segment.empty()) { return; } // Calculate the bounds of the possibly rotated shape with respect to the viewport. vec2<> nePixel = {-INFINITY, -INFINITY}; vec2<> swPixel = {INFINITY, INFINITY}; for (LatLng latLng : segment) { vec2<> pixel = pixelForLatLng(latLng); swPixel.x = std::min(swPixel.x, pixel.x); nePixel.x = std::max(nePixel.x, pixel.x); swPixel.y = std::min(swPixel.y, pixel.y); nePixel.y = std::max(nePixel.y, pixel.y); } vec2<> size = nePixel - swPixel; // Calculate the zoom level. double scaleX = (getWidth() - padding.left - padding.right) / size.x; double scaleY = (getHeight() - padding.top - padding.bottom) / size.y; double minScale = std::fmin(scaleX, scaleY); double zoom = std::log2(getScale() * minScale); zoom = std::fmax(std::fmin(zoom, getMaxZoom()), getMinZoom()); // Calculate the center point of a virtual bounds that is extended in all directions by padding. vec2<> paddedNEPixel = { nePixel.x + padding.right / minScale, nePixel.y + padding.top / minScale, }; vec2<> paddedSWPixel = { swPixel.x - padding.left / minScale, swPixel.y - padding.bottom / minScale, }; vec2<> centerPixel = (paddedNEPixel + paddedSWPixel) * 0.5; LatLng centerLatLng = latLngForPixel(centerPixel); setLatLngZoom(centerLatLng, zoom, duration); }