Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Finding the specific venue at a current location using foursquare API in swift

I'm newer to coding in swift and I'm working on finding a specific venue that corresponds to my current location using the foursquare API in the swift language. I've got it down to a cluster of venues around my location, but it only works sometimes when I run the simulator. Can someone please help me out? I have two ViewControllers. The first one:

import Foundation

import UIKit

import CoreLocation

let API_URL = "https://api.foursquare.com/v2/"

let CLIENT_ID = ""

let CLIENT_SECRET = ""

class FourSquareRequest: NSObject {

class func requestVenuesWithLocation(location: CLLocation) -> [AnyObject] {

let requestString = "\(API_URL)venues/search?client_id=\(CLIENT_ID)&client_secret=\(CLIENT_SECRET)&v=20130815&ll=\(location.coordinate.latitude),\(location.coordinate.longitude)"

println(requestString)

if let url = NSURL(string: requestString) {

    let request = NSURLRequest(URL: url)

    if let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil) {

        if let returnInfo = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: nil) as? [String:AnyObject] {

            let responseInfo = returnInfo["response"] as [String:AnyObject]

            let venues = responseInfo["venues"] as [[String:AnyObject]]

            return venues

        }



    }



}


return []

} }

And the second ViewController:

import UIKit

import MapKit

import CoreLocation

var onceToken: dispatch_once_t = 0

class VenueViewController: UIViewController, CLLocationManagerDelegate {

var lManager = CLLocationManager() var mapView = MKMapView()

override func viewDidLoad() { super.viewDidLoad()

mapView.frame = view.frame
view.addSubview(mapView)

lManager.requestWhenInUseAuthorization()

lManager.delegate = self

lManager.desiredAccuracy = kCLLocationAccuracyBest
lManager.distanceFilter = kCLDistanceFilterNone

lManager.startUpdatingLocation()

}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

dispatch_once(&onceToken) { () -> Void in

    println(locations.last)

    if let location = locations.last as? CLLocation {

        let span = MKCoordinateSpanMake(0.1, 0.1)
        let region = MKCoordinateRegion(center: location.coordinate, span: span)

        self.mapView.setRegion(region, animated: true)

        let venues = FourSquareRequest.requestVenuesWithLocation(location)

        println(venues)

        self.createAnnotationsWithVenues(venues)

        // request to foursquare for venues with location

    }

}

lManager.stopUpdatingLocation()

}

func createAnnotationsWithVenues(venues: [AnyObject]) {

for venue in venues as [[String:AnyObject]] {

    let locationInfo = venue["location"] as [String:AnyObject]

    let lat = locationInfo["lat"] as CLLocationDegrees
    let lng = locationInfo["lng"] as CLLocationDegrees

    let coordinate = CLLocationCoordinate2DMake(lat, lng)

    let annotation = MKPointAnnotation()
    annotation.setCoordinate(coordinate)

    mapView.addAnnotation(annotation)

}

} }