-4
我有這個標記簡介小問題。我應該怎麼做才能讓用戶在我的基於谷歌地圖的應用上放置自己的標記?如何讓用戶放置標記在地圖上?
我有這個標記簡介小問題。我應該怎麼做才能讓用戶在我的基於谷歌地圖的應用上放置自己的標記?如何讓用戶放置標記在地圖上?
有多種方法可以做到這一點。其中之一是在用戶長按地圖時添加標記。爲了檢測長按,實現這個委託方法:
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
let marker = GMSMarker(position: coordinate)
// marker.isDraggable = true
// marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = mapView
// marker.icon = GMSMarker.markerImage(with: UIColor.blue)
}
被註釋掉的線是可選的,因爲它們只設置標記的自定義屬性。還有更多,你可以自定義。
另外,如果您還沒有這樣做的話,加GMSMapViewDelegate
到您的視圖控制器類的聲明:
class YourViewController: UIViewController, GMSMapViewDelegate {
,並分配self
到delegate
屬性:
let camera = GMSCameraPosition.camera(withLatitude: 0, longitude: 0, zoom: 3)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.isMyLocationEnabled = true
view = mapView
mapView.delegate = self // add this line!
另外,您可以在發生其他事件時添加標記。例如,您可以設置當用戶點擊一個UIBarBUttonItem
或UIButton
添加標記。全取決於你!但添加按鈕的過程基本上是這兩行:
let marker = GMSMarker(position: coordinate)
marker.map = mapView
// mapView is the map that you want to add the marker to. If you are doing this outside a delegate method, use self.view
您還可以考慮將標記添加到集合,以便稍後修改它們。
如果你覺得我的答案回答你的問題,請考慮一下對對號接受它! @情慾 – Sweeper