Sptibo
Sptibo

Reputation: 283

Put multiple markers in Google maps. Swift

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

Upvotes: 4

Views: 15516

Answers (4)

anand
anand

Reputation: 31

//you can fill nearByPlacesArray

nearByPlacesArray = ["lat:":"23.898988",Long:"72.898988"]

// you can add multiple lat and long in  nearByPlacesArray this array.

for data in nearByPlacesArray{
        
        let lat = Double(data.Latitude)
        let long = Double(data.Longitude)
        
        let location = CLLocationCoordinate2D(latitude:lat!, longitude:long!)
        print("location1: \(location)")
        let marker = GMSMarker()
        marker.position = location
        //  marker.snippet = data.name
        marker.map = mapView
        marker.icon = GMSMarker.markerImage(with:.black)
        marker.icon = UIImage(named: "Location_Icon")
        
        
        
        
    }

Upvotes: 0

Sid Mhatre
Sid Mhatre

Reputation: 3417

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

Upvotes: 3

Enamul Haque
Enamul Haque

Reputation: 5063

in swift 5, You can use

for x in mylocationsArr {
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(x.lat, x.lon)
    marker.title = x.description
    marker.map = self.googleMap        
}

Upvotes: 0

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.

:-)

Upvotes: 3

Related Questions