2017-12-16 3 views
0

위치를 검색하고 핀을 놓을 수있는 간단한 앱을 작성 중입니다. 그러나 내가 시도하고 다른 하나 추가 할 때이 핀을 유지하는 방법을 알아낼 수 없습니다. 당신은 내가 그것을 게시 할 수 있습니다 더 이상보고 싶어(Swift 3) MapView에 여러 개의 핀을 추가하는 방법

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) 

이, 단지 작은 부분이지만 주석에 관련되지 않은 : 여기에 내 코드입니다. 누구든지 이걸 도와 주면 대단히 감사하겠습니다.

+0

단계를 건너 뜁니다. longArr, latArr, cityArr은 무엇인가요? –

+0

배열의 핵심 데이터를 나중에 사용하고 있습니다. 중요하지 않습니다. – Mot

+0

열거 형 또는 for 루프를 사용할 수 있습니다. –

답변

0

지도보기에는 여러 개의 핀을 표시하는 기능이 있습니다.

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

여기서 arrAnotation은 MKAnnotation의 arr입니다.

관련 문제