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.
答案 0 :(得分:8)
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;
}
答案 1 :(得分:2)
Swift 解决方案:
您可以通过为标记的键设置值来为标记添加标识符或任何内容:
marker.setValue("20", forKey: "id")
您可以稍后使用:
获取marker.value(forKey: "id")
或
我的方法是为GMSMarker创建一个扩展,并使用userData属性使GMSMarker符合我的需要,对我来说,我想用Branch类的实例添加每个标记(带有标记的地图用于某些分支)公司)这就是我的方式:
extension GMSMarker {
var branch: Branch {
set(branch) {
self.userData = branch
}
get {
return self.userData as! Branch
}
}
}
所以当我设置标记属性时,我将它设置为:
marker.branch = someBranch
比标记更清晰,更可读。用户名= someBranch ??