diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index cdfa77935..cdd3044dc 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -1621,6 +1621,10 @@ mbgl::LatLngBounds MGLLatLngBoundsFromCoordinateBounds(MGLCoordinateBounds coord { CGPoint convertedPoint = [self convertPoint:point fromView:view]; + // flip y coordinate for iOS view origin top left + // + convertedPoint.y = self.bounds.size.height - convertedPoint.y; + return MGLLocationCoordinate2DFromLatLng(_mbglMap->latLngForPixel(mbgl::vec2(convertedPoint.x, convertedPoint.y))); } @@ -1628,6 +1632,10 @@ mbgl::LatLngBounds MGLLatLngBoundsFromCoordinateBounds(MGLCoordinateBounds coord { mbgl::vec2 pixel = _mbglMap->pixelForLatLng(MGLLatLngFromLocationCoordinate2D(coordinate)); + // flip y coordinate for iOS view origin in top left + // + pixel.y = self.bounds.size.height - pixel.y; + return [self convertPoint:CGPointMake(pixel.x, pixel.y) toView:view]; } diff --git a/src/mbgl/map/transform_state.cpp b/src/mbgl/map/transform_state.cpp index 9e4ea457e..c672ca5de 100644 --- a/src/mbgl/map/transform_state.cpp +++ b/src/mbgl/map/transform_state.cpp @@ -217,7 +217,7 @@ vec2 TransformState::coordinateToPoint(const TileCoordinate& coord) cons matrix::vec4 p; matrix::vec4 c = {{ coord.column, coord.row, 0, 1 }}; matrix::transformMat4(p, c, mat); - return { p[0] / p[3], p[1] / p[3] }; + return { p[0] / p[3], height - p[1] / p[3] }; } TileCoordinate TransformState::pointToCoordinate(const vec2 point) const { @@ -232,14 +232,16 @@ TileCoordinate TransformState::pointToCoordinate(const vec2 point) const if (err) throw std::runtime_error("failed to invert coordinatePointMatrix"); + double flippedY = height - point.y; + // since we don't know the correct projected z value for the point, // unproject two points to get a line and then find the point on that // line with z=0 matrix::vec4 coord0; matrix::vec4 coord1; - matrix::vec4 point0 = {{ point.x, point.y, 0, 1 }}; - matrix::vec4 point1 = {{ point.x, point.y, 1, 1 }}; + matrix::vec4 point0 = {{ point.x, flippedY, 0, 1 }}; + matrix::vec4 point1 = {{ point.x, flippedY, 1, 1 }}; matrix::transformMat4(coord0, point0, inverted); matrix::transformMat4(coord1, point1, inverted);