在以下代码中,我需要将每个特定标记设置为一个函数。如何将函数设置为:当MarkerinFowdow被点击时,然后打开URL?URL需要针对每个标记。
IDEA 是在MarkerinFowindow攻击Marker1时,它应该显示此Marker1的导航。
问题是我如何重写此代码以使每个标记特定功能
特定于功能标记1:
let marker1 = GMSMarker()
marker1.position = CLLocationCoordinate2D(latitude: 55.6726299, longitude: 12.5662175)
marker1.title = "Københavns Hovedbanegård"
marker1.snippet = "Press for navigation"
marker1.tracksViewChanges = true
marker1.opacity = 0.9
marker1.icon = UIImage(named: "BCmarker")
marker1.appearAnimation = kGMSMarkerAnimationNone
marker1.map = self.googleMapsView
标记2:
let marker2 = GMSMarker()
marker2.position = CLLocationCoordinate2D(latitude: 55.68, longitude: 12.55)
marker2.title = "Test Marker2"
marker2.snippet = "Press for navigation"
marker2.tracksViewChanges = true
marker2.opacity = 0.9
marker2.icon = UIImage(named: "BCmarker")
marker2.appearAnimation = kGMSMarkerAnimationNone
marker2.map = self.googleMapsView
应特别分配每个标记的功能:
func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {
let testURL = URL(string: "comgooglemaps-x-callback://")!
if UIApplication.shared.canOpenURL(testURL) {
let directionsRequest = "comgooglemaps-x-callback://" +
"?daddr=55.6726299,12.5662175&directionsmode=walking&zoom=17"
let directionsURL = URL(string: directionsRequest)!
UIApplication.shared.openURL(directionsURL)
} else {
UIApplication.shared.openURL(NSURL(string: "http://maps.apple.com/?address=1600,PennsylvaniaAve.,20500")! as URL)
}
谢谢!
在@Sweeper的答案后,这就是代码的样子: 导入Uikit 导入Googlemaps 导入Google
class ViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate, GMSAutocompleteViewControllerDelegate {
// OUTLETS!
@IBOutlet weak var googleMapsView: GMSMapView!
// VARIABLES!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// GET LOCATION WHILE USING APP
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.startMonitoringSignificantLocationChanges()
initGoogleMaps()
}
// START GOOGLE MAPS!
func initGoogleMaps() {
let zoomCamera = GMSCameraUpdate.zoomIn()
googleMapsView.animate(with: zoomCamera)
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
let camera = GMSCameraPosition.camera(withLatitude: 55.6760968, longitude: 12.568337100000008, zoom: 12.5)
_ = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.googleMapsView.camera = camera
// CREATE FIND LOCATION BUTTON??
self.googleMapsView.delegate = self
self.googleMapsView.isMyLocationEnabled = true
self.googleMapsView.settings.myLocationButton = true
// MARKERS
let marker1 = GMSMarker()
marker1.position = CLLocationCoordinate2D(latitude: 55.6726299, longitude: 12.5662175)
marker1.title = "Københavns Hovedbanegård"
marker1.snippet = "Tryk for at få navigation"
marker1.tracksViewChanges = true
marker1.opacity = 0.9
marker1.icon = UIImage(named: "BCmarker")
marker1.appearAnimation = kGMSMarkerAnimationNone
marker1.map = self.googleMapsView
let marker2 = GMSMarker()
marker2.position = CLLocationCoordinate2D(latitude: 55.68, longitude: 12.55)
marker2.title = "Test Marker2"
marker2.snippet = "Tryk for at få navigation"
marker2.tracksViewChanges = true
marker2.opacity = 0.9
marker2.icon = UIImage(named: "BCmarker")
marker2.appearAnimation = kGMSMarkerAnimationNone
marker2.map = self.googleMapsView
let markerFunctions: [GMSMarker: (() -> Void)] = [
marker1: { print("Test1") },
marker2: { print("Test2") }
]
}
// ...something else about Google Places
mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker)
将被调用。您想获得只有在攻击a 特定标记的信息窗口时才会被调用的特定特定。希望我正确理解您的问题。
不幸的是,Google Maps API中没有这样的委托方法。
好吧,简单的解决方法将是使用IF语句检查是marker1
还是marker2
:
func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {
if marker == marker1 { // remember to declare marker 1 and 2 at class level!
// do stuff
} else if marker == marker2 {
// do other stuff
}
}
另外,您可以将每个标记的内容放在字典中:
let markerFuntions: [GMSMarker: (() -> Void)] = [
marker1: { // do stuff }
marker2: { // do other stuff }
]
您只需写这样的委托方法:
func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {
markerFunctions[marker]?()
}
编辑:我试图解决您的代码问题,这是我得到的:
class ViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate, GMSAutocompleteViewControllerDelegate {
// OUTLETS!
@IBOutlet weak var googleMapsView: GMSMapView!
// VARIABLES!
var locationManager = CLLocationManager()
var marker1: GMSMarker!
var marker2: GMSMarker!
var markerFunctions: [GMSMarker: (() -> Void)]!
override func viewDidLoad() {
super.viewDidLoad()
// GET LOCATION WHILE USING APP
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.startMonitoringSignificantLocationChanges()
initGoogleMaps()
}
// START GOOGLE MAPS!
func initGoogleMaps() {
let zoomCamera = GMSCameraUpdate.zoomIn()
googleMapsView.animate(with: zoomCamera)
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
let camera = GMSCameraPosition.camera(withLatitude: 55.6760968, longitude: 12.568337100000008, zoom: 12.5)
_ = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.googleMapsView.camera = camera
// CREATE FIND LOCATION BUTTON??
self.googleMapsView.delegate = self
self.googleMapsView.isMyLocationEnabled = true
self.googleMapsView.settings.myLocationButton = true
// MARKERS
marker1 = GMSMarker()
marker1.position = CLLocationCoordinate2D(latitude: 55.6726299, longitude: 12.5662175)
marker1.title = "Københavns Hovedbanegård"
marker1.snippet = "Tryk for at få navigation"
marker1.tracksViewChanges = true
marker1.opacity = 0.9
marker1.icon = UIImage(named: "BCmarker")
marker1.appearAnimation = kGMSMarkerAnimationNone
marker1.map = self.googleMapsView
marker2 = GMSMarker()
marker2.position = CLLocationCoordinate2D(latitude: 55.68, longitude: 12.55)
marker2.title = "Test Marker2"
marker2.snippet = "Tryk for at få navigation"
marker2.tracksViewChanges = true
marker2.opacity = 0.9
marker2.icon = UIImage(named: "BCmarker")
marker2.appearAnimation = kGMSMarkerAnimationNone
marker2.map = self.googleMapsView
markerFunctions = [
marker1: { print("Test1") },
marker2: { print("Test2") }
]
}
func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {
markerFunctions[marker]?()
}
// ...something else about Google Places