Put multiple markers in Google maps. Swift

Sptibo Source

I've number of latitude and longitude in array. I want to display multiple markers in google map. I'm getting proble on that. Here's my code.

@IBOutlet weak var viewMap: UIView!
let locationManager = CLLocationManager()
var mapView = GMSMapView()
var cameraPosition = GMSCameraPosition()


fileprivate func loadData() {
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
    showCurrentLocationOnMap()
}

func showCurrentLocationOnMap(){
        mapView.settings.myLocationButton = true
        mapView.isMyLocationEnabled = true
        for data in nearByPlacesArray!{
            let camera = GMSCameraPosition.camera(withLatitude: (data.latitude)!, longitude: (data.longitude)!, zoom: 14.0)
            mapView = GMSMapView.map(withFrame: CGRect(x: 0, y: 0, width: self.viewMap.frame.size.width, height: self.viewMap.frame.size.height), camera: camera)
            let location = CLLocationCoordinate2D(latitude: data.latitude!, longitude: data.longitude!)
            print("location: \(location)")
            let marker = GMSMarker()
            marker.position = location
            marker.snippet = data.name!
            marker.map = mapView
        }
        self.viewMap.addSubview(mapView)   
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    self.showCurrentLocationOnMap()
    self.locationManager.stopUpdatingLocation()
}

I am unable to show multiple markers in mapview with this code. There is only one marker displayed in map related to last index of array. Please someone help me. Thanks

swiftgoogle-mapsmarker

Answers

answered 3 months ago Sid Mhatre #1

You Just need to addSubview mapView to view before creating markers.

var mapView: GMSMapView!

func showCurrentLocationOnMap(){
        mapView.settings.myLocationButton = true
        mapView.isMyLocationEnabled = true
            let camera = GMSCameraPosition.camera(withLatitude: 11.11, longitude: 12.12, zoom: 14.0) //Set default lat and long
            mapView = GMSMapView.map(withFrame: CGRect(x: 0, y: 0, width: self.viewMap.frame.size.width, height: self.viewMap.frame.size.height), camera: camera)
        self.viewMap.addSubview(mapView)   


        for data in nearByPlacesArray!{
            let location = CLLocationCoordinate2D(latitude: data.latitude!, longitude: data.longitude!)
            print("location: \(location)")
            let marker = GMSMarker()
            marker.position = location
            marker.snippet = data.name!
            marker.map = mapView
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    self.showCurrentLocationOnMap()
    self.locationManager.stopUpdatingLocation()
}

answered 2 weeks ago João Marcos Santos Teixeira #2

this is the code for 1 one marker, right?:

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView

This code above show multiple markers:

for marker in 1...5 {

        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: -33.96, longitude: 151.20)
        marker.title = "Jow Loove"
        marker.snippet = "California"
        marker.map = mapView

        print ("Your GMSMarker")
        print (marker)

    }

Print a simple marker after loop to see work!

But you be implement lat, long, title and snipped from some data / variable.

Yes, i lost a biggest time looking for one way to implement dynamic var...

var marker1 = 
var marker2 = 
var marker3 = 
....

If you now how make it with swift, please, implement the post.

:-)

comments powered by Disqus