Scott Scott - 3 months ago 15
Swift Question

Xcode Swift - View entered Postcode on Apples map

i'm after for some help please!

I've got a textField called 'textFieldPostcode' and a

UIButton
called 'btnViewOnMap'

my idea is that when the user enters their postcode into the
textFieldPostcode
, and presses
BtnViewOnMap
button, it will take the user to Apples Built in maps and show the postcode that was entered.

I've seen a few tutorials on how to open Apples build-in Maps with code, which works

Code:

@IBOutlet weak var textFieldPostcode: UITextField?
@IBOutlet weak var btnViewOnMap: UIButton?


ViewDidLoad()

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(viewOnMapPress))
btnViewOnMap?.addGestureRecognizer(tapGesture)


Function for the viewOnMapPress:

func viewOnMapPress(sender: UITapGestureRecognizer){
if (buttonError != nil)
{
SVProgressHUD.showErrorWithStatus(buttonError?.localizedDescription)
}
else{
UIApplication.sharedApplication().openURL(NSURL(string: "http://maps.apple.com/?")!)

}
}


the line below opens the maps, and shows a location, but how can i change this so that it searches for the users input instead ?

UIApplication.sharedApplication().openURL(NSURL(string: "http://maps.apple.com/?")!)


Thanks in advance!

Answer

Try this (send postcode as query parameter to map):

let stringURLSearchPostCode = "http://maps.apple.com/?q=" + self.textFieldPostcode.text
UIApplication.sharedApplication().openURL(NSURL(string: stringURLSearchPostCode)!)