在用户位置的地图上添加图钉

时间:2019-12-17 16:04:50

标签: swift xcode

我想在用户位置附近添加图钉。 我可以用静态坐标来做  但我不能用用户的位置。我需要一个添加它的按钮 我按下了按钮,但是当我按下按钮时却无能为力   我不能按下相同的按钮  这就是我尝试过的

import UIKit
import MapKit
import CoreLocation
import LocalAuthentication


class ViewController: UIViewController {
//   Per la mappa
    @IBOutlet weak var Map: MKMapView!
    let newPin = MKPointAnnotation()
    let locationManager:CLLocationManager = CLLocationManager()


    // FOTOCAMERA
//
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        locationManager.requestWhenInUseAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.startUpdatingLocation()
        Map.showsUserLocation = true
    }

    @IBAction func Add(_ sender: Any) {



    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

        let span : MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)

        var userLocation : CLLocation = locations[0] as! CLLocation

        let location : CLLocationCoordinate2D = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude)


            let region : MKCoordinateRegion = MKCoordinateRegion(center: location, span: span)

          Map.setRegion(region, animated: true)
        let annotation = MKPointAnnotation()

        annotation.coordinate = CLLocationCoordinate2DMake ( userLocation.coordinate.latitude,userLocation.coordinate.longitude)
        annotation.title = "urgenza"
        annotation.subtitle = "QUI SI TROVA LA TUA URGENZA"
        Map.addAnnotation(annotation)

    }
}

0 个答案:

没有答案