Storing CLLocation and keeping annotation - ios

My app currently has three tabs, a tab for pinning a location, and a detailView of the pinned location on the second tab. I am trying to save the location of the pin into NSUserdefaults. I would then like that location to stay pinned upon reloading the app, and therefore the detail view would still display the detail view of the pinned location. Here is what I have so far,
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
// Find location of user
var userLocation:CLLocation = locations[0] as! CLLocation
var latitude = userLocation.coordinate.latitude
var longitude = userLocation.coordinate.longitude
var latDelta:CLLocationDegrees = 0.01
var longDelta: CLLocationDegrees = 0.01
var span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
var location:MKUserLocation = currentLocation;
var region: MKCoordinateRegion = MKCoordinateRegionMake(location.coordinate, span)
var coordinate:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude);
carInitialLocation = userLocation;
let locationData = NSKeyedArchiver.archivedDataWithRootObject(carInitialLocation);
NSUserDefaults.standardUserDefaults().setObject(locationData, forKey: "locationData");
carInitialCoordinate = coordinate;
self.map.setRegion(region, animated: true);
}
override func viewDidLoad() {
super.viewDidLoad()
if let loadedData = NSUserDefaults.standardUserDefaults().dataForKey("locationData") {
if let loadedLocation = NSKeyedUnarchiver.unarchiveObjectWithData(loadedData) as? CLLocation {
println(loadedLocation.coordinate.latitude);
println(loadedLocation.coordinate.longitude);
var annotation = MKPointAnnotation()
annotation.coordinate = loadedLocation.coordinate
annotation.title = title
self.map.addAnnotation(annotation)
}
}
map.addAnnotations(artworks)
map.delegate = self;
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
manager.requestWhenInUseAuthorization();
manager.startUpdatingLocation();
self.map.showsUserLocation = true;
currentLocation = map.userLocation;
}
I then want the pinLocation button to be deactivated once the user has pinned a location once. I try to do this as so:
#IBAction func pinLocationButton(sender: AnyObject) {
// add location to the array, so it can be retrieved and put it into temporary storage
//places.append(["name":title,"lat":"\(newCoordinate.latitude)","lon":"\(newCoordinate.longitude)"])
if let loadedData = NSUserDefaults.standardUserDefaults().dataForKey("locationData") {
if let loadedLocation = NSKeyedUnarchiver.unarchiveObjectWithData(loadedData) as? CLLocation {
println(loadedLocation.coordinate.latitude);
println(loadedLocation.coordinate.longitude);
pinLocationButton.enabled = false;
}
}
var location = carInitialLocation
var coordinate = carInitialCoordinate
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
var title = ""
if (error == nil) {
if let p = CLPlacemark(placemark: placemarks?[0] as! CLPlacemark) {
var subThoroughfare:String = ""
var thoroughfare:String = ""
if p.subThoroughfare != nil {
subThoroughfare = p.subThoroughfare
}
if p.thoroughfare != nil {
thoroughfare = p.thoroughfare
}
completeAddress = self.displayLocationInfo(p);
title = "\(subThoroughfare) \(thoroughfare)"
}
}
// annotation, i.e pins
var annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = title
self.map.addAnnotation(annotation)
// NSUserDefaults.standardUserDefaults().setObject(places, forKey: "places")
})
}
Then, in my detailVC I attempt to reverse geocode the pinned location, but it is defaulting to the current location.. which I don't understand why
Here's the code:
super.viewDidLoad()
addressLabel.font = UIFont(name: addressLabel.font.fontName, size: 18)
smallMapView.delegate = self;
locationManager.delegate = self;
smallMapView.mapType = MKMapType.Hybrid;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.requestWhenInUseAuthorization();
locationManager.startUpdatingLocation();
smallMapView.zoomEnabled = true;
smallMapView.rotateEnabled = true;
if let loadedData = NSUserDefaults.standardUserDefaults().dataForKey("locationData") {
if let loadedLocation = NSKeyedUnarchiver.unarchiveObjectWithData(loadedData) as? CLLocation {
println(loadedLocation.coordinate.latitude);
println(loadedLocation.coordinate.longitude);
CLGeocoder().reverseGeocodeLocation(loadedLocation, completionHandler: { (placemarks, error) -> Void in
var title = "";
var subtitle = "";
var locality = "";
if(error == nil) {
if let placemark = CLPlacemark(placemark: placemarks?[0] as! CLPlacemark) {
var subThoroughfare:String = "";
var thoroughfare:String = "";
var locality:String = "";
var postalCode:String = "";
var administrativeArea:String = "";
var country:String = "";
if (placemark.subThoroughfare != nil) {
subThoroughfare = placemark.subThoroughfare;
}
if(placemark.thoroughfare != nil) {
thoroughfare = placemark.thoroughfare;
}
if(placemark.locality != nil) {
locality = placemark.locality;
}
if(placemark.postalCode != nil) {
postalCode = placemark.postalCode;
}
if(placemark.administrativeArea != nil) {
administrativeArea = placemark.administrativeArea;
}
if(placemark.country != nil) {
country = placemark.country;
}
println("viewcontroller placmark data:");
println(locality);
println(postalCode);
println(administrativeArea);
println(country);
title = " \(subThoroughfare) \(thoroughfare) \n \(locality), \(administrativeArea) \n \(postalCode) \(country)";
subtitle = " \(subThoroughfare) \(thoroughfare)";
println(title);
self.addressLabel.text = title;
}
}
var latitude = loadedLocation.coordinate.latitude;
var longitude = loadedLocation.coordinate.longitude;
var latDelta:CLLocationDegrees = 0.001;
var longDelta:CLLocationDegrees = 0.001;
var span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta);
var overallLoc = CLLocationCoordinate2DMake(latitude, longitude);
var region:MKCoordinateRegion = MKCoordinateRegionMake(overallLoc, span);
var annotation = MKPointAnnotation();
annotation.coordinate = loadedLocation.coordinate;
annotation.title = subtitle;
self.smallMapView.addAnnotation(annotation);
self.smallMapView.setRegion(region, animated: true)
})
}
}
}

