如何在 mapkit 上放置位置图标 objective c
how to place location icon on mapkit objective c
我是 objective 的新手 c.I 创建了一个包含 mapView 的项目
在 ViewController.h 在 ny 项目中,
#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 图标。我怎样才能完成这个任务?以及如何将地图缩放到最大比例?
从这个 link 你可以下载我的示例项目:https://drive.google.com/file/d/0B5pNDpbvZ8SnZGZnU1ZfbjZMRWs/view?usp=sharing
我已经解决了你的问题,这是我的结果,使用这段代码,现在我正在使用一个自定义的 class,它实现了 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
希望对你有帮助,
看起来是这样
我是 objective 的新手 c.I 创建了一个包含 mapView 的项目
在 ViewController.h 在 ny 项目中,
#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 图标。我怎样才能完成这个任务?以及如何将地图缩放到最大比例? 从这个 link 你可以下载我的示例项目:https://drive.google.com/file/d/0B5pNDpbvZ8SnZGZnU1ZfbjZMRWs/view?usp=sharing
我已经解决了你的问题,这是我的结果,使用这段代码,现在我正在使用一个自定义的 class,它实现了 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
希望对你有帮助,
看起来是这样