Search code examples
objective-cgmsmapview

Obj-C - Google Maps SDK get data for selected marker?


I'm trying to get the data for a selected marker using the following code:

-(void)viewDidLoad {

 NSMutableDictionary *viewParams3 = [NSMutableDictionary new];
     [viewParams3 setValue:@"breweries" forKey:@"view_name"];
     [DIOSView viewGet:viewParams3 success:^(AFHTTPRequestOperation *operation, id responseObject) {
         
         self.breweryLocations = [responseObject mutableCopy];
      
      
         int index = 0;
         
         for (NSMutableDictionary *brewInfo in self.breweryLocations) {
         
         NSString *location = brewInfo[@"address"];
         NSString *userNames = brewInfo[@"node_title"];
         NSString *firstRemoved = [userNames stringByReplacingOccurrencesOfString:@"'" withString:@""];

          
         NSString *userBio = brewInfo[@"body"];
         

          
          CLGeocoder *geocoderFriend = [[CLGeocoder alloc] init];
          [geocoderFriend geocodeAddressString:location
            completionHandler:^(NSArray* placemarks, NSError* error){
            if (placemarks && placemarks.count > 0) {
           CLPlacemark *topResult = [placemarks objectAtIndex:0];
             MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
      
                
                
                CLLocationCoordinate2D position = placemark.coordinate;
                GMSMarker *marker = [GMSMarker markerWithPosition:position];
                marker.title = firstRemoved;
                marker.icon = [UIImage imageNamed:@"brewIconMax"];
                marker.map = self.mapView;
                marker.userData = self.breweryLocations;
               
                marker.map.selectedMarker.zIndex = index + 1;
                                                                               }
          
          }
                                                        ];
          
          }
      
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Failure: %@", [error localizedDescription]);
     }];


}

- (BOOL) mapView: (GMSMapView *)mapView didTapMarker:(GMSMarker *)marker
{

   self.venueCategory.text = marker.userData[mapView.selectedMarker.zIndex][@"type"];

}

That said, this line of code

self.venueCategory.text = marker.userData[mapView.selectedMarker.zIndex][@"type"];

returns the data from the first dictionary in my array no matter which marker I tap (zIndex is always returned as 0). I can't seem to find the correct code to obtain the selected marker's array data anywhere.

Any idea what that line should look like instead?


Solution

  • It's not clear to me whether "type" is an element of the brewery location dictionary, so I'm taking some guesses here.

    I'd suggest setting marker.userData to the brewInfo dictionary, and not the entire breweryLocations array.

    marker.userData = brewInfo;
    

    Then this to get the "type" from the brewInfo dictionary.

    self.venueCategory.text = marker.userData[@"type"];