in the pinLocationButton function, you use the manager.location which is wrong, you should use
CLGeocoder().reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
var title = ""
if (error == nil) {
// ....

Related

iOS/Swift: mapView won't update coordinates

I'm having an issue with updating/refreshing the map.
The code below is from the MainViewController
let forecastConstants = ForecastConstants(apiKey: forecastAPIKey)
coordinates.latitude = latPassed
coordinates.longitude = LongPassed
if latPassed == 0 && LongPassed == 0 {
coordinates.latitude = 43.161030
coordinates.longitude = -77.610922
}
LatPassed and LongPassed are passed from another controller (LocationViewController), where a new location (Lat and Long) is entered. All Controllers are in embedded in TabBar.
In MapViewController, I have:
var coordinate = MainForecastViewController()
override func viewDidLoad() {
super.viewDidLoad()
let lat = coordinate.coordinates.latitude
let lon = coordinate.coordinates.longitude
}
mapView.delegate = self
let coord = CLLocationCoordinate2D(latitude: lat, longitude: lon)
let region = MKCoordinateRegionMakeWithDistance(coord, 1000, 1000)
mapView.setRegion(region, animated: true)
pin = AnnotationPin(title: "Load temp here", subtitle: "", coordinate: coord)
mapView.addAnnotation(pin)
mapView.selectAnnotation(pin, animated: true)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView = MKAnnotationView(annotation: pin, reuseIdentifier: "forecast")
annotationView.image = UIImage(named: "weatherMapIcon")
let transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
annotationView.transform = transform
return annotationView
}
The problem is, when I enter a new location, it is updated in the main controller, but when I switch to MapViewController, the pin is still pinned to the default lat and long (43.161030, -77.610922). Am I doing this right and just need to update/refresh the mapView/Pin, or is there another approach to this?
Thanks you advance!
UPDATE:
Here is the MainViewController:
class MainViewController: UIViewController {
var latPassed = Double()
var LongPassed = Double()
// These are passed from LocationViewController
var coordinates: (latitude: Double, longitude: Double) = (43.161030,-77.610922)
// These are the default coordinates
func loadWeatherTemp() {
let forecastConstants = ForecastConstants(apiKey: forecastAPIKey)
coordinates.latitude = latPassed
coordinates.longitude = LongPassed
if latPassed == 0 && LongPassed == 0 {
coordinates.latitude = 43.161030
coordinates.longitude = -77.610922
}
forecastConstants.getForecast(latitude: coordinates.latitude, longitude: coordinates.longitude) { (currentWeather) in
if let currentWeather = currentWeather {
DispatchQueue.main.async {
// stuff
}
}
}
// more stuff
}
I'd prefer to have a coordinate object to have in MapViewController() and set the new coordinates from MainViewController(). I'm telling Something like below:
MapViewController.swift
var newCoordinates = CLLocationCoordinate2D() {
didSet {
let region = MKCoordinateRegionMakeWithDistance(newCoordinates, 1000, 1000)
mapView.setRegion(region, animated: true)
pin = AnnotationPin(title: "Load temp here", subtitle: "", coordinate: coord)
mapView.addAnnotation(pin)
mapView.selectAnnotation(pin, animated: true)
}
UPDATE: -
MainViewController.swift
class MainViewController: UIViewController {
var latPassed = Double()
var LongPassed = Double()
weak var mapVC = MapViewController()//this will be your mapvc which is loaded in your tabbar.
var coordinates: (latitude: Double, longitude: Double) = (43.161030,-77.610922) {
didSet {
mapVC.newCoordinates = self.coordinates // Every time you update your coordinates here, send that coordinate to MapViewController() so that it stays same.
}
}
func loadWeatherTemp() {
let forecastConstants = ForecastConstants(apiKey: forecastAPIKey)
coordinates.latitude = latPassed
coordinates.longitude = LongPassed
if latPassed == 0 && LongPassed == 0 {
coordinates.latitude = 43.161030
coordinates.longitude = -77.610922
}
forecastConstants.getForecast(latitude: coordinates.latitude, longitude: coordinates.longitude) { (currentWeather) in
if let currentWeather = currentWeather {
DispatchQueue.main.async {
// stuff
}
}
}
}
Sorry for late reply.
Hope it helps!

Passing custom data with map pin segue

I've been having trouble figuring out how to pass a custom variable in a map pin to another view controller in Swift. I know that passing the coordinates, title, and subtitle are available when you addAnnotation. I would like to try and pass a custom variable but hidden. Is there such a thing? Below I am getting the users location, mapping it, dropping a pin of a couple locations nearby with annotations which goes to another view controller and passes just the title and subtitle. Any insight is greatly appreciated.
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var mappedCity = String()
var mappedState = String()
var manager = CLLocationManager()
var annotation:MKAnnotation!
var error:NSError!
var pointAnnotation:MKPointAnnotation!
var pinAnnotationView:MKPinAnnotationView!
var selectedAnnotation: MKPointAnnotation!
private var mapChangedFromUserInteraction = false
#IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.mapView.delegate = self
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
if CLLocationManager.locationServicesEnabled(){
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0]
let latitude = userLocation.coordinate.latitude
let longitude = userLocation.coordinate.longitude
let latDelta:CLLocationDegrees = 0.05
let lonDelta:CLLocationDegrees = 0.05
let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
self.mapView.setRegion(region, animated: true)
self.mapView.showsUserLocation = true
CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) in
if (error != nil){
print(error)
}else {
if let p = placemarks?[0]{
let locality = p.locality ?? ""
let administrativeArea = p.administrativeArea ?? ""
self.mappedCity = String(locality)
self.mappedState = String(administrativeArea)
self.parseJSON("\(locality)", state: "\(administrativeArea)")
}
}
}
self.manager.stopUpdatingLocation()
}
func parseJSON(city: String, state: String){
let passedCity = city
let passedState = state
let escapedCity = passedCity.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
let escapedState = passedState.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
let url = NSURL(string:"http://www.API.com/api.php?city=\(escapedCity)&stateAbv=\(escapedState)")!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url) { (items, response, error) -> Void in
if error != nil {
print(error)
}else {
if let items = items {
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(items, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
if jsonResult.count > 0 {
if let datas = jsonResult["data"] as? NSArray{
for data in datas{
if let title = data["title"] as? String {
if let street = data["street"] as? String {
if let city = data["city"] as? String {
if let stateAbv = data["stateAbv"] as? String {
if let zip = data["zip"] as? String {
self.geoAddress("\(title)", street: "\(street)", city: "\(city)", state: "\(stateAbv)", zip: "\(zip)")
}
}
}
}
}
}
}
}
} catch{}
}
}
}
task.resume()
}
func geoAddress(title: String, street: String, city: String, state: String, zip: String){
let storeName = "\(title)"
let location = "\(street) \(city) \(state) \(zip)"
let geocoder = CLGeocoder();
geocoder.geocodeAddressString(location, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
if (error != nil) {
print("Error \(error!)")
} else if let placemark = placemarks?[0] {
let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
let pointAnnotation:MKPointAnnotation = MKPointAnnotation()
pointAnnotation.coordinate = coordinates
pointAnnotation.title = storeName
pointAnnotation.subtitle = location
self.mapView.addAnnotation(pointAnnotation)
}
})
}
private func mapViewRegionDidChangeFromUserInteraction() -> Bool {
let view: UIView = self.mapView.subviews[0] as UIView
// Look through gesture recognizers to determine whether this region change is from user interaction
if let gestureRecognizers = view.gestureRecognizers {
for recognizer in gestureRecognizers {
if( recognizer.state == UIGestureRecognizerState.Began || recognizer.state == UIGestureRecognizerState.Ended ) {
return true
}
}
}
return false
}
func mapView(mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
mapChangedFromUserInteraction = mapViewRegionDidChangeFromUserInteraction()
if (mapChangedFromUserInteraction) {
// user changed map region
}
}
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
if (mapChangedFromUserInteraction) {
// user changed map region
let center = mapView.centerCoordinate
let mapLatitude = center.latitude
let mapLongitude = center.longitude
let locationmove = CLLocation(latitude: mapLatitude, longitude: mapLongitude)
CLGeocoder().reverseGeocodeLocation(locationmove) { (placemarks, error) in
if (error != nil){
print(error)
}else {
if let p = placemarks?[0]{
let locality = p.locality ?? ""
let administrativeArea = p.administrativeArea ?? ""
self.mappedCity = String(locality)
self.mappedState = String(administrativeArea)
self.parseJSON("\(locality)", state: "\(administrativeArea)")
}
}
}
}
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView?.animatesDrop = false
pinView?.canShowCallout = true
pinView?.draggable = true
pinView?.pinTintColor = UIColor.greenColor()
let rightButton: AnyObject! = UIButton(type: UIButtonType.DetailDisclosure)
pinView?.rightCalloutAccessoryView = rightButton as? UIView
}
else {
pinView?.annotation = annotation
}
return pinView
}
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == view.rightCalloutAccessoryView {
selectedAnnotation = view.annotation as? MKPointAnnotation
performSegueWithIdentifier("Details", sender: self)
}
}
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
if newState == MKAnnotationViewDragState.Ending {
let droppedAt = view.annotation?.coordinate
print(droppedAt)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "Details"){
let myDetails = segue.destinationViewController as! DetailViewController
myDetails.mytitle = selectedAnnotation.title
myDetails.mysubtitle = selectedAnnotation.subtitle
}
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
}
}
Subclass the "MKPointAnnotation" class and add your custom property in it.
class MyAnnotation : MKPointAnnotation {
var customProperty : String?
}
And you can use MyAnnotation instead of MKPointAnnotation. Like following
let pointAnnotation:MyAnnotation = MyAnnotation()
pointAnnotation.coordinate = coordinates
pointAnnotation.title = storeName
pointAnnotation.subtitle = location
pointAnnotation.customProperty = "your value"
self.mapView.addAnnotation(pointAnnotation)

