【问题标题】:Custom Annotation view is not displaying on the map自定义注释视图未显示在地图上
【发布时间】:2015-03-27 08:46:49
【问题描述】:

您好,我正在尝试在地图视图中创建自定义注释。在 mapview 中,当用户单击要显示一些文本和按钮的图钉时,我在地图上有多个图钉制造商,因此尝试创建自定义注释视图,但它不起作用。

我的代码

#import <MapKit/MapKit.h>
#import <GoogleMaps/GoogleMaps.h>
      @interface myviewcon : UIViewController<MKMapViewDelegate>
      @property (strong, nonatomic) IBOutlet MKMapView *mpview;

    @end

viewdidload 中我的 MKPointAnnotation 代码

for (NSDictionary *dic in [jsonArray1 valueForKey:@"houses"]) {


      MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
      NSString *name=[dic valueForKey:@"building_name"];

      CLLocationCoordinate2D placeCoord;

      placeCoord.latitude=[[dic objectForKey:@"lat"] doubleValue];
      placeCoord.longitude=[[dic objectForKey:@"lng"] doubleValue];



      [annotation setCoordinate:placeCoord];
      [annotation setTitle:name];
      [mpview addAnnotation:annotation];

      }

还有我的自定义注释视图代码

  - (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation{

    MKAnnotationView *anview = nil;

    UIButton *btnViewVenue = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    anview.rightCalloutAccessoryView=btnViewVenue;
    anview.enabled = YES;
    anview.canShowCallout = YES;
    anview.multipleTouchEnabled = NO;

    return anview;
  }

当用户单击地图上的 pin 制造商时,我使用上面的代码制作 customview 我已经用文本和按钮制作了视图,我已经厌倦了这样但它不起作用我检查了代表正在调用的断点但它不起作用,请告诉如何解决这个问题。

提前致谢。

【问题讨论】:

  • 唯一的问题似乎是在 viewForAnnotation 中,您没有创建视图。它被初始化为 nil,所以在它上面设置属性什么都不做。

标签: ios objective-c mkannotationview


【解决方案1】:

要自定义您的注释,请使用:

    -(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 

这里只添加你需要的子视图

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {

    UIView *test = [[UIView alloc]initWithFrame:CGRectMake(view.frame.origin.x - 12.5, view.frame.origin.y - 45, 50, 50)];
    test.backgroundColor = [UIColor redColor];
    [_mapView addSubview:test];
}

这里只是从 superView 中删除您的自定义 callOutView

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-14
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 2023-03-23
相关资源
最近更新 更多