3

我想创建一个显示确切位置的地图视图(坐标:纬度 45.733333° 和经度 9.133333°),添加带有标题和副标题的简单注释。ma 必须是静态的,不需要有当前用户位置或其他任何东西。

我将地图查看器添加到选项卡式应用程序的简单视图控制器中。我有我的 MapViewController 文件,我将它们连接(我猜)到我的 ViewController,但我似乎无法找到一个教程来做我需要的事情,其他人非常复杂,我认为他们做的比我实际做的要多得多需要,而且我是初学者,所以我遇到了一些麻烦。

谢谢!

4

2 回答 2

3

尝试这个:

    CLLocationCoordinate2D coord;
    coord.latitude = 45.733333;
    coord.longitude = 9.133333;

    MKAnnotation *ann = [[MKAnnotation alloc] initWithLocation:coord];
    [ann setTitle:@"Title"];
    [ann setSubtitle:@"Subtitle"];
    [map addAnnotation:ann];

不要忘记将变量更改为变量的名称。

编辑

解决方案:

创建一个类 AddressAnnotation 并粘贴此代码:

地址注解.h

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

@interface AddressAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D _coordinate;
    NSString *_title;
}

@property (nonatomic, retain) NSString *title;

-(id)initWithCoordinate:(CLLocationCoordinate2D)c;

@end

地址注释.m

#import "AddressAnnotation.h"

@implementation AddressAnnotation

- (NSString *)subtitle{
    return nil;
}

- (NSString *)title{
    return _title;
}

- (CLLocationCoordinate2D)coordinate{
    return _coordinate;
}

-(id)initWithCoordinate:(CLLocationCoordinate2D)c{
    _coordinate = c;
    return self;
}

@end

现在,这是一个处理地图并创建注释的类:

MapViewController.h

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

@interface MapViewController : UIViewController

@property (nonatomic, retain) IBOutlet MKMapView *mapView;

- (IBAction)changeMapType:(UISegmentedControl *)control;

@end

MapViewController.m

#import "MapViewController.h"
#import "AddressAnnotation.h"

@interface MapViewController ()

@end

@implementation MapViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    CLLocationCoordinate2D  location;
    location.latitude = -15.830731; // change to your coordinate latitude
    location.longitude = -47.8753; // change to your coordinate longitude

    AddressAnnotation *addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
    addAnnotation.title = [NSString stringWithFormat:@"Title for you annotation"];

    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.002;     // 0.0 is min value u van provide for zooming
    span.longitudeDelta= 0.002;

    region.span=span;
    region.center =location;     // to locate to the center
    [_mapView addAnnotation:addAnnotation];
    [_mapView setRegion:region animated:TRUE];
    [_mapView regionThatFits:region];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
于 2013-03-13T21:12:36.987 回答
3

2020年解决方案...

“我想创建一个显示确切位置的地图视图(坐标:纬度 45.733333° 和经度 9.133333°)添加一个简单的注释......

所以 ...

@IBOutlet var map: MKMapView!

func demoMap() {
    var c = CLLocationCoordinate2D()
    c.latitude = 45.733333
    c.longitude = 9.133333
    
    let a = MKPointAnnotation()
    a.coordinate = c
    a.title = "Steve"
    map.addAnnotation(a)
}

只需在情节提要中添加一个 MKMapView。像任何其他视图一样使用约束来定位/调整它。将 IBOutlet 链接拖到“地图”。

于 2020-07-02T17:15:21.413 回答