Why are my annotations not appearing in my map view?

I have created a set of "IssueLocation"'s, a class which conforms to the MKAnnotation protocol. Why are my annotations (built from API JSON data) not appearing in my map view ?
Here is the code behind it all:
Class code:
class IssueLocation: NSObject, MKAnnotation {
var locationName: String
var campusName: String
var latitude: Double
var longitude: Double
var coordinate: CLLocationCoordinate2D
init(locationName: String, campusName: String, latitude: Double, longitude: Double, coordinate: CLLocationCoordinate2D) {
self.locationName = locationName
self.campusName = campusName
self.latitude = latitude
self.longitude = longitude
self.coordinate = coordinate
super.init()
}
var subtitle: String? {
if (latitude == 44.22438242146097) {
return "West Campus"
} else {
return "Main Campus"
}
}
var title: String? {
return locationName
}
func mapItem() -> MKMapItem {
let addressDictionary = [String(kABPersonAddressStreetKey): locationName]
let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDictionary)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = locationName
return mapItem
}
}
Creating a set of IssueLocations:
func populateMapObjects() {
if populatingMapObjects {
return
}
populatingMapObjects = true
self.loadingIndicator.startAnimating()
var index = 0
Alamofire.request(GWNetworking.Router.MapObjects).responseJSON() { response in
if let JSON = response.result.value {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)) {
/* Making an array of all the node IDs from the JSON file */
if (JSON .isKindOfClass(NSArray)) {
for _ in JSON as! [Dictionary<String,AnyObject>] {
if let issueLocation: IssueLocation = IssueLocation(locationName: "Center of the universe", campusName: "Queen's University", latitude: 44.22661586877309, longitude: -76.49380087852478, coordinate: CLLocationCoordinate2D(latitude: 44.22661586877309, longitude: -76.49380087852478)) {
if let locationName = JSON[index]["name"] as? String {
issueLocation.locationName = locationName
}
if let latitude = JSON[index]["coordinates"]!![1] as? Double {
issueLocation.latitude = latitude
}
if let longitude = JSON[index]["coordinates"]!![0] as? Double {
issueLocation.longitude = longitude
}
issueLocation.coordinate = CLLocationCoordinate2D(latitude: issueLocation.latitude, longitude: issueLocation.longitude)
index = index+1
self.mapObjects.append(issueLocation)
}
}
}
dispatch_async(dispatch_get_main_queue()) {
self.loadingIndicator.stopAnimating()
self.populatingMapObjects = false
print(self.mapObjects.count)
}
}
}
}
}
And finally, this is where I try to add the annotations to the map view:
func loadObjectsIntoMapView() {
for mapObject in mapObjects {
let temporaryMapAnnotation = IssueLocation(locationName: mapObject.locationName, campusName: "Main Campus", latitude: mapObject.latitude, longitude: mapObject.longitude, coordinate: mapObject.coordinate)
if (temporaryMapAnnotation.longitude < -76.50921821594238) {
temporaryMapAnnotation.campusName = "West Campus"
}
self.mapView.addAnnotation(temporaryMapAnnotation)
}
}
Move loadObjectsIntoMapView() into this block:
dispatch_async(dispatch_get_main_queue()) {
self.loadingIndicator.stopAnimating()
self.populatingMapObjects = false
print(self.mapObjects.count)
}
Calling loadObjectsIntoMapView() in viewDidLoad will have it execute immediately and the data has not come down from the server yet so you will have no mapObject in mapObjects hence no annotations.

