Stefan van de Laarschot
Stefan van de Laarschot

Reputation: 2163

Multiple Markers Google Map iOS SDK

I'm struggling to get multiple markers in my Google Map on iOS 8.0. My current code is:

ResourceGroep *rsg = [ResourceGroep alloc]init;
GMSCameraPosition *camera = nil;

for (int i = 0; i < [rsg.chosenResourceArray count]; i++)
{
    camera = [GMSCameraPosition cameraWithLatitude:[loc.Latitude doubleValue]
                               longitude:[loc.Long doubleValue]
                               zoom:15];

    mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
    mapView_.myLocationEnabled = NO;

    CLLocationCoordinate2D position = { [rsg.Latitude doubleValue], [rsg.Long    doubleValue] };
    GMSMarker *marker = [GMSMarker markerWithPosition:position];
      marker.title = [NSString stringWithFormat:@"Marker %i", i];
      marker.appearAnimation = YES;
      marker.flat = YES;
      marker.snippet = @"";
      marker.map = mapView_;
}

[self.view addSubview:mapView_];

I have iterated through my array, but I only see 1 marker while my array count is like 2 or 3 depends on what user selected. What am I missing?

Upvotes: 1

Views: 9487

Answers (4)

Dnyaneshwar Shinde
Dnyaneshwar Shinde

Reputation: 187

func showmarkeronmap(){
    DispatchQueue.main.async {
        let first = 0
        let last = self.vehicallistArr.count
        let interval = 1
        let sequence = stride(from: first, to: last, by: interval)
        for element in sequence {
            let cateAryrray = self.vehicallistArr[element]
            let mylatitude = Double(cateAryrray.carLatitude!)
            let mylongitude = Double(cateAryrray.carLongitute!)
            let camera = GMSCameraPosition.camera(withLatitude: mylatitude!, longitude: mylongitude!, zoom: 10.0)
             self.googleMapView.camera = camera
            showMarker(position: camera.target, markerTitle: cateAryrray.dailypriceStr!, markerSnippet: cateAryrray.titleStr!)
     }
        let update = GMSCameraUpdate.fit(self.bounds, withPadding: 30.0)
        self.googleMapView.animate(with: update)
}


func showMarker(position: CLLocationCoordinate2D, markerTitle : String , markerSnippet : String){
        let marker = GMSMarker()
        marker.position = position
        marker.title = markerTitle
        marker.snippet = markerSnippet
        marker.map = self.googleMapView
        self.googleMapView.selectedMarker = marker
     }
}

Upvotes: 1

Patel Jigar
Patel Jigar

Reputation: 2151

i got the best way hope it will be usefull to u too.

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:_sourceloc.latitude  longitude:_sourceloc.longitude zoom:6];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];


GMSMarker *marker = [GMSMarker markerWithPosition:_sourceloc];
marker.title=@"Source";
marker.snippet =_sourceAdd;
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = mapView;


GMSMarker *marker2 = [GMSMarker markerWithPosition:_destinationloc];
marker2.title=@"Destination";
marker2.snippet =_destinationAdd;
marker2.appearAnimation = kGMSMarkerAnimationPop;
marker2.map = mapView;

self.view = mapView;

Upvotes: 0

anjani kp
anjani kp

Reputation: 63

Putting these lines outside the loop should help.

camera = [GMSCameraPosition cameraWithLatitude:[loc.Latitude doubleValue]
                            longitude:[loc.Long doubleValue]
                                 zoom:15];

mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
mapView_.myLocationEnabled = NO;

You are initializing the map everytime you're adding a marker. Initialization will clear all the markers added so far.

Upvotes: 0

hoya21
hoya21

Reputation: 893

Check this:

   ResourceGroep *rsg = [ResourceGroep alloc]init;
   GMSCameraPosition *camera = nil;

   camera = [GMSCameraPosition cameraWithLatitude:[loc.Latitude doubleValue]
                            longitude:[loc.Long doubleValue]
                                 zoom:15];

   mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
   mapView_.myLocationEnabled = NO;

   for(int i = 0; i < [rsg.chosenResourceArray count]; i++)
   {




      CLLocationCoordinate2D position = { [rsg.Latitude doubleValue], [rsg.Long    doubleValue] };
      GMSMarker *marker = [GMSMarker markerWithPosition:position];
      marker.title = [NSString stringWithFormat:@"Marker %i", i];
      marker.appearAnimation = YES;
      marker.flat = YES;
      marker.snippet = @"";
      marker.map = mapView_;
  }

 [self.view addSubview:mapView_];

I have just changed position of some of your code.

Upvotes: 5

Related Questions