mapbox / mapbox-maps-ios

Interactive, thoroughly customizable maps for iOS powered by vector tiles and Metal
https://www.mapbox.com/mapbox-mobile-sdk
Other
472 stars 153 forks source link

Fitting camera to bounds does not work as intended #2101

Open ristiisa opened 8 months ago

ristiisa commented 8 months ago

Environment

Observed behavior and steps to reproduce

Fitting the map to a bounds does not work properly if the angle of the diagonal is steep and the width of the device is small enough.

Simulator Screenshot - iPhone 15 Pro - 2024-01-24 at 12 08 53

import UIKit
import MapboxMaps

final class MultipleGeometriesExample: UIViewController, ExampleProtocol {
    private var mapView: MapView!
    private var cancelables = Set<AnyCancelable>()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set the center coordinate and zoom level.
        let centerCoordinate = CLLocationCoordinate2D(latitude: 0, longitude: 0)
        let options = MapInitOptions(cameraOptions: CameraOptions(center: centerCoordinate, zoom: 11))

        mapView = MapView(frame: view.bounds, mapInitOptions: options)
        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(mapView)

        // Allow the view controller to receive information about map events.
        mapView.mapboxMap.onMapLoaded.observeNext { [weak self] _ in
            guard let self = self else { return }

            let coordinates = [
                CLLocationCoordinate2DMake(59, 25),
                CLLocationCoordinate2DMake(56, 13)
            ]

            let camera = (try? mapView.mapboxMap.camera(
                for: coordinates,
                camera: CameraOptions(),
                coordinatesPadding: .init(allEdges: 0),
                maxZoom: nil,
                offset: nil))!

            mapView.camera.fly(to: camera, duration: 0.5)

            var line = GeoJSONSource(id: "line")
            line.data = .feature(Feature(geometry: LineString(coordinates.compactMap({ $0 }))))

            var lineLayer = LineLayer(id: "line-layer", source: line.id)
            lineLayer.lineColor = .constant(StyleColor(.red))
            lineLayer.lineWidth = .constant(3.0)
            lineLayer.lineCap = .constant(.round)

            try! mapView.mapboxMap.addSource(line)
            try! mapView.mapboxMap.addLayer(lineLayer)

            self.finish()
        }.store(in: &cancelables)
    }
}

Expected behavior

Simulator Screenshot - iPhone 15 Pro - 2024-01-24 at 12 09 03

Notes / preliminary analysis

The issue seems to be related to the aspect ratio of the screen and the angle of the corner coordinates. As it is possible to find coordinates that work well.

Additional links and references

ristiisa commented 8 months ago

@pjleonard37 @persidskiy @aleksproger @OdNairy @evil159 @maios

Any thoughts on this issue?

jhoanarango commented 8 months ago

Hi @ristiisa, just an observation here. What happens if you add values to the padding?


            let camera = (try? mapView.mapboxMap.camera(
                for: coordinates,
                camera: CameraOptions(),
                coordinatesPadding: .init(allEdges: 0), // <-- ADD 20 OR MORE TO THIS.
                maxZoom: nil,
                offset: nil))!
ristiisa commented 8 months ago

Hi @ristiisa, just an observation here. What happens if you add values to the padding?

            let camera = (try? mapView.mapboxMap.camera(
                for: coordinates,
                camera: CameraOptions(),
                coordinatesPadding: .init(allEdges: 0), // <-- ADD 20 OR MORE TO THIS.
                maxZoom: nil,
                offset: nil))!

still failure

persidskiy commented 8 months ago

@ristiisa you can try to workaround it by using mercator projection like here https://github.com/mapbox/mapbox-maps-ios/issues/2113#issuecomment-1935887812

ristiisa commented 8 months ago

@ristiisa you can try to workaround it by using mercator projection like here #2113 (comment)

I confirm, the fit to bounds does work with mercator projection. For us not a possible workaround though.

ristiisa commented 6 months ago

in 11.3-rc1 the issue is mostly resolved where the area is now fully visible but not centered.

simulator_screenshot_D8DE3A1E-A0AE-4B8E-A737-191FBCF23C21