【发布时间】:2017-05-12 13:36:56
【问题描述】:
在以下代码中,我需要将每个特定标记设置为一个函数。如何将功能设置为:点击 MarkerInfoWindow 时,打开 URL? URL 需要针对每个标记都是特定的。
想法是当 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 导入谷歌地图 导入 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
【问题讨论】:
标签: swift function google-maps navigation google-maps-markers