我一直在寻找MKMapview上自定义标注的解决方案,我不想在左侧或右侧附件视图中添加图像和文本。我想我宁愿创建一个UIAlertView,它会显示所有选中映射引脚的信息。
我在地图引脚上收集了来自JSON数据的lat和long,并且JSON数据还包含其他信息,例如:电话号码,网站链接等。
如何将所有这些信息输入到按下地图引脚时调用的UIAlertView中?
以下是一些示例代码:
self.result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if(error == nil)
self.mapLocations = result;
NSLog(@"%@", [result allKeys]);
for (NSDictionary *location in self.mapLocations[@"merchants"]) {
for (NSDictionary *locations in location[@"branches"]) {
NSLog(@"%@",locations);
CLLocationCoordinate2D annotationCoordinate =
CLLocationCoordinate2DMake([locations[@"latitude"] doubleValue],
[locations[@"longitude"] doubleValue]);
Annotation *annotation = [[Annotation alloc] init];
annotation.coordinate = annotationCoordinate;
annotation.title = location[@"name"];
annotation.subtitle = locations[@"street"];
[self.mapView addAnnotation:annotation];
这是我的MKAnnotationView方法:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annoPin"];
MKAnnotationView *view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"annoView"];
if(!view) {
view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annoView"];
}
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:nil action:@selector(showDetails :) forControlEvents:UIControlEventTouchUpInside];
pinView.animatesDrop = YES;
view.rightCalloutAccessoryView = rightButton;
view.image = [UIImage imageNamed:@"check.png"];
view.enabled = YES;
view.canShowCallout = YES;
return view;
}
帮助!
答案 0 :(得分:0)
如果要在按下显示按钮后显示UIAlertView,您应该删除有关为按钮指定操作的行并实施– mapView:annotationView:calloutAccessoryControlTapped:
。只要您的类被设置为地图的委托,那么将调用该函数,您可以从那里创建UIAlertView。关于该函数的参数是annotationView
,您可以从中获取MKAnnotation
,并且所有细节都应附加到该函数。