4

I'm attempting to geocode a string into a lat/lon in the swift playground. Here is my code:

import CoreLocation

var geocoder = CLGeocoder()
geocoder.geocodeAddressString("San Francisco, CA", {(placemarks, error)->Void in
    println("here")
})

However, here never gets printed to the console (console output is blank). Why?

Shruti Thombre
  • 1,009
  • 3
  • 11
  • 27
Zain
  • 639
  • 1
  • 7
  • 13
  • 1
    possible duplicate of [How do I run Asynchronous callbacks in Playground](http://stackoverflow.com/questions/24058336/how-do-i-run-asynchronous-callbacks-in-playground) – rickster Aug 17 '14 at 05:04
  • 1
    This is an asynchronous API. The playground process normally stops running after the last line executes, so there's no time for the geocoder to get a response and call your completion handler. See the duplicate for more. – rickster Aug 17 '14 at 05:05
  • That's it! Thanks a lot, @rickster. – Zain Aug 17 '14 at 05:38
  • @Zain, i am having the same issue...how can i resolve this one? – dhaval shah Jan 08 '15 at 07:02
  • I also have the same issue. No matter how to set the inRegion parameter. It's always return locations based my current position. @dhavalshah – ackratos Jan 18 '15 at 11:15
  • hi @ackratos... i have a issue because of my VPN connections...When my VPN is turned on...my net was not working....So please check the net...if net is up than your google map will work perfectly...Let me know if u still face an issue...i ll put my code – dhaval shah Jan 19 '15 at 09:47

1 Answers1

8

I was having the same issue. While there are some hints to how this is fixed in other posts, I will lay it out here so that others who find this will have something to chew on.

//: Playground - noun: a place where people can play

import UIKit
import CoreLocation
import XCPlayground

var geocoder = CLGeocoder()
var address = "Epcot Center Dr, Orlando FL"


geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [AnyObject]!, error: NSError!) -> Void in

    println(error)

    if let placemark = placemarks?[0] as? CLPlacemark {

        println(placemark.location.coordinate.latitude)
        println(placemark.location.coordinate.longitude)

    }

})


XCPSetExecutionShouldContinueIndefinitely()
Christopher Wade Cantley
  • 6,472
  • 4
  • 31
  • 48