使用地图
iOS系统自带地图(主要是iPhone),为了使用地图,需要做如下步骤:
- (void)viewDidLoad
{
[super viewDidLoad];
MKMapView *mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[mapView setMapType: MKMapTypeHybrid];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setShowsUserLocation:YES];
[self.view addSubview:mapView];
}
GIS(Geographic Information System)介绍
我们需要了解几个方面的知识。
聚焦
下面使用一个小例子来看如何使用聚焦到某个地点的功能。我们在下面的步骤中都将采用拖拽方式生成界面的形式而不是手动编写界面生成代码。
#import "ViewController.h"
#import
// ShangHai
#define SH_LATITUDE 31.14
#define SH_LONGITUDE 121.29
// BeiJing
#define BJ_LATITUDE 39.55
#define BJ_LONGITUDE 116.24
// Span
#define SPAN_VALUE 0.20f
@interface ViewController ()
@end
@implementation ViewController
@synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
// region property: center, span
MKCoordinateRegion region;
CLLocationCoordinate2D center;
center.latitude = BJ_LATITUDE;
center.lOngitude= BJ_LONGITUDE;
MKCoordinateSpan span;
span.latitudeDelta = SPAN_VALUE;
span.lOngitudeDelta= SPAN_VALUE;
region.span = span;
region.center = center;
// assign region to map
[mapView setRegion:region animated:YES];
}
#import
#import
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@end
#import "ViewController.h"
#import
@interface ViewController ()
@end
@implementation ViewController
@synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
[mapView setDelegate:self];
[mapView setShowsUserLocation:YES];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
CLLocationCoordinate2D loc = [userLocation coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 500, 500);
[self.mapView setRegion:region animated:YES];
}
添加注解
我们经常需要将某个地点标注到地图上,我们需要增加Annotation功能,实际就是一个标注。
#import
#import
@interface MapAnnotation : NSObject
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subTitle;
- initWithPosition:(CLLocationCoordinate2D) coords;
@end
#import "MapAnnotation.h"
@implementation MapAnnotation
@synthesize coordinate;
@synthesize title;
@synthesize subTitle;
- initWithPosition:(CLLocationCoordinate2D) coords {
if (self = [super init]) {
self.coordinate = coords;
}
return self;
}
@end
// ShangHai
#define SH_LATITUDE 31.14
#define SH_LONGITUDE 121.29
// BeiJing
#define BJ_LATITUDE 39.55
#define BJ_LONGITUDE 116.24
// Span
#define SPAN_VALUE 0.20f
@interface ViewController ()
@end
@implementation ViewController
@synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
// region property: center, span
MKCoordinateRegion region;
CLLocationCoordinate2D center;
center.latitude = BJ_LATITUDE;
center.lOngitude= BJ_LONGITUDE;
MKCoordinateSpan span;
span.latitudeDelta = SPAN_VALUE;
span.lOngitudeDelta= SPAN_VALUE;
region.span = span;
region.center = center;
// assign region to map
[mapView setRegion:region animated:YES];
CLLocationCoordinate2D location;
location.latitude = SH_LATITUDE;
location.lOngitude= SH_LONGITUDE;
MapAnnotation *anno = [[MapAnnotation alloc] initWithPosition: location];
[anno setTitle: @"Shanghai"];
[anno setSubTitle: @"Shanghai center"];
[mapView addAnnotation: anno];
}
自定义注解
默认的注解是一个图钉一样的图标,点击后弹出文字。我们可以修改注解为我们期望的效果。
- (void)viewDidLoad
{
[super viewDidLoad];
[mapView setDelegate: self];
// region property: center, span
MKCoordinateRegion region;
CLLocationCoordinate2D center;
center.latitude = SZ_LATITUDE;
center.lOngitude= SZ_LONGITUDE;
MKCoordinateSpan span;
span.latitudeDelta = SPAN_VALUE;
span.lOngitudeDelta= SPAN_VALUE;
region.span = span;
region.center = center;
// assign region to map
[mapView setRegion:region animated:YES];
CLLocationCoordinate2D location;
location.latitude = SH_LATITUDE;
location.lOngitude= SH_LONGITUDE;
MapAnnotation *anno = [[MapAnnotation alloc] initWithPosition: location];
[anno setTitle: @"Shanghai"];
[anno setSubTitle: @"Shanghai center"];
[mapView addAnnotation: anno];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
// view
MKPinAnnotationView * view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
// pin color
[view setPinColor:MKPinAnnotationColorPurple];
// enabled animated
[view setEnabled: YES];
[view setAnimatesDrop: YES];
[view setCanShowCallout: YES];
// image button
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"customanno.png"]];
[view setLeftCalloutAccessoryView:imageView];
[view setRightCalloutAccessoryView: [UIButton buttonWithType: UIButtonTypeDetailDisclosure]];
return view;
}
增加AccessoryView点击效果
我们点击大头针会弹出自定义的AccessoryView,显示一个图文标题加上一个向右箭头,但是这些都是无法点击的,现在我们加上向右箭头点击效果。
我们添加一个代理方法:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
// view
MKPinAnnotationView * view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
// pin color
[view setPinColor:MKPinAnnotationColorPurple];
// enabled animated
[view setEnabled: YES];
[view setAnimatesDrop: YES];
[view setCanShowCallout: YES];
// image button
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"customanno.png"]];
[view setLeftCalloutAccessoryView:imageView];
[view setRightCalloutAccessoryView: [UIButton buttonWithType: UIButtonTypeDetailDisclosure]];
return view;
}
我们可能希望将多个地点注解添加到我们的地图中,那么我们需要以下几步:
// ShangHai
#define SH_LATITUDE 31.14
#define SH_LONGITUDE 121.29
// SuZhou
#define SZ_LATITUDE 31.19
#define SZ_LONGITUDE 120.37
// ChangZhou
#define CZ_LATITUDE 31.47
#define CZ_LONGITUDE 119.58
// KunShan
#define KS_LATITUDE 31.23
#define KS_LONGITUDE 120.57
// Span
#define SPAN_VALUE 2.0f
- (void)viewDidLoad
{
[super viewDidLoad];
[mapView setDelegate: self];
// region property: center, span
MKCoordinateRegion region;
CLLocationCoordinate2D center;
center.latitude = KS_LATITUDE;
center.lOngitude= KS_LONGITUDE;
MKCoordinateSpan span;
span.latitudeDelta = SPAN_VALUE;
span.lOngitudeDelta= SPAN_VALUE;
region.span = span;
region.center = center;
// assign region to map
[mapView setRegion:region animated:YES];
NSMutableArray *annotatiOns= [[NSMutableArray alloc] init];
MapAnnotation *anno;
CLLocationCoordinate2D location;
anno = [[MapAnnotation alloc] init];
location.latitude = SH_LATITUDE;
location.lOngitude= SH_LONGITUDE;
[anno setCoordinate: location];
[anno setTitle: @"上海"];
[annotations addObject:anno];
anno = [[MapAnnotation alloc] init];
location.latitude = SZ_LATITUDE;
location.lOngitude= SZ_LONGITUDE;
[anno setCoordinate: location];
[anno setTitle: @"苏州"];
[annotations addObject:anno];
anno = [[MapAnnotation alloc] init];
location.latitude = CZ_LATITUDE;
location.lOngitude= CZ_LONGITUDE;
[anno setCoordinate: location];
[anno setTitle: @"常州"];
[annotations addObject:anno];
anno = [[MapAnnotation alloc] init];
location.latitude = KS_LATITUDE;
location.lOngitude= KS_LONGITUDE;
[anno setCoordinate: location];
[anno setTitle: @"昆山"];
[annotations addObject:anno];
[mapView addAnnotations:annotations];
}
MKPinAnnotationView *view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];
if (view == nil) {
view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
}
添加多个不同的注解
我们期望对不同的注解添加不同的显示效果,则需要添加如下代码:
#import
#import
@interface MapAnnotation : NSObject
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subTitle;
@property (nonatomic, copy) NSString *province;
@property (nonatomic, copy) NSString *name;
- initWithPosition:(CLLocationCoordinate2D) coords;
@end
- (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
// Get a referene to the annotation to get its state value
MapAnnotation *myAnnotation = (MapAnnotation *)annotation;
NSString *myImage;
if ([myAnnotation.province isEqualToString:@"JiangSu"]) {
myImage = @"jiangsu.png";
} else if ([myAnnotation.province isEqualToString:@"ShangHai"]) {
myImage = @"shanghai.png";
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:myImage]];
self.leftCalloutAccessoryView = imageView;
if ([myAnnotation.name isEqualToString:@"ShangHai"]) {
self.image = [UIImage imageNamed:@"shanghai_anno.png"];
}
self.enabled = YES;
self.canShowCallout = YES;
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[mapView setDelegate: self];
// region property: center, span
MKCoordinateRegion region;
CLLocationCoordinate2D center;
center.latitude = KS_LATITUDE;
center.lOngitude= KS_LONGITUDE;
MKCoordinateSpan span;
span.latitudeDelta = SPAN_VALUE;
span.lOngitudeDelta= SPAN_VALUE;
region.span = span;
region.center = center;
// assign region to map
[mapView setRegion:region animated:YES];
NSMutableArray *annotatiOns= [[NSMutableArray alloc] init];
MapAnnotation *anno;
CLLocationCoordinate2D location;
anno = [[MapAnnotation alloc] init];
location.latitude = SH_LATITUDE;
location.lOngitude= SH_LONGITUDE;
[anno setCoordinate: location];
[anno setTitle: @"上海"];
[anno setName: @"ShangHai"];
[anno setProvince: @"ShangHai"];
[annotations addObject:anno];
anno = [[MapAnnotation alloc] init];
location.latitude = SZ_LATITUDE;
location.lOngitude= SZ_LONGITUDE;
[anno setCoordinate: location];
[anno setTitle: @"苏州"];
[anno setName: @"SuZhou"];
[anno setProvince: @"JiangSu"];
[annotations addObject:anno];
anno = [[MapAnnotation alloc] init];
location.latitude = CZ_LATITUDE;
location.lOngitude= CZ_LONGITUDE;
[anno setCoordinate: location];
[anno setTitle: @"常州"];
[anno setName: @"ChangZhou"];
[anno setProvince: @"JiangSu"];
[annotations addObject:anno];
anno = [[MapAnnotation alloc] init];
location.latitude = KS_LATITUDE;
location.lOngitude= KS_LONGITUDE;
[anno setCoordinate: location];
[anno setTitle: @"昆山"];
[anno setName: @"KunShan"];
[anno setProvince: @"JiangSu"];
[annotations addObject:anno];
[mapView addAnnotations:annotations];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
// view
MapAnnotationView *view = (MapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];
if (view == nil) {
view = [[MapAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
}
return view;
}