iOS MKMapView顯示地址及路線
阿新 • • 發佈:2018-11-13
導讀:本篇博文實現兩點:
(1)根據地名在地圖上顯示位置
(2)開啟系統地圖檢視周邊及路線圖
注意:要在info.plist裡面新增 NSLocationWhenInUseDescription 和 Privacy - Location Always Usage Description,兩個引數值都是YES
#import "THMapViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
//這個是用於顯示大頭針效果
#import "THMapLocation.h"
@interface THMapViewController()<CLLocationManagerDelegate,MKMapViewDelegate>
/**
* 地圖
*/
@property(strong, nonatomic) MKMapView *mapview;
@property (nonatomic, strong) CLLocationManager *locationManager;
/**
建立一個地理編碼器,來實現編碼和反編碼
*/
@property (nonatomic, strong) CLGeocoder *geocoder;
@end
@implementation THMapViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"活動地圖";
[self setMapView];
_geocoder=[[CLGeocoder alloc]init];
[self getLocation];
}
/**
建立ui
*/
- (void)setMapView{
//地圖
MKMapView *mapview =[[MKMapView alloc]init];
mapview.size = CGSizeMake(THScreenW, THfloat(475));
mapview.x = 0;
mapview.y = 0;
mapview.mapType = MKMapTypeStandard ;
mapview.scrollEnabled = YES;
mapview.delegate = self;
self.mapview = mapview;
[self.scrollView addSubview:mapview];
//檢視路線
UIButton *btnFind = [[UIButton alloc]initWithFrame:CGRectMake(THScreenW/2-THfloat(100), CGRectGetMaxY(mapview.frame)+THfloat(7), THfloat(200), THfloat(18))];
[btnFind setImage:[UIImage imageNamed:@"activity_area"] forState:UIControlStateNormal];
[btnFind setTitle:@"檢視路線及周邊" forState:UIControlStateNormal];
[btnFind setTitleColor:[UIColor hexChangeFloat:@"333333"] forState:UIControlStateNormal];
btnFind.titleLabel.font = [UIFont systemFontOfSize:THfloat(14)];
[btnFind addTarget:self action:@selector(turnByTurn) forControlEvents:UIControlEventTouchUpInside];
[btnFind setTitleEdgeInsets:UIEdgeInsetsMake(0,btnFind.imageView.frame.size.height+THfloat(14), 0.0,0.0)];
[btnFind setImageEdgeInsets:UIEdgeInsetsMake(0,0,0.0,btnFind.titleLabel.frame.size.height)];
btnFind.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[scrollView addSubview:btnFind];
}
/**
在地圖上定位活動地址
*/
- (void)getLocation{
[_geocoder geocodeAddressString:@"北京市" completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"查詢記錄數: %lu",(unsigned long)[placemarks count]);
if ([placemarks count ] > 0) {
//移除目前地圖上得所有標註點
[self.mapview removeAnnotations:self.mapview.annotations];
}
for (int i = 0; i< [placemarks count]; i++) {
CLPlacemark * placemark = placemarks[i];
//調整地圖位置和縮放比例,第一個引數是目標區域的中心點,第二個引數:目標區域南北的跨度,第三個引數:目標區域的東西跨度,單位都是米
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(placemark.location.coordinate, 10000, 10000);
//重新設定地圖檢視的顯示區域
[self.mapview setRegion:viewRegion animated:YES];
// 例項化 MapLocation 物件
THMapLocation * annotation = [[THMapLocation 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:就會被回撥
[self.mapview addAnnotation:annotation];
}
}];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
self.mapview.centerCoordinate = userLocation.location.coordinate;
}
- (void)mapViewDidFailLoadingMap:(MKMapView *)theMapView withError:(NSError *)error {
NSLog(@"error : %@",[error description]);
}
/**
開啟系統地圖檢視路線
*/
- (void)turnByTurn{
//地理編碼
[_geocoder geocodeAddressString:@"北京市" completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *clPlacemark2=[placemarks firstObject];//獲取第一個地標
MKPlacemark *mkPlacemark2=[[MKPlacemark alloc]initWithPlacemark:clPlacemark2];
NSDictionary *[email protected]{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard),MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving};
MKMapItem *mapItem1=[MKMapItem mapItemForCurrentLocation];//當前位置
MKMapItem *mapItem2=[[MKMapItem alloc]initWithPlacemark:mkPlacemark2];
[MKMapItem openMapsWithItems:@[mapItem1,mapItem2] launchOptions:options];
}];
}
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
[self.mapview removeFromSuperview];
[self.view addSubview:mapView];
[self applyMapViewMemoryHotFix];
}
- (void)dealloc {
self.mapview.showsUserLocation = NO;
self.mapview.userTrackingMode = MKUserTrackingModeNone;
[self.mapview.layer removeAllAnimations];
[self.mapview removeAnnotations:self.mapview.annotations];
[self.mapview removeOverlays:self.mapview.overlays];
[self.mapview removeFromSuperview];
self.mapview.delegate = nil;
self.mapview = nil;
}
- (void)applyMapViewMemoryHotFix{
switch (self.mapview.mapType){
case MKMapTypeHybrid:
{
self.mapview.mapType = MKMapTypeStandard;
}
break;
case MKMapTypeStandard:
{
self.mapview.mapType = MKMapTypeHybrid;
} break;
default:
break;
}
self.mapview.mapType = MKMapTypeStandard;
}
/*
**
* @file: THMapLocation
* @brief:地圖
* Copyright: Copyright (c) 2017
*
* @date: 2017-08-29
**/
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface THMapLocation : 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
*.m檔案*
#import "THMapLocation.h"
@implementation THMapLocation
#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;
}