Search code examples
iosswift4google-maps-markersgoogle-maps-sdk-ios

I'm having a trouble adding multiple markers in a mapView


I'm having a trouble adding multiple markers in a mapView but I always end up by showing one marker which is the last called marker I don't know why. the object of this is to fetch data that contains latitudes and longitudes and add those markers I'm trying to do it static but I'm enable to show multiple markers

I created a function that adds a new marker and I call it in the viewDidLoad

override func viewDidLoad() {
        super.viewDidLoad()
        
        
        AddMarker(title: "pala", snippet: "nanana", latitude: 35.741522, longitude: 9.805937)
        
        
        AddMarker(title: "pala", snippet: "nanana", latitude: 36.89939467218524, longitude: 10.187976658321267)
        
        
        
    }
    private func AddMarker(title:String , snippet:String  , latitude:Double , longitude:Double){
        var title = title
        var snippet = snippet
        var latitude = latitude
        var longitude = longitude
        
        let camera = GMSCameraPosition.camera(withLatitude: latitude, longitude: longitude, zoom: 6.0)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        view = mapView
        
        // Creates a marker in the center of the map.
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        marker.title = title
        marker.snippet = snippet
        marker.map = mapView
    }

Solution

  • Create a GMSMapView instance as an instance property, outside the addMarker method. And in addMarker method change it's camera position and add the markers.

    let mapView = GMSMapView()
    
    private func addMarker(title:String, snippet:String , latitude:Double , longitude:Double){
        let camera = GMSCameraPosition.camera(withLatitude: latitude, longitude: longitude, zoom: 6.0)
        self.mapView.animate(to: camera)
    
        // Creates a marker in the center of the map.
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        marker.title = title
        marker.snippet = snippet
        marker.map = mapView
    }
    

    When adding multiple markers one by one, don't animate the camera position to last marker position. To show all markers in the mapview you can use GMSCoordinateBounds

    let mapView = GMSMapView()
    var bounds = GMSCoordinateBounds()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addMarker(title: "pala", snippet: "nanana", latitude: 35.741522, longitude: 9.805937)
        addMarker(title: "pala", snippet: "nanana", latitude: 36.89939467218524, longitude: 10.187976658321267)
    }
    private func addMarker(title:String, snippet:String , latitude:Double , longitude:Double){
        // Creates a marker in the center of the map.
        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: 50)
        mapView.animate(with: update)
    }