2017-05-09 6 views
2

私はGoogleマップマーカーに問題がありますが、私はマーカーをタッチしたいと思いますが、それを処理する方法はわかりませんいくつかの方法を試しましたが、地図上でタッチします。 pressrecognizerで何か問題があるようです。スウィフト3 Googleマップはタッチでマーカーを追加します

更新日:

class MainMapController: UIViewController, CLLocationManagerDelegate { 

@IBOutlet weak var viewMap: GMSMapView! 
var makers: [GMSMarker] = [] 

var locationManager = CLLocationManager() 

override func viewDidLoad() { 
    super.viewDidLoad() 


    initializeTheLocationManager() 
    self.viewMap.isMyLocationEnabled = true 
    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress)) 
    self.viewMap.addGestureRecognizer(longPressRecognizer) 


} 

    func handleLongPress(recognizer: UILongPressGestureRecognizer) 
    { 
if (recognizer.state == UIGestureRecognizerState.began) 
{ 
    let longPressPoint = recognizer.location(in: self.viewMap); 
    let coordinate = viewMap.projection.coordinate(for: longPressPoint) 
    let marker = GMSMarker(position: coordinate) 
    marker.opacity = 0.6 
    marker.title = "Current Location" 
    marker.snippet = "" 
    marker.map = viewMap 
    makers.append(marker) 
    } 
    } 


func initializeTheLocationManager() 
{ 
    locationManager.delegate = self 
    locationManager.requestWhenInUseAuthorization() 
    locationManager.startUpdatingLocation() 
} 


func locationManager(_ manager: CLLocationManager,  didUpdateLocations locations: [CLLocation]) { 

    var location = locationManager.location?.coordinate 

    cameraMoveToLocation(toLocation: location) 
    locationManager.stopUpdatingLocation()  
}  
func cameraMoveToLocation(toLocation: CLLocationCoordinate2D?) { 
    if toLocation != nil { 
     viewMap.camera = GMSCameraPosition.camera(withTarget: toLocation!, zoom: 15)   
    } 
    } 
+0

です。viewMapはGMSMapViewですか? –

+0

はいいいえ –

+0

私は試しましたが、地図上でタップしても何も起こりません –

答えて

5

あなたはそれが相互作用自体と共通のジェスチャーを処理するためのデリゲート関数を捧げているのです管理し、Googleマップのために手動でジェスチャーrecognisersを追加しないでください。

extension ViewController: GMSMapViewDelegate { 
    func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) { 
     // Custom logic here 
     let marker = GMSMarker() 
     marker.position = coordinate 
     marker.title = "I added this with a long tap" 
     marker.snippet = "" 
     marker.map = mapView 
    } 
} 

を使用すると、デリゲートに

self.mapView.delegate = self 

を設定していることを確認し、適切なデリゲート機能をアップ配線上記のコードは場所にマーカーを追加しますGSMMapView上で長押しを行うには

また、あなたが見ているようにタイトルとスニペットを追加することもできます。実際に地図に追加する部分はmarker.map = mapView

+0

私はこれを追加しようとしましたが、私が地図上にタップすると何も起こりません。 –

+0

長いタップをしていますか?私はここにこのコードを持つサンプルプロジェクトを持っており、それは私のためにうまく動作します – Scriptable

+0

はい私は、何も起こらない、あなたは私にそれを表示することができますか? –

関連する問題