在地图中添加标记

时间:2014-06-23 16:00:24

标签: ios map mapkit marker

我是iOS新手,与Android逻辑有很大不同,我无法找到如何在地图中添加标记: - |

我已将MKMapView添加到xib

我已将此代码添加到我的.m(已修剪)

#import "AppDelegate.h"
#import <MapKit/MapKit.h>

@interface Dove ()
@property (weak,nonatomic) IBOutlet MKMapView *mappa;

@end
#define METERS_PER_MILE 1609.344
@implementation Dove
- (IBAction)vaiHome:(id)sender {
    Index *second = [[Index alloc] initWithNibName:@"Index" bundle:nil];
    [self presentViewController:second animated:YES completion:nil];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 45.40170;
    zoomLocation.longitude= 8.91552;
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, METERS_PER_MILE, METERS_PER_MILE);
    [_mappa setRegion:viewRegion animated:YES];
    [_mappa regionThatFits:viewRegion];
}

现在,如何在该位置显示引脚?它应该很简单,但我无法找到解决方案: - | 此外,地图仍然在美国,但大多数都是标记。

非常感谢。

1 个答案:

答案 0 :(得分:0)

首先,您必须设置注释标记(在viewdidload中调用它):

- (void)setupAnnotation
{
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 45.40170;
    zoomLocation.longitude= 8.91552;

    NARMapViewAnnotation* annot = [[NARMapViewAnnotation alloc] initWithCoord:zoomLocation];
    [_mapView addAnnotation:annot];
}

然后你必须设置MKMapViewDelegate方法:

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
    MKAnnotationView* annotView = [views objectAtIndex:0];
    id<MKAnnotation> mp = [annotView annotation];
    MKCoordinateRegion region = [mv regionThatFits:MKCoordinateRegionMakeWithDistance([mp coordinate], REGION_WINDOW, REGION_WINDOW)];
    [mv setRegion:region animated:YES];
}

NARMapViewAnnotation:

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

@interface NARMapViewAnnotation : NSObject <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

- (id)initWithCoord:(CLLocationCoordinate2D)coord;

@end

#import "NARMapViewAnnotation.h"

@implementation NARMapViewAnnotation

- (id)initWithCoord:(CLLocationCoordinate2D)coord
{
    if (self = [super init])
    {
        _coordinate = coord;
    }

    return self;
}
@end