【发布时间】:2018-05-12 20:41:34
【问题描述】:
我正在尝试复制我在另一个项目中所拥有的内容,在该项目中我只使用代码来使用情节提要。我正在努力使用的视图控制器是地图视图控制器。
我正在尝试创建一个显示用户位置的地图,在启动时围绕该位置缩放到合适的级别,然后允许用户在地图上放置一个图钉,该图钉将在销。
我已经成功地创建了地图并显示了用户位置,但是在地图启动时正在努力设置区域以放大位置。我已经为此苦苦挣扎了几天。
我知道我需要在某处使用 CLlocation2D,但目前不确定在哪里/如何使用它。
我还尝试使用函数设置我的地图,到目前为止的代码如下(这是一个来自不同来源的拙劣工作,试图使其符合我的目标,如果它很混乱,请道歉)
> import UIKit
> import MapKit
> import CoreLocation
>
> class MapViewController: UIViewController, MKMapViewDelegate,
> CLLocationManagerDelegate {
> var mapView: MKMapView!
> var locationManager = CLLocationManager()
>
> override func viewDidLoad() {
> super.viewDidLoad()
>
> setupMapView()
>
> }
>
> override func viewWillAppear(_ animated: Bool) {
> super.viewWillAppear(animated)
>
> }
>
> func setupMapView() {
> let mapView = MKMapView()
>
> let leftMargin:CGFloat = 0
> let topMargin:CGFloat = 0
> let mapWidth:CGFloat = view.frame.size.width
> let mapHeight:CGFloat = view.frame.size.height
>
> mapView.frame = CGRect(x: leftMargin, y: topMargin, width: mapWidth, height: mapHeight)
>
> mapView.mapType = MKMapType.standard
> mapView.isZoomEnabled = true
> mapView.isScrollEnabled = true
> mapView.showsCompass = true
> mapView.showsScale = true
> mapView.showsUserLocation = true
>
> let noLocation = CLLocationCoordinate2D()
> let span:MKCoordinateSpan = MKCoordinateSpanMake(0.05, 0.05)
> let viewLocation = MKCoordinateRegionMake(noLocation, span)
>
>
> mapView.setRegion(viewLocation, animated: true)
> print(viewLocation)
>
> view.addSubview(mapView)
> }
>
> //add function to create circle overlay
>
> // override func didReceiveMemoryWarning() { //
> super.didReceiveMemoryWarning() // // Dispose of any resources
> that can be recreated. // }
>
>
> /*
> // MARK: - Navigation
>
> // In a storyboard-based application, you will often want to do a little preparation before navigation
> override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
> // Get the new view controller using segue.destinationViewController.
> // Pass the selected object to the new view controller.
> }
> */
>
> }
对于图钉,我在情节提要中使用了
@IBAction func pinDrop(_ sender: UILongPressGestureRecognizer)
但不确定如何将其转换为程序代码。
任何帮助都会很棒!
【问题讨论】: