将地址 (UITextView) 连接到 Apple 地图
Connecting an address (UITextView) to the Apple Maps
我正在尝试实现一些功能:
我想要一个 UITextView,恰好是某个地方的地址,当用户点击这个 UITextView 时将用户引导到 Apple 地图,并在 UITextView 中显示地址的位置。关于我该怎么做的任何想法?
到目前为止我尝试过的事情:
import MapKit
@IBOutlet weak var firstAddressTapped: UITextView!
func viewDidLoad() {
let attributedString = NSMutableAttributedString(string: "The address of my place")
attributedString.addAttribute(.link, value: "http://maps.apple.com/?daddr=The+address+of+my+place", range: NSRange(location: 19, length: 55))
firstAddressTapped.attributedText = attributedString
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
UIApplication.shared.open(URL)
return false
}
谢谢!
你可以这样传递地址;
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
UIApplication.shared.open(URL)
// check if user touched the link.
if range == NSRange(location: 19, length: 55) {
UIApplication.sharedApplication().openURL(NSURL(string: "http://maps.apple.com/?address=The+address+of+my+place")!)
}
return false
}
Alexander Adelmaer 在 Medium 上的一个很棒的简单教程帮助我解决了我的问题!享受。
https://medium.com/app-makers/how-to-add-a-tap-gesture-to-uilabel-in-xcode-swift-7ada58f1664
我做错了什么:
当我应该将它添加到视图控制器时,试图在错误的文件中实现该功能。
我是如何解决的:
override func viewDidLoad() {
super.viewDidLoad()
self.setupLabelTap()
}
}
@objc func labelTapped(_ sender: UITapGestureRecognizer) {
if let url = URL(string: "http://maps.apple.com/?address=My+Address") {
UIApplication.shared.open(url)
}
print("The label is tapped!")
}
func setupLabelTap() {
let labelTap = UITapGestureRecognizer(target: self, action: #selector(self.labelTapped(_:)))
self.myView.labelAddress.isUserInteractionEnabled = true
self.myView.labelAddress.addGestureRecognizer(labelTap)
}
此外,你绝对不需要MapKIT来使用UITapRecognizer打开苹果地图。
我正在尝试实现一些功能:
我想要一个 UITextView,恰好是某个地方的地址,当用户点击这个 UITextView 时将用户引导到 Apple 地图,并在 UITextView 中显示地址的位置。关于我该怎么做的任何想法?
到目前为止我尝试过的事情:
import MapKit
@IBOutlet weak var firstAddressTapped: UITextView!
func viewDidLoad() {
let attributedString = NSMutableAttributedString(string: "The address of my place")
attributedString.addAttribute(.link, value: "http://maps.apple.com/?daddr=The+address+of+my+place", range: NSRange(location: 19, length: 55))
firstAddressTapped.attributedText = attributedString
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
UIApplication.shared.open(URL)
return false
}
谢谢!
你可以这样传递地址;
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
UIApplication.shared.open(URL)
// check if user touched the link.
if range == NSRange(location: 19, length: 55) {
UIApplication.sharedApplication().openURL(NSURL(string: "http://maps.apple.com/?address=The+address+of+my+place")!)
}
return false
}
Alexander Adelmaer 在 Medium 上的一个很棒的简单教程帮助我解决了我的问题!享受。
https://medium.com/app-makers/how-to-add-a-tap-gesture-to-uilabel-in-xcode-swift-7ada58f1664
我做错了什么:
当我应该将它添加到视图控制器时,试图在错误的文件中实现该功能。
我是如何解决的:
override func viewDidLoad() {
super.viewDidLoad()
self.setupLabelTap()
}
}
@objc func labelTapped(_ sender: UITapGestureRecognizer) {
if let url = URL(string: "http://maps.apple.com/?address=My+Address") {
UIApplication.shared.open(url)
}
print("The label is tapped!")
}
func setupLabelTap() {
let labelTap = UITapGestureRecognizer(target: self, action: #selector(self.labelTapped(_:)))
self.myView.labelAddress.isUserInteractionEnabled = true
self.myView.labelAddress.addGestureRecognizer(labelTap)
}
此外,你绝对不需要MapKIT来使用UITapRecognizer打开苹果地图。