在下面的代码中,我需要将每个特定标记设置为一个函数。如何将功能设置为:当点击MarkerInfoWindow时,打开URL? URL需要特定于每个标记。
想法是为标记1点击MarkerInfoWindow,然后它应显示此标记1的导航。
问题是我如何重新编写此代码以使每个标记具体的功能
标记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
Marker 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 导入GooglePlaces
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
答案 0 :(得分:0)
mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker)
。您希望获得一个仅在轻触特定标记的信息窗口时才会调用的特定链接。希望我能正确理解你的问题。
遗憾的是,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