Mapbox iOS setVisibleCoordinates 未按预期运行
Mapbox iOS setVisibleCoordinates not behaving as expected
我在 ios 中使用 Mapbox 在不同的屏幕尺寸上显示固定的爱尔兰地图时遇到了一些问题。 MGLMapView.setVisibleCoordinates 似乎是我需要的,但它的行为并不像我期望的那样。我希望传入左下角坐标和右上角坐标,并定义一个无论屏幕大小如何显示的矩形。
mapView.setVisibleCoordinates([CLLocationCoordinate2D(latitude: 50.347676, longitude: -11.444362), CLLocationCoordinate2D(latitude: 56.375322, longitude: -4.128659)], count: 2, edgePadding: UIEdgeInsets.zero, animated: false)
我也试过了
mapView.setVisibleCoordinateBounds(MGLCoordinateBounds(sw: CLLocationCoordinate2D(latitude: 50.347676, longitude: -11.444362), ne: CLLocationCoordinate2D(latitude: 56.375322, longitude: -4.128659)), edgePadding: UIEdgeInsets.zero, animated: false)
结果与下图相同。如您所见,iphone 5s 布局非常完美,但我希望上面的代码在 ipad 上显示相同的视图,但它被缩小了太多。如何跨设备显示相同的视图?
Mapbox 网站上有一个示例(我认为)完全符合您的要求。我冒昧地插入您的坐标等,如下所示。链接断开所以我将 post 代码。
编辑:更改为不允许滚动、平移和缩放。
import Mapbox
class ViewController: UIViewController, MGLMapViewDelegate {
private var ireland: MGLCoordinateBounds!
override func viewDidLoad() {
super.viewDidLoad()
let mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.delegate = self
let northeast = CLLocationCoordinate2D(latitude: 56.375322, longitude: -4.128659)
let southwest = CLLocationCoordinate2D(latitude: 50.347676, longitude: -11.444362)
var box = [northeast, southwest]
mapView.setVisibleCoordinates(&box, count: 2, edgePadding: UIEdgeInsets.init(top: 20.0, left: 20.0, bottom: 20.0, right: 20.0), animated: false)
mapView.isScrollEnabled = false
mapView.allowsZooming = false
view.addSubview(mapView)
}
}
好的,我成功了。 MGLMapView 上有一个相机方法,它将 return 相机尽可能放大,同时仍显示您指定的矩形。只需确保这是在 didFinishLoading
委托方法中完成的。
let northeast = CLLocationCoordinate2D(latitude: 55.499130, longitude: -5.945938)
let southwest = CLLocationCoordinate2D(latitude: 51.133369, longitude: -10.531064)
let ireland = MGLCoordinateBounds(sw: southwest, ne: northeast)
let camera = mapView.camera(mapView.camera, fitting: ireland, edgePadding: UIEdgeInsets.zero)
mapView.setCamera(camera, animated: false)
这有助于我在 Mapbox 中缩放特定区域
self.mapView.centerCoordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
self.mapView.zoomLevel = 13
self.mapView.centerCoordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
self.mapView.setCenter(CLLocationCoordinate2D(latitude: lat, longitude: long, animated: true)
我在 ios 中使用 Mapbox 在不同的屏幕尺寸上显示固定的爱尔兰地图时遇到了一些问题。 MGLMapView.setVisibleCoordinates 似乎是我需要的,但它的行为并不像我期望的那样。我希望传入左下角坐标和右上角坐标,并定义一个无论屏幕大小如何显示的矩形。
mapView.setVisibleCoordinates([CLLocationCoordinate2D(latitude: 50.347676, longitude: -11.444362), CLLocationCoordinate2D(latitude: 56.375322, longitude: -4.128659)], count: 2, edgePadding: UIEdgeInsets.zero, animated: false)
我也试过了
mapView.setVisibleCoordinateBounds(MGLCoordinateBounds(sw: CLLocationCoordinate2D(latitude: 50.347676, longitude: -11.444362), ne: CLLocationCoordinate2D(latitude: 56.375322, longitude: -4.128659)), edgePadding: UIEdgeInsets.zero, animated: false)
结果与下图相同。如您所见,iphone 5s 布局非常完美,但我希望上面的代码在 ipad 上显示相同的视图,但它被缩小了太多。如何跨设备显示相同的视图?
Mapbox 网站上有一个示例(我认为)完全符合您的要求。我冒昧地插入您的坐标等,如下所示。链接断开所以我将 post 代码。
编辑:更改为不允许滚动、平移和缩放。
import Mapbox
class ViewController: UIViewController, MGLMapViewDelegate {
private var ireland: MGLCoordinateBounds!
override func viewDidLoad() {
super.viewDidLoad()
let mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.delegate = self
let northeast = CLLocationCoordinate2D(latitude: 56.375322, longitude: -4.128659)
let southwest = CLLocationCoordinate2D(latitude: 50.347676, longitude: -11.444362)
var box = [northeast, southwest]
mapView.setVisibleCoordinates(&box, count: 2, edgePadding: UIEdgeInsets.init(top: 20.0, left: 20.0, bottom: 20.0, right: 20.0), animated: false)
mapView.isScrollEnabled = false
mapView.allowsZooming = false
view.addSubview(mapView)
}
}
好的,我成功了。 MGLMapView 上有一个相机方法,它将 return 相机尽可能放大,同时仍显示您指定的矩形。只需确保这是在 didFinishLoading
委托方法中完成的。
let northeast = CLLocationCoordinate2D(latitude: 55.499130, longitude: -5.945938)
let southwest = CLLocationCoordinate2D(latitude: 51.133369, longitude: -10.531064)
let ireland = MGLCoordinateBounds(sw: southwest, ne: northeast)
let camera = mapView.camera(mapView.camera, fitting: ireland, edgePadding: UIEdgeInsets.zero)
mapView.setCamera(camera, animated: false)
这有助于我在 Mapbox 中缩放特定区域
self.mapView.centerCoordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
self.mapView.zoomLevel = 13
self.mapView.centerCoordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
self.mapView.setCenter(CLLocationCoordinate2D(latitude: lat, longitude: long, animated: true)