Annotation pins reset after segue back to original map (Mapkit Swift)

I am using MapKit's local search feature to populate a bunch of pin annotations based on a user's search. When you click on a pin, in the callout you go to a detail viewcontroller of the location. When the user segues and clicks the back button on the toolbar from the detailviewcontroller to the previous viewcontroller, all the annotation pins are gone. Is there any way to avoid this?
here is my code for the localsearch:
#IBAction func returnText(sender: AnyObject) {
sender.resignFirstResponder();
attractionsMap.removeAnnotations(attractionsMap.annotations);
performSearch();
}
func performSearch() {
matchingItems.removeAll()
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchText.text;
request.region = attractionsMap.region;
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler({(response:
MKLocalSearchResponse!,
error: NSError!) in
if error != nil {
println("Error occured in search: \(error.localizedDescription)")
} else if response.mapItems.count == 0 {
println("No matches found")
} else {
println("Matches found")
for item in response.mapItems as! [MKMapItem] {
println("Name = \(item.name)")
println("Phone = \(item.phoneNumber)")
matchingItems.append(item as MKMapItem)
println("Matching items = \(matchingItems.count)")
var placemark = item.placemark;
var subThoroughfare:String = "";
var thoroughfare:String = "";
var locality:String = "";
var postalCode:String = "";
var administrativeArea:String = "";
var country:String = "";
var title = "";
var subtitle = "";
if (placemark.subThoroughfare != nil) {
subThoroughfare = placemark.subThoroughfare;
}
if(placemark.thoroughfare != nil) {
thoroughfare = placemark.thoroughfare;
}
if(placemark.locality != nil) {
locality = placemark.locality;
}
if(placemark.postalCode != nil) {
postalCode = placemark.postalCode;
}
if(placemark.administrativeArea != nil) {
administrativeArea = placemark.administrativeArea;
}
if(placemark.country != nil) {
country = placemark.country;
}
println("viewcontroller placmark data:");
println(locality);
println(postalCode);
println(administrativeArea);
println(country);
title = " \(subThoroughfare) \(thoroughfare) \n \(locality), \(administrativeArea) \n \(postalCode)\(country)";
subtitle = " \(subThoroughfare) \(thoroughfare)";
println(title);
var annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name + " " + subtitle;
self.attractionsMap.addAnnotation(annotation)
}
}
})
}
I then pass the placemark of the matching items through this method...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var attractionsDetailViewController:AttractionsDetailViewController = segue.destinationViewController as! AttractionsDetailViewController
attractionsDetailViewController.attractionLocation = indicatedMapItem;
}
ANy help would be greatly appreciated.
You can make use of NSUserDefaults, "used to determine an application’s default state". Have a look at: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/

