2017-05-09 68 views
2

我得到了谷歌地圖標記的問題,我想把標記接觸,但我不知道如何處理它我嘗試了幾種方法,但它不工作,沒有任何反應,然後我觸摸地圖。 pressrecognizer似乎有些問題。斯威夫特3谷歌地圖添加標記觸摸

更新時間:

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

不要添加手勢識別器手動谷歌地圖,它管理的是互動本身並專門委託函數來處理常用手勢。

做一個長按一個GSMMapView確保您設置的委託

self.mapView.delegate = self 

,然後線了相應的委託功能

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 
    } 
} 

上面的代碼將在該位置添加標記你長期堅持,你也可以添加一個標題和片段,你可以看到。實際上將它添加到地圖的部分是marker.map = mapView

+0

我試圖添加這個,但是當我在地圖上的tapp沒有任何反應。 –

+0

你在做長時間的水龍頭嗎?我有一個示例項目在這裏與此代碼中,它適用於我 – Scriptable

+0

是的,我是,沒有發生,你能告訴我嗎? –