一、有两个文件,一个是显示地图的MapViewController ,另一个是MapLocation.h地图标点类 ,用来实现 MKAnnotation 协议
二、MapViewController
import "MapViewController.h"
import <CoreLocation/CoreLocation.h>
import <MapKit/MapKit.h>
import "MapLocation.h"
@interface MapViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>{
CLLocationManager *_manager;
}
@property (nonatomic,strong)MKMapView *mapView;
@end
@implementation MapViewController
-
(void)viewDidLoad {
[super viewDidLoad];[self navTitle:@"地图"];
self.view.backgroundColor = [UIColor whiteColor];[self initMapView];
}
-
(void)initMapView {
_manager = [[CLLocationManager alloc]init];
_manager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
if ([[UIDevice currentDevice].systemVersion floatValue]>8.0) {
[_manager requestWhenInUseAuthorization];
}
[_manager startUpdatingLocation];
//创建地图
_mapView = [[MKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds];
_mapView.showsUserLocation = NO;
_mapView.delegate =self;
[self.view addSubview:_mapView];
}
-
(void)setLocationName:(NSString *)locationName{
_locationName = locationName;
//根据位置获取经纬度
if (_locationName == nil || [_locationName length] == 0) {
return ;
}CLGeocoder *geocode = [[CLGeocoder alloc] init];
[geocode geocodeAddressString:_locationName completionHandler:^(NSArray *placemarks, NSError *error) {
if ([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, 10000, 10000); //重新设置地图视图的显示区域 [_mapView setRegion:viewRegion animated:YES]; // 实例化 MapLocation 对象 MapLocation * annotation = [[MapLocation alloc] init]; annotation.streetAddress = placemark.thoroughfare ; annotation.city = placemark.locality; annotation.state = placemark.administrativeArea ; annotation.zip = placemark.postalCode; annotation.coordinate = placemark.location.coordinate; //把标注点MapLocation 对象添加到地图视图上,一旦该方法被调用,地图视图委托方法mapView:ViewForAnnotation:就会被回调 [_mapView addAnnotation:annotation]; }
}];
}
三、
mapLocation.h 代码如下:
import <Foundation/Foundation.h>
import <MapKit/MapKit.h>
@interface mapLocation : NSObject<MKAnnotation>
// 地图标点类必须实现 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 ;
@end
mapLocation.m 文件如下:
import "mapLocation.h"
@implementation mapLocation
pragma mark 标点上的主标题
- (NSString *)title{
return @"您的位置!";
}
pragma mark 标点上的副标题
- (NSString *)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];
}
return ret;
}
@end
网友评论