(Swift 3)如何向MapView添加多个引脚

问题描述 投票:0回答:1

我正在编写一个简单的应用程序,允许您搜索位置并在那里放置一个引脚。但是当我尝试添加另一个时,我无法弄清楚如何让这个引脚停留。这是我的代码:

class MyMapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, UISearchBarDelegate{

var isMapToBeUpdated = true
var numOfTrackedLocations = 0
let locationManager = CLLocationManager.self
let annotation = MKPointAnnotation()
var annotationTitle = ""
let appDelegate = UIApplication.shared.delegate as! AppDelegate
var longArr: [Double] = []
var latArr: [Double] = []
var cityArr: [String] = [""]
var count: Int = 0

//sets variables and links this file to the app delegate//
@IBOutlet weak var myMapView: MKMapView!

@IBAction func seachButton (_sender: Any){
    let searchController = (UISearchController(searchResultsController: nil))
    searchController.searchBar.delegate = self
    present(searchController, animated: true, completion: nil)
}
//links the search button to the search method//

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    UIApplication.shared.beginIgnoringInteractionEvents()
    let activityIndicator = UIActivityIndicatorView()
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
    activityIndicator.center = self.view.center
    activityIndicator.hidesWhenStopped = true
    activityIndicator.startAnimating()
    //the above adds a search bar function, and sets the loading animation//
    self.view.addSubview(activityIndicator)

    searchBar.resignFirstResponder()
    dismiss(animated: true, completion: nil)

    let searchRequest = MKLocalSearchRequest()
    searchRequest.naturalLanguageQuery = searchBar.text

    let activeSearch = MKLocalSearch(request: searchRequest)

    activeSearch.start { (response, error) in
        //starts a search session//
        activityIndicator.stopAnimating()
        UIApplication.shared.endIgnoringInteractionEvents()

        if response == nil
        {
            print ("ERROR")
        }
        else
        {
            var annotation = self.myMapView.annotations

            let latitude = response?.boundingRegion.center.latitude
            let longitude = response?.boundingRegion.center.longitude

            self.annotation.title = searchBar.text
            self.annotation.coordinate = CLLocationCoordinate2DMake(latitude!, longitude!)
            self.myMapView.addAnnotation(self.annotation)

            let coordinate: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude!, longitude!)
            let span = MKCoordinateSpanMake(0.1, 0.1)
            let region = MKCoordinateRegionMake(coordinate, span)
            self.myMapView.setRegion(region, animated: true)

这只是一小部分,如果你想看到我可以发布它但它与注释无关。如果有人能帮助我,我将不胜感激。

swift xcode maps
1个回答
0
投票

地图视图具有显示多个引脚的功能。

map.showAnnotations([arrAnnotation], animated: true)

其中arrAnotation是MKAnnotation的arr。

© www.soinside.com 2019 - 2024. All rights reserved.