gpt4 book ai didi

Swift:如何为每个特定标记设置功能?

转载 作者:行者123 更新时间:2023-11-30 12:48:07 24 4
gpt4 key购买 nike

在下面的代码中,我需要将每个特定标记设置为一个函数。如何将功能设置为:点击 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

标记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 导入谷歌地图 导入 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, didTapInfoWindowOfmarker: GMSMarker) 将在点击任何标记的信息窗口时调用。您想要获取一个仅在点击特定标记的信息窗口时才会调用的特定标记。希望我正确理解你的问题。

不幸的是,Google Maps API 中没有这样的委托(delegate)方法。

好吧,简单的解决方法就是使用 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 }
]

然后您可以像这样编写委托(delegate)方法:

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

关于Swift:如何为每个特定标记设置功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41359178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com