0

Having some trouble getting the annotation to appear on the map. The address string is getting passed in from the previous ViewController. Everything working fine up to pushing the annotation to the map. Any suggestions?

class PostViewController: UIViewController {
@IBOutlet weak var locationInput: UITextField!
@IBOutlet weak var debugText: UILabel!


@IBAction func postDirectionTouched(sender: AnyObject) {
    if locationInput.text!.isEmpty {
        debugText.text = "Location Input Empty."
    } else {
        nextSegue()
    }

}

func nextSegue() {
    dispatch_async(dispatch_get_main_queue(), {
        let controller = self.storyboard!.instantiateViewControllerWithIdentifier("LinkPostController") as! LinkPostViewController
        controller.address = self.locationInput.text!
        self.presentViewController(controller, animated: true, completion: nil)
    })
}

}

import UIKit
import CoreLocation
import AddressBook
import MapKit

class LinkPostViewController: UIViewController, MKMapViewDelegate {

@IBOutlet weak var pointMap: MKMapView!
@IBOutlet weak var linkInput: UITextField!
@IBOutlet weak var debugText: UILabel!
var address: String = ""
var coords: CLLocationCoordinate2D?

override func viewDidLoad() {
    super.viewDidLoad()

    self.mapLocation()

}

func mapLocation(){
    let geocoder = CLGeocoder()

    geocoder.geocodeAddressString(self.address, completionHandler: {(placemark, error) -> Void in
        if error != nil{
            print("Geocode failed with error: \(error!.localizedDescription)")
        } else if placemark!.count > 0 {
            let pm = placemark?[0]
            let location = pm!.location
            self.coords = location!.coordinate

            print(self.coords!)

            self.showMap()


        }

    })
}

func showMap() {
        print("last")
        let coordinates = self.coords
        let pinn = MKPointAnnotation()
        pinn.title = "hello"
        pinn.subtitle = "This my pin."
        pinn.coordinate = coordinates!



        dispatch_async(dispatch_get_main_queue()) {
            self.pointMap.addAnnotation(pinn)
        }
   }
func mapView(mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {}
}
baconck
  • 386
  • 2
  • 5
  • 19
  • 1
    is the delegate method `mapView:viewForAnnotation:` implemented? – vadian Oct 19 '15 at 07:35
  • The annotation appears from another viewController or are you tapping to create an annotation? In other words: you are tapping on an address in a different VC and then it appears in the map VC? – Lukesivi Oct 19 '15 at 07:43
  • address is a string. mapLocation() uses **address** to coordinates and triggers showMap() – baconck Oct 19 '15 at 07:45
  • From a TableViewViewController? If so, can you update the question and add that code? – Lukesivi Oct 19 '15 at 07:46
  • and no I don't have mapView:viewForAnnotation: implemented. I have used mapkit with annoatations before without it. – baconck Oct 19 '15 at 07:58
  • How are you finding the coordinates from the string to match a location on the map? @CBaco – Lukesivi Oct 19 '15 at 08:04
  • I'm having trouble understanding. I'd recommend updating your question with more information on how you're accessing the data, passing the data (particularly the location's coordinates), etc. For now, which is obvious, it seems like you're missing extracting the coordinates from the location you're adding to be added in the mapView, which thus, should generate an annotation. In the meantime, I've built something similar. Check out these 2 gists, hopefully they help: https://gist.github.com/rinyfo4/b98559563aaf6c02b3ab https://gist.github.com/rinyfo4/1f259ddbc67ee8f438ec – Lukesivi Oct 19 '15 at 08:10
  • @rinyfo4 I am using ` geocoder.geocodeAddressString` to find the lat long – baconck Oct 19 '15 at 08:10

1 Answers1

1

Simplest working example, adds an annotation in Australia:

import UIKit
import MapKit

class ViewController: UIViewController {

    // MARK: - Private variables

    private var mapView: MKMapView?

    // MARK: - UIViewController methods

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        mapView = MKMapView(frame: view.frame)
        view.addSubview(mapView!)
        let locationCoordinate = CLLocationCoordinate2DMake(-25.27, 133.775)
        let pointAnnotation = MKPointAnnotation()
        pointAnnotation.coordinate = locationCoordinate
        mapView?.addAnnotation(pointAnnotation)
    }
}

check placemark!.count > 0 (your code looks correct)

Gary Davies
  • 920
  • 15
  • 12