(Swift 3)如何向MapView添加多个引脚
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(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)
这只是一小部分,如果你想看到我可以发布它但它与注释无关。如果有人能帮助我,我将不胜感激。
答案
地图视图具有显示多个引脚的功能。
map.showAnnotations([arrAnnotation], animated: true)
其中arrAnotation是MKAnnotation的arr。
以上是关于(Swift 3)如何向MapView添加多个引脚的主要内容,如果未能解决你的问题,请参考以下文章
带有默认地图视图引脚的 Swift Mapview 自定义调出视图
如何在 Swift 3 中的 mapview 上生成不同的图钉?