开始也是 导入MapKit.framework
IOS8要请求定位
获取本地位置有两种方法 1 MKMapItem *mylocation = [MKMapItem mapItemForCurrentLocation];
2 代理方法调用返回 userLocation(内有经纬度) 它有经纬度再转:
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, 默认 标准
// MKMapTypeSatellite, 卫星
// MKMapTypeHybrid 混合 = 标准 + 卫星
self.mapView.mapType = MKMapTypeStandard;
// 用户位置跟踪模式
// MKUserTrackingModeNone = 0, //用户位置,不请允许跟踪
// MKUserTrackingModeFollow, // 用户位置允许跟踪
// MKUserTrackingModeFollowWithHeading,用户位置允许跟踪(方向)
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
// 设置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.当前位置详细描述
userLocation.title = @"广州";
userLocation.subtitle = @"天河";
//当前的位置详细描述,要显示哪个城市,哪个区-(反地理编码)
//当前位置信息
NSLog(@"%f----%f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
// 2.设置显示的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 在此方法, 动画效果不起作用,其它方法方法可以
//[self.mapView setRegion:region animated:YES];
}
网友评论