[IOS]地图的简单应用

编程入门 行业动态 更新时间:2024-10-12 05:50:48

[IOS]地图的<a href=https://www.elefans.com/category/jswz/34/1770983.html style=简单应用"/>

[IOS]地图的简单应用

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

效果图:


操作步骤:

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


转载于:

更多推荐

[IOS]地图的简单应用

本文发布于:2024-02-06 12:02:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1748900.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:简单   地图   IOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!