Reputation: 689
I have this app where it randomly selects a state. Lets say its New York, how would I take the name New York and press a button to search for it in the maps. I don't want the user to have to type it in, I want it to be so I press a button and it searches. I already have the maps setup but im having trouble with this part.
func randomStateGenerator() {
var randomState = Int(arc4random() % 50)
println(randomState)
if randomState == 0 {
println("NEW YORK")
newyork = SKLabelNode(fontNamed: "Happy Phantom")
newyork.text = "newyork"
newyork.zPosition = 22
newyork.fontSize = 25
newyork.fontColor = SKColor.blackColor()
newyork.position = CGPointMake(self.size.width / 2.0, self.size.height / 1.8)
self.addChild(newyork)
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
var touch: UITouch = touches.first as! UITouch
var location = touch.locationInNode(self)
var node = self.nodeAtPoint(location)
if node.name == "find" {
}
}
}
Upvotes: 0
Views: 33
Reputation: 11127
Use this function to get the Lat Long of the address which you have passed
func geoCodeUsingAddress(address: String) -> CLLocationCoordinate2D {
var latitude: Double = 0
var longitude: Double = 0
var esc_addr: String = address.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
var req: String = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=\(esc_addr)"
var result: String = NSString.stringWithContentsOfURL(NSURL.URLWithString(req), encoding: NSUTF8StringEncoding, error: nil)
if result {
var scanner: NSScanner = NSScanner.scannerWithString(result)
if scanner.scanUpToString("\"lat\" :", intoString: nil) && scanner.scanString("\"lat\" :", intoString: nil) {
scanner.scanDouble(&latitude)
if scanner.scanUpToString("\"lng\" :", intoString: nil) && scanner.scanString("\"lng\" :", intoString: nil) {
scanner.scanDouble(&longitude)
}
}
}
var center: CLLocationCoordinate2D
center.latitude = latitude
center.longitude = longitude
return center
}
This function will return the lat long, with the help of lat long show it in your map.
Upvotes: 1