How to add a button to the MKPointAnnotation?

I've just got stuck trying to add a detail button to my annotation point, unfortunately I don't know how to do it. Does anyone could help me with that?
The image below presents what I'd like to achieve. Thanks!
MapKitViewController:
import UIKit
import MapKit
import CoreLocation
class MapKitViewController: UIViewController, MKMapViewDelegate
{
let locationManager = CLLocationManager()
#IBOutlet weak var nmapView: MKMapView!
override func viewDidLoad()
{
super.viewDidLoad()
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
let location = CLLocationCoordinate2D(
latitude: 53.4265107,
longitude: 14.5520357)
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegion(center: location, span: span)
nmapView.setRegion(region, animated: true)
nmapView.showsPointsOfInterest = false
nmapView.showsUserLocation = true
displayMarkers()
}
func displayMarkers() -> Void
{
let jsonURL: NSURL = NSURL(string: "http://jsonstring.com/")!
var dataFromNetwork: NSData = NSData(contentsOfURL: jsonURL)!
let json = JSON(data: dataFromNetwork)
var jsonSize = json.count
var todaysDate:NSDate = NSDate()
var dateFormatter:NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
var formattedDate:String = dateFormatter.stringFromDate(todaysDate)
let annotationView = MKAnnotationView()
let detailButton: UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton
annotationView.rightCalloutAccessoryView = detailButton
for(var i = 0; i < jsonSize; i++)
{
if(json[i]["rozpoczecie"].stringValue == formattedDate)
{
let clubID = json[i]["id_klub"].stringValue
let annotation = MKPointAnnotation()
let (resultSet, err) = SD.executeQuery("SELECT * FROM Clubs WHERE ID = ?", withArgs: [clubID])
if(err != nil){println("blad")}
else
{
for row in resultSet
{
let name = row["Name"]?.asString()
let latitude = row["Latitude"]?.asDouble()
let longitude = row["Longitude"]?.asDouble()
annotation.title = name
var markerLatitude: Double = latitude!
var markerLongitude: Double = longitude!
let location = CLLocationCoordinate2D(latitude: markerLatitude, longitude: markerLongitude)
annotation.setCoordinate(location)
annotation.subtitle = json[i]["nazwa"].stringValue
}
nmapView.addAnnotation(annotation)
}
}
}
}
You are doing it right.You just need to have these methods implemented for adding button along with title and subtitle
iOS 8 and Xcode 6
import UIKit
import MapKit
import CoreLocation
class MapKitViewController: UIViewController, MKMapViewDelegate
{
let locationManager = CLLocationManager()
#IBOutlet weak var nmapView: MKMapView!
override func viewDidLoad()
{
super.viewDidLoad()
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
let location = CLLocationCoordinate2D(
latitude: 53.4265107,
longitude: 14.5520357)
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegion(center: location, span: span)
nmapView.setRegion(region, animated: true)
nmapView.showsPointsOfInterest = false
nmapView.showsUserLocation = true
displayMarkers()
}
// When user taps on the disclosure button you can perform a segue to navigate to another view controller
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
if control == view.rightCalloutAccessoryView{
println(view.annotation.title) // annotation's title
println(view.annotation.subtitle) // annotation's subttitle
//Perform a segue here to navigate to another viewcontroller
// On tapping the disclosure button you will get here
}
}
// Here we add disclosure button inside annotation window
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
println("viewForannotation")
if annotation is MKUserLocation {
//return nil
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
//println("Pinview was nil")
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
}
var button = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton // button with info sign in it
pinView?.rightCalloutAccessoryView = button
return pinView
}
func displayMarkers() -> Void
{
let jsonURL: NSURL = NSURL(string: "http://atnight.wtznc.com/json.php")!
var dataFromNetwork: NSData = NSData(contentsOfURL: jsonURL)!
let json = JSON(data: dataFromNetwork)
var jsonSize = json.count
var todaysDate:NSDate = NSDate()
var dateFormatter:NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
var formattedDate:String = dateFormatter.stringFromDate(todaysDate)
let annotationView = MKAnnotationView()
// Adding button here wont do anything so remove these two lines
let detailButton: UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton
annotationView.rightCalloutAccessoryView = detailButton
// For adding button we have to use a method named as viewForAnnotation
for(var i = 0; i < jsonSize; i++)
{
if(json[i]["rozpoczecie"].stringValue == formattedDate)
{
let clubID = json[i]["id_klub"].stringValue
let annotation = MKPointAnnotation()
let (resultSet, err) = SD.executeQuery("SELECT * FROM Clubs WHERE ID = ?", withArgs: [clubID])
if(err != nil){println("blad")}
else
{
for row in resultSet
{
let name = row["Name"]?.asString()
let latitude = row["Latitude"]?.asDouble()
let longitude = row["Longitude"]?.asDouble()
annotation.title = name
var markerLatitude: Double = latitude!
var markerLongitude: Double = longitude!
let location = CLLocationCoordinate2D(latitude: markerLatitude, longitude: markerLongitude)
annotation.setCoordinate(location)
annotation.subtitle = json[i]["nazwa"].stringValue
}
nmapView.addAnnotation(annotation)
}
}
}
}
}
Check out my output.

Resources