Issue
Part of my app contains a mapview which searches automatically based on a keyword. I added a textbox and search button to my mapview, but I noticed the pins do not clear from the previous search. How do I clear the map of all pins before the new search?
import UIKit
import MapKit
import CoreLocation
import iAd
class MapClass: UIViewController, CLLocationManagerDelegate, UISearchBarDelegate, ADBannerViewDelegate {
var searchController:UISearchController!
var annotation:MKAnnotation!
var localSearchRequest:MKLocalSearchRequest!
var localSearch:MKLocalSearch!
var localSearchResponse:MKLocalSearchResponse!
var error:NSError!
var pointAnnotation:MKPointAnnotation!
var pinAnnotationView:MKPinAnnotationView!
var holidayKeyWord = NSString()
@IBOutlet var mapSearchTextbox: UITextField!
@IBOutlet weak var mapView: MKMapView!
@IBOutlet var adBannerView: ADBannerView!
var locationManager: CLLocationManager!
let searchRadius: CLLocationDistance = 2000
override func viewDidLoad() {
super.viewDidLoad()
holidayKeyWord = "Restaurant"
println(holidayKeyWord)
mapSearchTextbox.text = holidayKeyWord as String
self.canDisplayBannerAds = true
self.adBannerView?.delegate = self
self.adBannerView?.hidden = false
if (CLLocationManager.locationServicesEnabled())
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
}
@IBAction func mapSearchButton(sender: UIButton) {
//clear pins
var holidayKeyWord = mapSearchTextbox.text
if (CLLocationManager.locationServicesEnabled())
{
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = holidayKeyWord as String
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler {
(response: MKLocalSearchResponse!, error: NSError!) in
for item in response.mapItems as! [MKMapItem] {
println(item.name)
self.addPinToMapView(item.name, latitude: item.placemark.location.coordinate.latitude, longitude: item.placemark.location.coordinate.longitude)
}
}
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//replace spaces with dashes in wikiDate string
if (segue.identifier == "amazonToWeb") {
var wikiDate = "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Ddigital-text&field-keywords=\(holidayKeyWord)"
var DestViewController : WebBrowser = segue.destinationViewController as! WebBrowser
DestViewController.wikiDate = wikiDate
}
if (segue.identifier == "searchToWeb") {
var wikiDate = "http://www.bing.com"
var DestViewController : WebBrowser = segue.destinationViewController as! WebBrowser
DestViewController.wikiDate = wikiDate
}
if (segue.identifier == "ebayToWeb") {
var wikiDate = "http://search.ebay.com/\(holidayKeyWord)"
var DestViewController : WebBrowser = segue.destinationViewController as! WebBrowser
DestViewController.wikiDate = wikiDate
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations.last as! CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(region, animated: true)
var latitude: Double = location.coordinate.latitude
var longitude: Double = location.coordinate.longitude
let initialLocation = CLLocation(latitude: latitude, longitude: longitude)
// 1
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = holidayKeyWord as String
// 2
let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
request.region = MKCoordinateRegion(center: initialLocation.coordinate, span: span)
// 3
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler {
(response: MKLocalSearchResponse!, error: NSError!) in
for item in response.mapItems as! [MKMapItem] {
println(item.name)
//println("Latitude = \(item.placemark.location.coordinate.latitude)")
//println("Longitude = \(item.placemark.location.coordinate.longitude)")
self.addPinToMapView(item.name, latitude: item.placemark.location.coordinate.latitude, longitude: item.placemark.location.coordinate.longitude)
}
}
locationManager.stopUpdatingLocation()
let coordinateRegion = MKCoordinateRegionMakeWithDistance(initialLocation.coordinate, searchRadius * 2.0, searchRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
}
func addPinToMapView(title: String, latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let annotation = MyAnnotation(coordinate: location, title: title)
mapView.addAnnotation(annotation)
}
func removePinFromMapView(title: String, latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let annotation = MyAnnotation(coordinate: location, title: title)
mapView.removeAnnotation(annotation)
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
{
println("Error: " + error.localizedDescription)
}
Solution
Iterate through your annotations and delete them one by one like this:
if let annotations = self.mapView.annotations {
for _annotation in annotations {
if let annotation = _annotation as? MKAnnotation
{
self.mapView.removeAnnotation(annotation)
}
}
}
Answered By - ezcoding
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.