我想在点击地图上的新地点时删除旧引脚

时间:2017-04-21 13:14:21

标签: ios swift uitableview mapkit cllocationmanager

我正在研究一个项目并坚持这一部分:当我选择数组indexpath.row == 0然后我选择另一个数组时,我希望最后一个数据消失并将新引脚放在地图上。

let annotation1 = MKPointAnnotation()

//SuperMarkets
let SuperMarket = ["Mass SuperMarket", "West Zone SuperMarket", "Home Needs SuperMarket", "E City SuperMarket", "Al Samary SuperMarket", "Cooperative SuperMarket","Al Madina SuperMarket"]
let locationManger1 = CLLocationManager()

var lat = 0.0
var lng = 0.0

//1
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    let lat1: CLLocationDegrees = lat
    let lng1: CLLocationDegrees = lng

    let myLocation1 = CLLocationCoordinate2DMake(lat1,lng1)
    let mySpan1:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
    let region1:MKCoordinateRegion = MKCoordinateRegion(center: myLocation1,span: mySpan1)
    self.MapMarket.setRegion(region1, animated: true)

    //Stop updating locations
    self.locationManger1.stopUpdatingLocation()

    //Place a pin for Burj Lhalifa on the map


    annotation1.coordinate = myLocation1
    annotation1.title = "Dubai"

    self.MapMarket.addAnnotation(annotation1)

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    self.locationManger1.delegate = self
    self.locationManger1.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManger1.requestAlwaysAuthorization()
    self.locationManger1.startUpdatingLocation()
    self.MapMarket.showsUserLocation = true

    if indexPath.row == 0
    {

        lat = 25.242066
        lng = 55.304286
        annotation1.subtitle = SuperMarket[0]

    }
    else if indexPath.row == 1
    {
        annotation1.subtitle = SuperMarket[1]
        lat = 25.257495
        lng = 55.296193
    }
    else if indexPath.row == 2
    {
        annotation1.subtitle = SuperMarket[2]
        lat = 25.257751
        lng = 55.298907
    }
    else if indexPath.row == 3
    {
        annotation1.subtitle = SuperMarket[3]
        lat = 25.270104
        lng = 55.320396
    }
    else if indexPath.row == 4
    {
        annotation1.subtitle = SuperMarket[4]
        lat = 25.269888
        lng = 55.303436
    }
    else if indexPath.row == 5
    {
        annotation1.subtitle = SuperMarket[5]
        lat = 25.251315
        lng = 55.333161
    }
    else if indexPath.row == 6
    {
        annotation1.subtitle = SuperMarket[6]
        lat = 25.280860
        lng = 55.350525
    }

}

1 个答案:

答案 0 :(得分:0)

如果要删除注释,则可以像这样使用

mapView.removeAnnotations(mapView.annotations)