iOS apps utilize uniform resource locators (URLs), a form of uniform resource identifiers (URIs), to communicate with system apps. By calling the OpenURL(NSUrl url)
method on the main application class, it is possible to launch the Maps application already focused on a specific location.
First, we must generate a
NSUrl
starting with the path"http://maps.apple.com/?q="
appended to the location we wish to bring up in Maps. For this example, lets place the following code in thecallMap
TouchUpInside
event handler:String cityAddress = cityText.Text.Replace (' ', '+'); var url = new NSUrl ("http://maps.apple.com/?q="+ cityAddress);
Since the
NSUrl
cannot contain space characters, we replace them with'+'
characters.To open Maps, we would place the following code after we generate ourNSUrl
variable:<pre><code>UIApplication.SharedApplication.OpenUrl (url);</code></pre>
This code will cause the Maps application to open, searching for the location entered in the
cityText
UILabel.
There are a variety of parameters that can be appended onto "http://maps.apple.com/?"
that can specify the information loaded by the Maps app. In this example, we used the query parameter, "q="
, which loads the following string as if it had been typed into the query box in the Maps app. To see all available parameters, see the Apple URL Scheme Reference