เมื่อค้นหาตำแหน่งจะแสดง 2 พินบนแผนที่แทนที่จะเป็น 1 พิน จะแสดงเพียงพินเดียวในการค้นหาได้อย่างไร

ในรหัสของฉัน ก่อนอื่นฉันกำลังแสดงตำแหน่งปัจจุบันและตำแหน่งคงที่ของสถานที่ที่ต้องการ เช่น โมเบียสพอดี แต่เมื่อฉันค้นหาสิ่งเดียวกันคือ mobius fit กำลังแสดงพินสองตัวที่อยู่ใกล้กันของตำแหน่ง mobius fit แทนที่จะเป็นอันเดียว จะแสดงเพียงพินเดียวในการค้นหาได้อย่างไร รหัสของฉัน:

 class LocationMapViewController:  UIViewController,CLLocationManagerDelegate, MKMapViewDelegate,UISearchBarDelegate,UIGestureRecognizerDelegate{

            var searchController:UISearchController!
            var annotation:MKAnnotation!
            var localSearchRequest:MKLocalSearchRequest!
            var localSearch:MKLocalSearch!
            var localSearchResponse:MKLocalSearchResponse!
            var error:NSError!
            var pointAnnotation:MKPointAnnotation!
            var pinAnnotationView:MKAnnotationView!
            let manager = CLLocationManager()
            @IBOutlet var MKView: MKMapView!
            @IBOutlet var searchBarButton: UISearchBar!


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
                let location = locations[0]
                let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
                let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
                let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
                MKView.setRegion(region, animated: true)
                self.MKView.showsUserLocation = true
            }



//Here im displaying two static location and current location


            let regionRadius: CLLocationDistance = 10000

            override func viewDidLoad() {
                self.searchBarButton.delegate = self
                super.viewDidLoad()
                manager.delegate = self
                manager.desiredAccuracy = kCLLocationAccuracyBest
                manager.requestWhenInUseAuthorization()
                manager.startUpdatingLocation()

                manager.stopMonitoringSignificantLocationChanges()
                let initialLocation = CLLocation(latitude: 37.539624, longitude: -122.062990)

                self.MKView.showsUserLocation = true
                self.MKView.isZoomEnabled = true
                centerMapOnLocation(location: initialLocation)
                let myAnnotation: MKPointAnnotation = MKPointAnnotation()
                myAnnotation.coordinate = CLLocationCoordinate2DMake(initialLocation.coordinate.latitude, initialLocation.coordinate.longitude);
                myAnnotation.title = "Fit@PRC"
                myAnnotation.subtitle = "Newark, CA 94560"
                MKView.addAnnotation(myAnnotation)
                MKView.selectAnnotation(myAnnotation, animated: true)

    let MobiusLocation = CLLocation(latitude: 37.454904, longitude: -122.228262)

                self.MKView.showsUserLocation = true
                self.MKView.isZoomEnabled = true
                centerMapOnLocation(location: MobiusLocation)
                let mobiusAnnotation: MKPointAnnotation = MKPointAnnotation()
                mobiusAnnotation.coordinate = CLLocationCoordinate2DMake(MobiusLocation.coordinate.latitude, MobiusLocation.coordinate.longitude);
                mobiusAnnotation.title = "Mobius Fit"
                mobiusAnnotation.subtitle = "Redwood City,CA 94061"
                MKView.addAnnotation(mobiusAnnotation)
                MKView.selectAnnotation(mobiusAnnotation, animated: true)


            }

// this is my search function . how to clear previous pins on search ?

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

                searchBar.endEditing(true)
                //1
                searchBar.resignFirstResponder()

                if self.MKView.annotations.count != 0{
                    annotation = self.MKView.annotations[0]
                    self.MKView.removeAnnotation(annotation)
                }
                //2
                localSearchRequest = MKLocalSearchRequest()
                localSearchRequest.naturalLanguageQuery = searchBar.text
                localSearch = MKLocalSearch(request: localSearchRequest)
                localSearch.start { (localSearchResponse, error) -> Void in

                    if localSearchResponse == nil{
                        let alertController = UIAlertController(title: nil, message: "Place Not Found", preferredStyle: UIAlertControllerStyle.alert)
                        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
                        self.present(alertController, animated: true, completion: nil)


                        return
                    }
                    //3
                    self.pointAnnotation = MKPointAnnotation()
                    self.pointAnnotation.title = searchBar.text
                    self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude:     localSearchResponse!.boundingRegion.center.longitude)


                    self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
                    self.MKView.centerCoordinate = self.pointAnnotation.coordinate

                    self.MKView.addAnnotation(self.pinAnnotationView.annotation!)

                    self.manager.stopUpdatingLocation()
                    self.manager.stopMonitoringSignificantLocationChanges()


                }
            }

person user8019529    schedule 21.06.2017    source แหล่งที่มา
comment
ให้ allAnnotations = self.mapView.annotations self.yourMapVieww.removeAnnotations(allAnnotations) คุณต้องค้นหาในการค้นหาของ Google วิธีลบคำอธิบายประกอบก่อนที่จะโพสต์คำถาม   -  person Gagan_iOS    schedule 21.06.2017


คำตอบ (1)


ปรับปรุงแล้ว

คุณต้องลบคำอธิบายประกอบที่เพิ่มไว้ก่อนหน้านี้ก่อนที่จะเพิ่มคำอธิบายประกอบใหม่ ดังนั้นคุณจึงสามารถทำได้ด้วยสองวิธีนี้

ขั้นแรก: ลบคำอธิบายประกอบเฉพาะของคุณก่อนที่จะเพิ่มคำอธิบายประกอบใหม่

for  annotation in mapView.annotations
{
    if(annotation.title! == "yourTitleAnnotationToRemove"){
    mapView.removeAnnotation(annotation);
        break
    }
}

ประการที่สอง: ลบสัญกรณ์ทั้งหมดก่อนที่จะเพิ่มใหม่ด้วย

let allAnnotations = mapView.annotations
mapView.removeAnnotations(allAnnotations)
person Vikky    schedule 21.06.2017