Làm thế nào để tạo thêm Marker khi chạm vào màn hình - Google Maps SDK ios Swift

Mọi người cho em hỏi, em đang tìm hiểu về Google Maps API cho iOS App trên nền Swift. Mình đã search nhiều trên Google nhưng vẫn chưa tìm ra cách bắt sự kiện chạm tay vào màn hình để tạo một Marker mới trên Map. Xin mọi người giúp ạ! Cảm ơn

1 Like

Mình chưa thử nhưng research thì ra cách vậy, bạn thử làm theo xem sao :

1/ Lấy tọa độ khi touch vào màn hình Map :

 @IBAction func revealRegionDetailsWithLongPressOnMap(sender: UILongPressGestureRecognizer) {
    if sender.state != UIGestureRecognizerState.Began { return }
    let touchLocation = sender.locationInView(protectedMapView)
    let locationCoordinate = protectedMapView.convertPoint(touchLocation, toCoordinateFromView: protectedMapView)
    println("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")
} 

2/Vẽ Marker lên map với tọa độ có sẵn :

let  position = CLLocationCoordinate2DMake(locationCoordinate.latitude, locationCoordinate.longitude)
let marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = mapView
1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?