gpt4 book ai didi

iOS 谷歌地图以编程方式选择注释 [Swift]

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:04:04 25 4
gpt4 key购买 nike

我有一种方法可以将标记 放置到我有自定义标注的 map 上。问题是用户需要单击此标记才能将其调出。是否可以从 Google Map 的 API 调用一个方法,该方法可以选择注释以编程方式调出标注?

我知道对于 Apple map 有 [mapView selectAnnotation:annotationName animated:YES]; 在 Objective C 和 self.mapView.selectAnnotation(selectedAnnotation, animated: true) 为 swift

我已通读 How to select a map pin programmatically但这似乎对 Google map 没有任何帮助。

仅供引用 - 这是一个 Swift 项目

最佳答案

需要使用GMSMapView selectedMarker 属性,同时需要将相机移动到新的selectedMarker位置

完整代码示例

import UIKit
import GoogleMaps

struct MarkerStruct {
let name: String
let lat: CLLocationDegrees
let long: CLLocationDegrees
}

class TapViewController: UIViewController, GMSMapViewDelegate {
let markers = [
MarkerStruct(name: "Food Hut 1", lat: 52.649030, long: 1.174155),
MarkerStruct(name: "Foot Hut 2", lat: 35.654154, long: 1.174185),
MarkerStruct(name: "Foot Hut 3", lat: 22.654154, long: 1.174185),
MarkerStruct(name: "Foot Hut 4", lat: 50.654154, long: 1.174185),
]

var mapView : GMSMapView? = nil

var mapMarkers : [GMSMarker] = []
var timer : Timer? = nil

override func viewDidLoad() {
super.viewDidLoad()

let camera = GMSCameraPosition.camera(withLatitude: 52.649030, longitude: 1.174155, zoom: 14)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

mapView?.delegate = self
self.view = mapView

for marker in markers {
let position = CLLocationCoordinate2D(latitude: marker.lat, longitude: marker.long)
let locationmarker = GMSMarker(position: position)
locationmarker.title = marker.name
locationmarker.map = mapView
mapMarkers.append(locationmarker)
}

timer = Timer.scheduledTimer(timeInterval: 15, target: self, selector: #selector(self.selectRandomMarker), userInfo: nil, repeats: true)

}

//Selecting random marker
func selectRandomMarker(){
let randomIndex = arc4random_uniform(UInt32(self.mapMarkers.count))
self.mapView?.selectedMarker = self.mapMarkers[Int(randomIndex)]
self.mapView?.animate(toLocation: (self.mapView?.selectedMarker!.position)!)
}

}

关于iOS 谷歌地图以编程方式选择注释 [Swift],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49156832/

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