【问题标题】:Adding pins to MapView将图钉添加到 MapView
【发布时间】:2012-01-21 16:07:49
【问题描述】:

我想知道如何在 iPhone 应用程序中向 MapView 添加图钉。我想把别针别在名字里有“茶”这个词的地方,把每个别针放在每个包含这个词的地方是不切实际的,所以我想知道是否有办法做到这一点,这样当MapView 被加载,引脚被固定到这些地方。我认为这将通过 Google 的 Map API 完成,但我不确定我将如何准确地做到这一点 - 有没有人知道任何可以实现这一点的教程。

到目前为止,我有一个简单的视图,其中包含一个 MapView 以及一个相应的视图控制器。

提前致谢!

【问题讨论】:

    标签: iphone google-maps mkmapview android-mapview


    【解决方案1】:

    您必须将 MKAnnotation 的实例添加到您的 MKMapView。

    [mapView addAnnotation:annotation];
    

    annotation 是符合 MKAnnotation 协议的类的实例。在此处阅读相应的文档:

    http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html

    http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKAnnotation_Protocol/Reference/Reference.html#//apple_ref/occ/intf/MKAnnotation

    示例代码:

    @interface MyAnnotation: NSObject <MKAnnotation>
    {
        CLLocationCoordinate2D coordinate;
        NSString *title;
    }
    
    @end
    
    @implementation MyAnnotation
    
    @synthesize coordinate, title;
    
    - (id) init
    {
        if ((self = [super init]))
        {
            coordinate.latitude = 0.0;
            coordinate.longitude = 0.0;
            title = NSLocalizedString(@"Tea");
        }
        return self;
    }
    
    @end
    

    在您的视图控制器中:

    - (void) viewDidLoad
    {
        [super viewDidLoad];
        // custom initialiation; create map view
        [self addPin]; // or with parameters, called multiple times, to add several annotations
    }
    
    - (void) addPin
    {
        MyAnnotation *ann = [[MyAnnotation alloc] init];
        [mapView addAnnotation:ann];
        [ann release];
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      • 1970-01-01
      相关资源
      最近更新 更多