開始也是 導(dǎo)入MapKit.framework
IOS8要請(qǐng)求定位
獲取本地位置有兩種方法 1 MKMapItem *mylocation = [MKMapItem mapItemForCurrentLocation];
2 代理方法調(diào)用返回 userLocation(內(nèi)有經(jīng)緯度) 它有經(jīng)緯度再轉(zhuǎn):
CLLocationCoordinate2D coords1 = CLLocationCoordinate2DMake(23.127923,113.388557);
MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords1 addressDictionary:nil]];
import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()<MKMapViewDelegate>{
NSArray *_items;
}
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 地圖類型
// MKMapTypeStandard = 0, 默認(rèn) 標(biāo)準(zhǔn)
// MKMapTypeSatellite, 衛(wèi)星
// MKMapTypeHybrid 混合 = 標(biāo)準(zhǔn) + 衛(wèi)星
self.mapView.mapType = MKMapTypeStandard;
// 用戶位置跟蹤模式
// MKUserTrackingModeNone = 0, //用戶位置,不請(qǐng)?jiān)试S跟蹤
// MKUserTrackingModeFollow, // 用戶位置允許跟蹤
// MKUserTrackingModeFollowWithHeading,用戶位置允許跟蹤(方向)
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
// 設(shè)置mapView代理
self.mapView.delegate = self;
MKMapItem *mylocation = [MKMapItem mapItemForCurrentLocation];
CLLocationCoordinate2D coords1 = CLLocationCoordinate2DMake(23.127923,113.388557);
CLLocationCoordinate2D coords2 = CLLocationCoordinate2DMake(23.05,113.15);
MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords1 addressDictionary:nil]];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords2 addressDictionary:nil]];
_items = [NSArray arrayWithObjects:mylocation, toLocation, nil];
NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES };
// [MKMapItem openMapsWithItems:items launchOptions:options];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES };
[MKMapItem openMapsWithItems:_items launchOptions:options];
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
// 1.當(dāng)前位置詳細(xì)描述
userLocation.title = @"廣州";
userLocation.subtitle = @"天河";
//當(dāng)前的位置詳細(xì)描述,要顯示哪個(gè)城市,哪個(gè)區(qū)-(反地理編碼)
//當(dāng)前位置信息
NSLog(@"%f----%f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
// 2.設(shè)置顯示的region
//MKCoordinateSpan span = MKCoordinateSpanMake(0.193626, 0.145513);
MKCoordinateSpan span = MKCoordinateSpanMake(0.085125, 0.015596);
MKCoordinateRegion region = MKCoordinateRegionMake(self.mapView.userLocation.coordinate, span);
self.mapView.region = region;
#pragma mark 在此方法, 動(dòng)畫效果不起作用,其它方法方法可以
//[self.mapView setRegion:region animated:YES];
}