温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

[IOS]地图的简单应用

发布时间:2020-05-07 23:59:56 来源:网络 阅读:397 作者:蓬莱仙羽 栏目:移动开发

IOSMapKit如何快速入门?下面我来写一个简单的Demo,做简要介绍。

效果图:

[IOS]地图的简单应用

操作步骤:

1.首先创建一个项目,在xib的view中添加一个MapVIew控件,并且导入MapKit.framework和CoreLocation.frameword框架。

2.实现ViewController的代码:

ViewController.h:

#import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> @interface DXWViewController : UIViewController<CLLocationManagerDelegate> @property (retain, nonatomic) IBOutlet MKMapView *mapview; @property(retain,nonatomic)CLLocationManager *manager; @property(retain,nonatomic)CLLocation *location; @property (retain, nonatomic) IBOutletCollection(UILabel) NSArray *labels; @end 

ViewController.m:

#import "DXWViewController.h" #import "Place.h" @interface DXWViewController ()  @end  @implementation DXWViewController  - (void)viewDidLoad {     [super viewDidLoad];     self.manager  = [[CLLocationManager alloc] init];     self.manager.delegate = self;     //精度     self.manager.desiredAccuracy = kCLLocationAccuracyBest;     [self.manager startUpdatingLocation]; 	self.mapview.showsUserLocation = YES; }   -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {     self.location = [locations objectAtIndex:0];     [self.labels[0] setText:[NSString stringWithFormat:@"%f\u00B0",self.location.coordinate.longitude]];     [self.labels[1] setText:[NSString stringWithFormat:@"%f\u00B0",self.location.coordinate.longitude]];     [self.labels[2] setText:[NSString stringWithFormat:@"%gm",self.location.horizontalAccuracy]];     [self.labels[3] setText:[NSString stringWithFormat:@"%gm",self.location.altitude]];     [self.labels[4] setText:[NSString stringWithFormat:@"%gm",self.location.verticalAccuracy]];          Place *place = [[Place alloc] init];     place.coordinate = self.location.coordinate;     place.title = @"Title";     place.subtitle = @"subTitle";          [self.mapview addAnnotation:place];     //设置精度范围     MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(place.coordinate, 1000, 1000);     [self.mapview setRegion:region animated:YES]; } - (void)didReceiveMemoryWarning {     [super didReceiveMemoryWarning]; }  - (void)dealloc {     [_mapview release];     [_labels release];     [super dealloc]; } @end

3.创建大头针的类:

Place.h:

#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface Place : NSObject<MKAnnotation>  @property(copy,nonatomic)NSString *title; @property(copy,nonatomic)NSString *subtitle; @property(assign,nonatomic)CLLocationCoordinate2D coordinate;  @end


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI