5

I am working on google map iOS sdk. Here I create multiple markers in separate location co-ordinates.

Now I need to add identifier such like TAG for all markers to perform action for particular marker.

If TAG or some other identifier option is not available in google map iOS sdk, please suggest me how to archive it.

Thanks in Advance.

2 Answers 2

9

What I do is that I simply inherit the GMSMarker and add whatever data I need to it like this, I guess this is the best and easiest option you have.

@interface ATGoogleMapsSelectiveMarker : GMSMarker

@property (nonatomic) int markerID;
@property (nonatomic) int order;
@property (strong, nonatomic) NSObject* referenceObject;
@property (nonatomic) BOOL selected;

@end

EDIT:

I thought it is clear but I'll continue on how to get the data... When you create your markers and add them to the map, you create ATGoogleMapsSelectiveMarker and add it to the map after you fill it with everything you need, then you register any class you want as a delegate which implements GMSMapViewDelegate and you implement this method

- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
    // Here you are sure that your marker object is of type ATGoogleMapsSelectiveMarker but it won't harm to double check
   if ([marker isKindOfClass:[ATGoogleMapsSelectiveMarker class]]) {
       ATGoogleMapsSelectiveMarker* parsedMarker = (ATGoogleMapsSelectiveMarker*)marker;
       NSLog(@"%d", parsedMarker.markerId);
   }

    return YES;
}
3
  • Using custom GMSMarker class, I create marker with markerID. But how can I get particular marker using the markerID? Ex: GMSMarker *marker = (GMSMarker *)[self.mapView viewWithMarkerID:100];
    – Balaji G
    Jun 16, 2015 at 14:41
  • I thought it was clear on how to do it, I'll update my answer
    – user1453351
    Jun 17, 2015 at 13:33
  • When I subclass from GMSMarker then when assign mapView to map of marker, app will crash. because can not get Bounds of subclass. Dec 14, 2016 at 8:45
4

Swift solution :

You can add identifier or any thing to a marker by setting a value for a key for the marker:

marker.setValue("20", forKey: "id")

and you can get it later using :

marker.value(forKey: "id")

Or

My way to do it is by create an extension for the GMSMarker and using the userData property to make the GMSMarker fits my need, for me I want to add each marker with instance of Branch class (map with markers for branches of some company) and this is the way that I did:

extension GMSMarker {
    var branch: Branch {
        set(branch) {
            self.userData = branch
        }

        get {
           return self.userData as! Branch
       }
   }
}

so when I set the marker property I set it like this:

marker.branch = someBranch

Isn't that clearer and more readable than marker.userData = someBranch ??

1
  • First solution is not correct, at leaset xcode complaining and throwing NSUknownKeyException. Although, the second solution is working swiftly :) Nov 10 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.