[Xcode10 实际操作]八、网络与多线程-(6)使用UIApplication对象打开地图

本文将演示如何使用应用程序单例对象,打开地图的功能。

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

 1 import UIKit
 2 
 3 class ViewController: UIViewController {
 4 
 5     override func viewDidLoad() {
 6         super.viewDidLoad()
 7         // Do any additional setup after loading the view, typically from a nib.
 8         //调用方法,打开地图
 9         openMap()
10     }
11     
12     //创建一个方法,实现打开地图的功能
13     func openMap()
14     {
15         //首先创建一个字符串变量,设置地图显示的目标地理位置
16         var address = "Shenzhen"
17         //对字符串进行编码,使地图能够解析到正确的地理位置
18         address = address.addingPercentEncoding(withAllowedCharacters: CharacterSet())!
19         //创建一个字符串常量,调用谷歌地图的相关接口
20         let urlText = "https://maps.google.cn/maps?q=\(address)"
21         //然后将字符串转换为网址对象
22         let url = URL(string: urlText)
23         //获取应用程序单例对象,
24         //使用它打开网页的功能,
25         //打开指定网址的网页。
26         UIApplication.shared.openURL(url!)
27     }
28 
29     override func didReceiveMemoryWarning() {
30         super.didReceiveMemoryWarning()
31         // Dispose of any resources that can be recreated.
32     }
33 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10055002.html
今日推荐