【问题标题】:Creating map to show user location and annotations programmatically in Swift 3在 Swift 3 中以编程方式创建地图以显示用户位置和注释
【发布时间】: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)

但不确定如何将其转换为程序代码。

任何帮助都会很棒!

【问题讨论】:

    标签: ios xcode swift3 mapkit


    【解决方案1】:

    像这样添加长按

    let longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.addAnnotation(_:)))
    
    self.mapView.addGestureRecognizer(longPress)
    

    //

    获取新闻坐标

    @objc func addAnnotation(_ gestureRecognizer:UIGestureRecognizer)
    {
    
       if gestureRecognizer.state != UIGestureRecognizerState.began
        {
          return
        }
         let touchPoint = gestureRecognizer.location(in: self.mapView)
         let newCoordinates = self.mapView.convert(touchPoint, toCoordinateFrom: self.mapView)
    
    }
    

    //

     let noLocation =  self.mapView.userLocation.coordinate
    
     let span:MKCoordinateSpan = MKCoordinateSpanMake(0.05, 0.05)
    
     let viewLocation = MKCoordinateRegionMake(noLocation, span)
    
     mapView.setRegion(viewLocation, animated: true)
    

    【讨论】:

    • 谢谢 - 我试过这个,但得到错误 - 线程 1:致命错误:在展开可选值时意外发现 nil。我猜这是因为没有初始的长按手势所以导致进程崩溃?我还在 setupMapView 中放了 let longPress...,这是正确的吗?我在函数和 ViewDidLoad 中都进行了尝试,在两种情况下都得到了相同的错误
    • 去掉这里 let mapView = MKMapView()
    • 成功了,谢谢! - 我明白了,为什么“让”会导致问题?
    • 因为允许您声明一个本地 mapView 变量,而不是此处声明的 var mapView: MKMapView!,该变量在 viewDidLoad 执行后被释放,将其保留为 var nil 并在您长按时导致崩溃,但当您remove 让初始化发生在那个 out var
    • 好的,我跟着。您知道如何设置区域吗?我在想我可以把 mapView.setRegion 放在 setupMapView 函数中,但不知道如何访问用户位置。目前该区域不是用户所在的位置。
    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多