0

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!

Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
Scott B
  • 219
  • 2
  • 3
  • 11

2 Answers2

1

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)!)
gvuksic
  • 2,983
  • 3
  • 32
  • 37
0

Please refer to this documentation, it contains examples of Apple Maps queries that search location in the Maps app.

https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html

But the problem here is that most likely Apple Maps don't search by ZIP-code. So at first you need to get location coordinates or address of postcode entered with the help of third-party API.

You may find example here

iOS Find Location (Latitude,longitude) from Zipcode

Community
  • 1
  • 1
Alexander Tkachenko
  • 3,221
  • 1
  • 33
  • 47