我是目标c的新手。我创建了一个由mapView
组成的项目在ny项目的ViewController.h中,
#import <UIKit/UIKit.h>
#import "MapKit/MapKit.h"
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
在ViewController.m文件中,我有viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
mapView.showsUserLocation=YES;
// Do any additional setup after loading the view, typically from a nib.
}
我想在代码中给出多个位置的坐标,我想在地图上显示与这些坐标对应的loc.png图标。我该如何完成这项任务?以及如何将地图缩放到最大比例? 通过此链接,您可以下载我的示例项目:https://drive.google.com/file/d/0B5pNDpbvZ8SnZGZnU1ZfbjZMRWs/view?usp=sharing
答案 0 :(得分:0)
我已经处理了您的问题,这是我的结果,使用此代码,现在我使用实现MKAnnotation
协议的自定义类
已编辑
<强> Place.h 强>
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Place : NSObject<MKAnnotation>
@property (nonatomic) CLLocationCoordinate2D coordinate;
// Title and subtitle for use by selection UI.
@property (nonatomic, nullable) NSString *title;
@property (nonatomic, nullable) NSString *subtitle;
-(id)initWithCoordinates:(CLLocationCoordinate2D) coordinates andName:(NSString*)name;
@end
<强> Place.m 强>
#import "Place.h"
@implementation Place
@synthesize coordinate,title,subtitle;
-(id)initWithCoordinates:(CLLocationCoordinate2D) coordinates andName:(NSString*)name
{
self = [super init];
if(self)
{
[self setTitle:name];
[self setCoordinate:coordinates];
}
return self;
}
@end
修改后的代码
#import "ViewController.h"
#import "Place.h"
@interface ViewController () <MKMapViewDelegate>
@property NSMutableArray * arrayOfLocations;
@end
@implementation ViewController
@synthesize mapView;
- (void)viewDidLoad {
[super viewDidLoad];
mapView.showsUserLocation=YES;
self.arrayOfLocations = [NSMutableArray arrayWithObjects:[[Place alloc] initWithCoordinates:CLLocationCoordinate2DMake(40.416691, -3.700345) andName:@"MADRID"],
[[Place alloc] initWithCoordinates:CLLocationCoordinate2DMake(35.416691, -3.700345) andName:@"SOMEWARE IN THE MAP"],
[[Place alloc] initWithCoordinates:CLLocationCoordinate2DMake(35.416691, -40.700345) andName:@"SOMEWARE IN THE MAP1"],
[[Place alloc] initWithCoordinates:CLLocationCoordinate2DMake(20.416691, -50.700345) andName:@"SOMEWARE IN THE MAP2"], nil];
[self.mapView setDelegate:self];
[self.mapView addAnnotations:self.arrayOfLocations];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView * annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"testAnnotationView"];
if(annotationView == nil){
annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"testAnnotationView"];
annotationView.image = [UIImage imageNamed:@"loc.png"];
annotationView.canShowCallout = true;
}
return annotationView;
}
@end
希望这会对你有所帮助,
这就是它的外观