1

I have the following function to determine the latitude/longitude of an address contained in a text field (searchAddress) to create a map region with this location as it's center and then add annotations firstly for this location and secondly for store locations that are stored in a Parse backend.

When the map view is first opened the searchAddress text field is preset with the users default address and the initial call to the function from viewDidLoad. It works as I had intended. However the user can then type a different address in this field and click a "Search" button which then also calls the same function and the second time. It works occasionally but generally does not display the annotations.

If I click "Search" again so the region does not change then the annotations are displayed correctly. I assume this is a timing issue and related to the time it takes to redraw the map when I change the region, as sometimes the annotations appear the first time but mostly they don't. And if I repeat the search without changing the address it works fine. Thanks in advance, I have been stuck on this and can't find a solution. Here's the code:

func addMapAnotations () {
        // var searchLocation = preferredDeliveryLocation.text
        var geocoder = CLGeocoder()
        var address = CLLocationCoordinate2D()
        geocoder.geocodeAddressString(searchAddress.text, completionHandler: {
            (placemarks: [AnyObject]!, error: NSError!) -> Void in
            if let placemark = placemarks?[0] as? CLPlacemark {
                var latDelta: CLLocationDegrees = 0.1
                var lonDelta: CLLocationDegrees = 0.1
                var span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
                address = placemark.location.coordinate
                println(placemark.location.coordinate.latitude)
                println("Inside geocoder lititude: \(address.latitude)")
                println("Inside geocoder longitude: \(address.longitude)")
                var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(address.latitude, address.longitude)
                var region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
                self.map.setRegion(region, animated: true)

                var tempLocationObject = self.currentUser
                var searchAnnotation = NewCustomPointAnnotation(coordinate: address, title: "", subtitle: "", locationObject: tempLocationObject!, locationCategory: "")
                self.annotationCategory = "homeLocation"
                searchAnnotation.locationCategory = "homeLocation"
                searchAnnotation.coordinate = address
                searchAnnotation.title = "Search address"
                searchAnnotation.subtitle = "Search for delivery locations near by..."
                // self.map.addAnnotation(MKPlacemark(placemark: placemark))
                self.map.addAnnotation(searchAnnotation)
            }
        })

        self.queryCurrent.whereKey("country", equalTo: "Australia")
        // self.queryCurrent.whereKey("State", equalTo: state)
        self.queryCurrent.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in
            if error == nil {
                if let objects = objects as? [PFObject] {
                    for object in objects {
                        println(object.objectId)
                        var latitude = object["latitude"] as! CLLocationDegrees
                        var longitude = object["longitude"] as! CLLocationDegrees
                        var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
                        var deliveryAnnotation = NewCustomPointAnnotation(coordinate: location, title: "", subtitle: "", locationObject: object, locationCategory: "")
                        self.annotationCategory = "deliveryLocation"
                        deliveryAnnotation.locationCategory = "deliveryLocation"
                        deliveryAnnotation.coordinate = location
                        deliveryAnnotation.title = object["name"] as! String
                        deliveryAnnotation.subtitle = "Click to select this location"
                        deliveryAnnotation.locationObject = object
                        println("Annotation 1: \(deliveryAnnotation.description)")
                        println("Object ID: \(deliveryAnnotation.locationObject.objectId)")
                        self.map.addAnnotation(deliveryAnnotation)
                    }
                }

            } else {
                println("No locations found")
            }
        }
    }
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
  • I have fixed this problem by adding dequeueReusableAnnotationViewWithIdentifier logic to my viewForAnnotation delegate and the pins are now displaying all the time. [link](http://stackoverflow.com/questions/25202613/mapkit-in-swift-part-2) – Lachlan Reid May 18 '15 at 23:09

0 Answers0