【问题标题】:Taking code from UIViewController to UIView从 UIViewController 获取代码到 UIView
【发布时间】:2016-02-27 05:28:43
【问题描述】:

我正在寻找在 UIView 中创建此地图。这可能吗?如果是这样,你能告诉我如何将这段代码从 UIViewController 带到 UIView。我正在尝试将 MapBox 地图放入 UIView 中,并且只有关于如何将其导入 UIViewController 的说明

这段代码看起来像 UIView 类:

import Mapbox

class mapboxMap: UIViewController, MGLMapViewDelegate {

var mapView: MGLMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    mapView = MGLMapView(frame: view.bounds)
    mapView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

    // set the map's center coordinate
    mapView.setCenterCoordinate(CLLocationCoordinate2D(latitude: 40.7326808,
        longitude: -73.9843407),
        zoomLevel: 10, animated: false)
    view.addSubview(mapView)

    // Set the delegate property of our map view to self after instantiating it.
    mapView.delegate = self

    // Declare the marker `hello` and set its coordinates, title, and subtitle
    let hello = MGLPointAnnotation()
    hello.coordinate = CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407)
    hello.title = "Hello world!"
    hello.subtitle = "Welcome to my marker"
    mapView.addAnnotation(hello)

    let hello2 = MGLPointAnnotation()
    hello2.coordinate = CLLocationCoordinate2D(latitude: 40.7526808, longitude: -73.9843407)
    hello2.title = "Hello world!"
    hello2.subtitle = "Welcome to my marker"
    mapView.addAnnotation(hello2)


}

// Use the default marker; see our custom marker example for more information
func mapView(mapView: MGLMapView, imageForAnnotation annotation: MGLAnnotation) -> MGLAnnotationImage? {
    return nil
}

func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
    return true
}

override func viewDidAppear(animated: Bool) {
    // Wait a bit before setting a new camera.

    // Create a camera that rotates around the same center point, back to 0°.
    // `fromDistance:` is meters above mean sea level that an eye would have to be in order to see what the map view is showing.
    let camera = MGLMapCamera(lookingAtCenterCoordinate: mapView.centerCoordinate, fromDistance: 9000, pitch: 45, heading: 0)

    // Animate the camera movement over 5 seconds.
    mapView.setCamera(camera, withDuration: 2.5, animationTimingFunction: CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut))
}

}

【问题讨论】:

    标签: ios swift uiview uiviewcontroller mapbox


    【解决方案1】:

    我认为您正在尝试做的是“在子视图中添加 UIViewController”。尝试使用Container view Controller。以下主题可能对您有所帮助。

    add UIViewController in subview

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-22
      • 2010-11-23
      • 2012-09-21
      • 1970-01-01
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多