Alaéddine
Alaéddine

Reputation: 49

the func didTapAt coordinate is not showing anything

i'm trying to het a marker position once the user taps on the map via the didTapAt coordinate function but the event don't trigger i don't know why

i've tried different function like didtap on location and the same i got no errors but also no results

import UIKit
import GoogleMaps
import Alamofire
class MapController: UIViewController {
    var postArray = [AnyObject]()
    var posts:NSArray = []
    var bounds = GMSCoordinateBounds()
    @IBOutlet var mapView: GMSMapView!
    override func viewDidLoad() {
        super.viewDidLoad()    
    }                    
        func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D){
AddMarker(title: "test", snippet: "test" latitude: coordinate.latitude, longitude: coordinate.longitude)
        }                
    func mapView(_ mapView: GMSMapView, didTapPOIWithPlaceID placeID: String,
                 name: String, location: CLLocationCoordinate2D) {
        AddMarker(title: placeID, snippet: name, latitude: location.latitude, longitude: location.longitude)
        print("You tapped \(name): \(placeID), \(location.latitude)/\(location.longitude)")
    }                        
    private func AddMarker(title:String , snippet:String  , latitude:Double , longitude:Double){

        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        marker.title = title
        marker.snippet = snippet
        marker.map = mapView
        bounds = bounds.includingCoordinate(marker.position)
        let update = GMSCameraUpdate.fit(bounds, withPadding: 100)
        mapView.animate(with: update)
    }    
    }
    override func loadView() {
       GMSServices.provideAPIKey("MyApiKey")
        let camera = GMSCameraPosition.camera(withLatitude: 36, longitude: 10, zoom: 15)
         mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        view = mapView
       // AddMarker(title: "pala", snippet: "nanana", latitude: 30.89939467218524, longitude: 10.187976658344267)
    }
}

Upvotes: 0

Views: 632

Answers (1)

Oscar Apeland
Oscar Apeland

Reputation: 6662

The mapView needs a reference to your view controller to know where to send events. You need to assign mapView.delegate to self, and conform your ViewController to the appropriate delegate protocol, like such:

class MapController: UIViewController, GMSMapViewDelegate

Then, in loadView, add this line.

mapView.delegate = self

Upvotes: 1

Related Questions