蘋果開發自帶地圖,可以直接使用,如果需要經緯度定位,或者擷取其他坐标資訊,還可以接入定位的一些代碼,參考連結:http://blog.csdn.net/w582324909/article/details/53610374
1、首先info.plist檔案請求授權:
2、導入庫:
3、導入頭檔案:
#import <MapKit/MapKit.h>
4代碼部分:
@interface MapViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>
@property (nonatomic) CLLocationCoordinate2D centerCoordinate;
@property (nonatomic) MKCoordinateRegion region;
@property (nonatomic, strong) CLLocationManager* locationMgr;
@property (nonatomic, strong) CLLocation* coordinate;
@property (weak, nonatomic) IBOutlet MKMapView *map;
@end
@implementation MapViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self createButton];
self.locationMgr.distanceFilter = 10.0f;
self.map.showsUserLocation = YES;
self.map.mapType = MKMapTypeStandard;
}
-(void)createButton
{
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, self.view.frame.size.height - 100, 30, 30)];
[btn setImage:[UIImage imageNamed:@"location.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[self.map addSubview:btn];//button的作用是點一下,可以回到目前的位置,類似于找到自己的功能。
}
-(void)back
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.centerCoordinate, 250, 250);
[self.map setRegion:region animated:YES];
}
//MapView委托方法,當定位自身時調用
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocationCoordinate2D loc = [userLocation coordinate];
self.centerCoordinate = loc;
// 放大地圖到自身的經緯度位置。
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
[self.map setRegion:region animated:YES];//有動畫
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
tip:用到地圖、定位的時候盡量使用真機測試~