0
私は本当に馬鹿なことをしているように感じ、答えは私の目の前にあります。私はマップ上でタップする機能を実装しようとしました。タップされたポイントにアドレスのタイトルが付いたマーカーが作成されます。しかし、地図をタップすると何も起こりません。エラーや何もありません。私もlongPress関数を使ってみましたが、それもうまくいきません。誰かが私のコードを見て、私が間違っていることを教えてください。ありがとうございました。Googleマップでマーカーを作成するsdk swift
私の輸入、locationManager、とのviewDidLoad
import UIKit
import GoogleMaps
class EventCreatorVC: UIViewController, GMSMapViewDelegate {
@IBOutlet weak var SearchMap: UISearchBar!
@IBOutlet weak var mapView: GMSMapView!
let marker = GMSMarker()
private lazy var locationManager: CLLocationManager = {
let locationManager = CLLocationManager()
locationManager.delegate = self
return locationManager
}()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
mapView.accessibilityElementsHidden = false
mapView.settings.myLocationButton = true
locationManager.requestWhenInUseAuthorization()
}
マイマップのconfigure:
private func configureMapView() {
mapView.delegate = self
// Center on California.
let camera = GMSCameraPosition.camera(withLatitude: 37.0, longitude: -120.0, zoom: 6.0)
mapView.camera = camera
mapView.settings.setAllGesturesEnabled(true)
mapView.settings.consumesGesturesInView = true
checkLocationAuthorizationStatus()
//addLocationMarkers()
}
私はそれを動作させるためにしようとするには持っている2つの機能は、最初は長押しと第二でありますちょうどタップです。
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
let location = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
marker.position = location
self.reverseGeocodeCoordinate(coordinate: coordinate, marker: marker)
marker.map = mapView
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
let location = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
marker.position = location
self.reverseGeocodeCoordinate(coordinate: coordinate, marker: marker)
marker.map = mapView
}
そして、私の逆ジオコーダ
func reverseGeocodeCoordinate(coordinate: CLLocationCoordinate2D, marker: GMSMarker) {
// 1
let geocoder = GMSGeocoder()
geocoder.reverseGeocodeCoordinate(coordinate) { response, error in
//Add this line
//Rest of response handling
}
// 2
geocoder.reverseGeocodeCoordinate(coordinate) { response, error in
if let address = response?.firstResult() {
// 3
let title = address.lines as [String]?
marker.title = title?.first
UIView.animate(withDuration: 0.25) {
self.view.layoutIfNeeded()
}
}
}
}
}
それはそれを解決しました!もう一度ありがとう! – Willstarr