【问题标题】:The simplest way to add a pin on a map in iOS在 iOS 地图上添加图钉的最简单方法
【发布时间】:2011-10-15 05:03:06
【问题描述】:

假设我有下面的代码在视图上添加地图,在位置上添加 pin最简单方法是什么。有人可以建议将代码 sn-p 添加到我的代码中吗?

谢谢

- (void)viewDidLoad
{
    MKMapView *map = [[MKMapView alloc] initWithFrame:CGRectMake(2, 2, 300, 300)];
    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };

    region.center.latitude = 22.278355;
    region.center.longitude = 114.181713;

    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;  

    [map setRegion:region animated:YES];
    [map regionThatFits:region];

    [self.view addSubview:map];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

更新:

xxx.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>


@interface ShopDetail : UIViewController <MKMapViewDelegate> {

}

@end

xxx.m

- (void)viewDidLoad
{
    MKMapView *map = [[MKMapView alloc] initWithFrame:CGRectMake(2, 2, 300, 300)];
    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };

    region.center.latitude = 22.278355; //40.105085;
    region.center.longitude = 114.181713; //-83.005237;

    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;  

    [map setRegion:region animated:YES];
    [map regionThatFits:region];

    map.delegate = self;

    [self.view addSubview:map];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView *pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];

    NSLog(@"called viewForAnnotation");

    pav.pinColor = MKPinAnnotationColorGreen;
    return pav;
}

*NSLog(@"调用viewForAnnotation");尚未执行。

【问题讨论】:

  • 更新似乎对我不起作用,即使我只是复制了您的代码。您确定代理正在触发注解吗?
  • 你可以在这里找到方法:stackoverflow.com/questions/5182082/…Cheers!

标签: objective-c


【解决方案1】:
  1. 设置地图的委托。
  2. 在地图上为您希望大头针出现的位置添加注释。
  3. 当地图为您在步骤 2 中添加的注释调用它的委托 -mapView:viewForAnnotation: 方法时,返回一个 MKPinAnnotationView 实例。

【讨论】:

  • 请检查我的问题的更新,我的图钉显示在地图上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-03
相关资源
最近更新 更多