删除以前的引脚并更新坐标

时间:2017-06-27 12:26:03

标签: swift xcode mkmapview mapkit

此代码显示我当前的位置,当我按下其他位置时,标签会更新新坐标并将新针脚放入新按下的位置。然而,即使我不断改变位置,它仍然显示以前的引脚。

import UIKit
import MapKit 
import CoreLocation

class PersonalViewController: UIViewController , CLLocationManagerDelegate ,MKMapViewDelegate {


let locationManager = CLLocationManager()
let newPin = MKPointAnnotation()


@IBOutlet weak var map: MKMapView!
@IBOutlet weak var lable: UILabel!
var myLocation:CLLocationCoordinate2D?


func resetTracking(){
    if (map.showsUserLocation){
        map.showsUserLocation = false
        if let annotationz = self.map.annotations {
            for _annotation in annotationz {
                if let annotation = _annotation as? MKAnnotation
                {
                    self.map.removeAnnotation(annotation)
                }
            }
        }
    }
}


func centerMap(_ center:CLLocationCoordinate2D){
    self.saveCurrentLocation(center)

    let spanX = 0.007
    let spanY = 0.007

    let newRegion = MKCoordinateRegion(center:center , span: MKCoordinateSpanMake(spanX, spanY))
    map.setRegion(newRegion, animated: true)
}

func saveCurrentLocation(_ center:CLLocationCoordinate2D){
    let message = "\(center.latitude) , \(center.longitude)"
    print(message)
    self.lable.text = message
    myLocation = center
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    map.removeAnnotation(newPin)

    let location = locations.last! as CLLocation

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    //set region on the map
    map.setRegion(region, animated: true)

    newPin.coordinate = location.coordinate
    map.addAnnotation(newPin)

}

static var enable:Bool = true
@IBAction func getMyLocation(_ sender: UIButton) {

    if CLLocationManager.locationServicesEnabled() {
        if PersonalViewController.enable {
            locationManager.stopUpdatingHeading()
            sender.titleLabel?.text = "Enable"
        }else{
            locationManager.startUpdatingLocation()
            sender.titleLabel?.text = "Disable"
        }
        PersonalViewController.enable = !PersonalViewController.enable
    }
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
    let identifier = "pin"
    var view : MKPinAnnotationView
    if let dequeueView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView{
        dequeueView.annotation = annotation
        view = dequeueView
    }else{
        view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        view.canShowCallout = true
        view.calloutOffset = CGPoint(x: -5, y: 5)
        view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    }
    view.pinColor =  .red
    return view
}

}

1 个答案:

答案 0 :(得分:1)

试试这个:迭代你的注释并逐个删除它们,如下所示:

if let annotations = self.map.annotations {
    for _annotation in annotations {
        if let annotation = _annotation as? MKAnnotation
        {
            self.map.removeAnnotation(annotation)
        }
    }
}