在我的应用程序中,我创建了一个名为 StoreInfo 的对象数组。 map 上的标记是使用 StoreInfo 对象的字段生成的。
稍后我想使用存储在 StoreInfo 字段中的附加数据生成一个信息窗口。
因此我需要在标记和信息对象之间创建一个字典。
storeMarkerMap = [[NSDictionary alloc] init];
for(StoreInfo *info in storesArray){
CLLocationCoordinate2D pos = CLLocationCoordinate2DMake(info.lat,info.lng);
GMSMarker *marker = [GMSMarker markerWithPosition:pos];
marker.title = info.name;
marker.snippet = info.type;
marker.icon =[self iconSelector: info];
marker.map = mapView_;
[storeMarkerMap setObject:info forKey:marker];
}
...
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker {
CustomInfoWindow *infowindow = [[[NSBundle mainBundle] loadNibNamed:@"InfoWindow" owner:self options:nil] objectAtIndex:0];
StoreInfo *info = [storeMarkerMap objectForKey:marker];
infowindow.title.text = info.name;
infowindow.description.text = info.address;
infowindow.phone.text=info.phone;
return infowindow;
}
这不起作用,因为标记对象不遵循以下要求:
the Keys must implement the hash and isEqual
最佳答案
最简单的 :将商店用作标记的 userData :)
==> 不需要弄乱额外的字典(恕我直言,也是最干净的)
所以:
for(StoreInfo *info in storesArray){
CLLocationCoordinate2D pos = CLLocationCoordinate2DMake(info.lat,info.lng);
GMSMarker *marker = [GMSMarker markerWithPosition:pos];
marker.userData = @{@"store":info};
...
}
...
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker {
StoreInfo *info = marker.userData[@"store"];
...
}
关于ios - Google Maps for ios,在 Objective C 中创建标记的 NSDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20404553/