地图与标记

显示地图

在地图的头文件中#import <MapKit/MapKit.h>

在地图的头文件中还要引入协议<MKMapViewDelegate>

代码如下:

1
2
3
4
5
6
7
8
9
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "MapLocation.h"
@interface AnnotationViewController : UIViewController <MKMapViewDelegate>
@property (nonatomic) CLLocationDegrees longitude; // 经度
@property (nonatomic) CLLocationDegrees latitude; // 纬度
@property (strong, nonatomic) NSString *institutionName; // 名称
@property (strong, nonatomic) NSString *locationName; // 地址名称
@end

在地图的controller中声明MKMapView

代码如下:

1
2
3
4
5
6
@interface AnnotationViewController ()
@property (strong, nonatomic) MKMapView *mapView; // 地图view
@property (nonatomic) CLLocationDistance latitudinalMeters; // 纬度放大倍数
@property (nonatomic) CLLocationDistance longitudinalMeters; // 经度放大倍数
@property (nonatomic, strong) CLGeocoder *geocoder; // 反编码
@end

初始化地图view和协议

代码如下:

1
2
3
4
5
6
7
- (void)initMap
{
_mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 64, DF_WIDTH, DF_HEIGHT-64)];
[self.view addSubview:_mapView];
_mapView.mapType = MKMapTypeStandard;
_mapView.delegate = self;
}

自定义地图地址模型

location模型头文件代码如下,感觉其实除了遵守<MKAnnotation>协议以外其他的暂时没什么用,只用到了<MKAnnotation>里面的titlesubtitle

1
2
3
4
5
6
7
8
9
10
11
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MapLocation : NSObject<MKAnnotation>
@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate; //
@property (nonatomic, copy) NSString *streetAddress; // 街道信息属性
@property (nonatomic, copy) NSString *city; // 城市信息属性
@property (nonatomic, copy) NSString *state; // 邮编
@property (nonatomic, copy) NSString *zip; // 地理坐标
@property (nonatomic, copy) NSString *title; // 标题
@property (nonatomic, copy) NSString *subtitle; // 副标题
@end

location的m文件代码如下:用了懒加载。因为在’mapViewController’中会赋值给这两个property,所以感觉也并没有什么用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@implementation MapLocation
- (NSString *)title {
if(!_title) _title = @"XXX";
return _title;
}
- (NSString *)subtitle {
if(!_subtitle){
NSMutableString *ret = [NSMutableString new];
if(_state)
[ret appendString:_state];
if(_city)
[ret appendString:_city];
if(_city && _state)
[ret appendString:@","];
if(_streetAddress && (_city || _state || _zip))
[ret appendString:@"·"];
if(_streetAddress)
[ret appendString:_streetAddress];
if(_zip)
[ret appendFormat:@", %@", _zip];
_subtitle = ret;
}
return _subtitle;
}
@end

annocation大头针定位

关于大头针的view

1
2
3
4
5
6
7
8
9
10
11
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"];
if(!annotationView){
annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"PIN_ANNOTATION"];
}
annotationView.pinColor = MKPinAnnotationColorRed;
annotationView.animatesDrop = YES;
annotationView.canShowCallout = YES;
return annotationView;
}

关于有经纬度编码+反编码的部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
CLLocation *location = [[CLLocation alloc] initWithLatitude:self.latitude longitude:self.longitude];
self.geocoder = [[CLGeocoder alloc] init];
[self.geocoder reverseGeocodeLocation:location
completionHandler:^(NSArray *placemarks, NSError *error)
{
if (error == nil && [placemarks count] > 0){
[_mapView removeAnnotations:_mapView.annotations];
for(int i=0;i<[placemarks count];i++)
{
CLPlacemark *placemark = placemarks[i];
// 调整地图位置和缩放比例
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(placemark.location.coordinate, self.latitudinalMeters, self.longitudinalMeters);
[_mapView setRegion:viewRegion animated:YES];
MapLocation *annotation = [[MapLocation alloc]init];
annotation.title = self.XXXName;
annotation.subtitle = self.locationName;
annotation.streetAddress = placemark.thoroughfare;
annotation.city = placemark.locality;
annotation.state = placemark.administrativeArea;
annotation.zip = placemark.postalCode;
annotation.coordinate = placemark.location.coordinate;
[_mapView addAnnotation:annotation];
}
}
else if (error == nil && [placemarks count] == 0){
NSLog(@"No results were returned.");
}
else if (error != nil){
NSLog(@"An error occurred = %@", error);
}
}];