iOS——地图的使用

来源:互联网 发布:数控激光切割编程教程 编辑:程序博客网 时间:2024/04/30 10:31

苹果开发自带地图,可以直接使用,如果需要经纬度定位,或者获取其他坐标信息,还可以接入定位的一些代码,参考链接: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:用到地图、定位的时候尽量使用真机测试~




0 0
原创粉丝点击