IOS开发-地图 (mapkit)实验
IOS 地图控件 : mapkit
第一步
显示地图
- - (void)viewDidLoad
- {
- self.mapView=[[[MKMapView alloc] initWithFrame:self.view.bounds] autorelease];
- mapView.delegate=self;
- mapView.autoresizingMask= (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
- [self.view addSubview:mapView];
- [self.mapView setZoomEnabled:YES];
- [self.mapView setScrollEnabled:YES];
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- }
- (void)viewDidLoad
{ self.mapView=[[[MKMapView alloc] initWithFrame:self.view.bounds] autorelease];mapView.delegate=self;mapView.autoresizingMask= (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);[self.view addSubview:mapView];[self.mapView setZoomEnabled:YES];[self.mapView setScrollEnabled:YES]; [super viewDidLoad];// Do any additional setup after loading the view from its nib.
}
也可以直接托控件。 直接运行,OK . 没问题。
第二步:
启动定位服务,标注自己当前位置
- if (lm) {
- lm.delegate=nil;
- [lm release];
- lm=nil;
- }
- lm=[[CLLocationManager alloc] init];
- lm.delegate=self;
- lm.desiredAccuracy= kCLLocationAccuracyNearestTenMeters;
- lm.distanceFilter =1000.0f;
- [lm startUpdatingLocation];
if (lm) {lm.delegate=nil;[lm release];lm=nil;} lm=[[CLLocationManager alloc] init]; lm.delegate=self;lm.desiredAccuracy= kCLLocationAccuracyNearestTenMeters;lm.distanceFilter =1000.0f; [lm startUpdatingLocation];
如果定位成功 :
- -(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
- {
- if (!newLocation) {
- [self locationManager:manager didFailWithError:(NSError *)NULL];
- }
- if (signbit(newLocation.horizontalAccuracy)) {
- [self locationManager:manager didFailWithError:(NSError *)NULL];
- return;
- }
- [manager stopUpdatingLocation];
- CLLocationCoordinate2D _coordination = [newLocation coordinate];
- now_lat = _coordination.latitude;
- now_lng =_coordination.longitude;
- mapView.showsUserLocation =YES;
- [lm stopUpdatingLocation];
- ///
- [NSThread detachNewThreadSelector:@selector(getNear) toTarget:self withObject:nil];
- }
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{if (!newLocation) {[self locationManager:manager didFailWithError:(NSError *)NULL]; } if (signbit(newLocation.horizontalAccuracy)) {[self locationManager:manager didFailWithError:(NSError *)NULL]; return;} [manager stopUpdatingLocation]; CLLocationCoordinate2D _coordination = [newLocation coordinate]; now_lat = _coordination.latitude;now_lng =_coordination.longitude; mapView.showsUserLocation =YES;[lm stopUpdatingLocation]; /// [NSThread detachNewThreadSelector:@selector(getNear) toTarget:self withObject:nil];}
这样便可以定自己当前位置。
/***
线程是根据当前经纬度 从网络上获取附近 。
返回JSON 字符串, 然后解析json 。
得到每个对象经纬度
**/
第三步:
在地图上画圈
首先在 头文件定义 :
- @property(nonatomic,retain)MKCircle *circle;
@property(nonatomic,retain)MKCircle *circle;
圈的颜色,属性
- -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
- {
- MKOverlayView *view=nil;
- if ([overlay isKindOfClass:[MKCircle class]]) {
- MKCircleView *cirView =[[MKCircleView alloc] initWithCircle:overlay];
- cirView.fillColor=[UIColor redColor];
- cirView.strokeColor=[UIColor redColor];
- cirView.alpha=0.1;
- cirView.lineWidth=4.0;
- view=[cirView autorelease];
- }
- return view;
- }
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{ MKOverlayView *view=nil;if ([overlay isKindOfClass:[MKCircle class]]) {MKCircleView *cirView =[[MKCircleView alloc] initWithCircle:overlay]; cirView.fillColor=[UIColor redColor]; cirView.strokeColor=[UIColor redColor]; cirView.alpha=0.1;cirView.lineWidth=4.0;view=[cirView autorelease];} return view;
}
第四步:
显示自己位置和为地图添加标注
- @interface POI : NSObject<MKAnnotation>
- {
- CLLocationCoordinate2D coordinate;
- NSString *subtitle;
- NSString *title;
- NSString *cofeId ;
- NSString *doroname;
- }
- @property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
- @property (nonatomic,copy) NSString *subtitle;
- @property (nonatomic,copy) NSString *title;
- @property (nonatomic,copy) NSString *xId ;
- @property (nonatomic,copy) NSString *name; ;
- -(id) initWithCoords:(CLLocationCoordinate2D) coords;
@interface POI : NSObject<MKAnnotation>
{CLLocationCoordinate2D coordinate;NSString *subtitle;NSString *title; NSString *cofeId ; NSString *doroname;
}@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString *subtitle;
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *xId ;
@property (nonatomic,copy) NSString *name; ; -(id) initWithCoords:(CLLocationCoordinate2D) coords;
- @implementation POI
- @synthesize coordinate,subtitle,title;
- @synthesize xId;
- @synthesize doroname;
- - (id) initWithCoords:(CLLocationCoordinate2D) coords{
- self = [super init];
- if (self) {
- coordinate = coords;
- }
- return self;
- }
- - (void) dealloc
- {
- [title release];
- [subtitle release];
- [xId release];
- [doroname release];
- [super dealloc];
- }
- @end
@implementation POI@synthesize coordinate,subtitle,title;
@synthesize xId;
@synthesize doroname;- (id) initWithCoords:(CLLocationCoordinate2D) coords{self = [super init];if (self) {coordinate = coords; }return self;}- (void) dealloc{[title release];[subtitle release]; [xId release]; [doroname release];[super dealloc];
}@end
这个网上很多, 可以根据需求自己适当修改 。
- doors=[jsonDic objectForKey:@"roomshops"];
- for (NSDictionary *dic in doors) {
- CLLocationCoordinate2D p1;
- p1.latitude= [[dic objectForKey:@"roomLng"] doubleValue];
- p1.longitude=[[dic objectForKey:@"roomLat"] doubleValue];
- POI *poi = [[[POI alloc] initWithCoords:p1] autorelease];
- poi.title=[dic objectForKey:@"roomName"];
- poi.subtitle=[dic objectForKey:@"roomAddress"];
- poi.xId= [dic objectForKey:@"roomid"];
- poi.doroname=[dic objectForKey:@"room"];
- [mapView addAnnotation:poi];
- }
doors=[jsonDic objectForKey:@"roomshops"]; for (NSDictionary *dic in doors) { CLLocationCoordinate2D p1; p1.latitude= [[dic objectForKey:@"roomLng"] doubleValue]; p1.longitude=[[dic objectForKey:@"roomLat"] doubleValue];POI *poi = [[[POI alloc] initWithCoords:p1] autorelease]; poi.title=[dic objectForKey:@"roomName"];poi.subtitle=[dic objectForKey:@"roomAddress"]; poi.xId= [dic objectForKey:@"roomid"]; poi.doroname=[dic objectForKey:@"room"];[mapView addAnnotation:poi];}
解析json。
这里要说明以下, google地图先纬度,再经度。 而一般我们都是先经度,再纬度。
地图标注 :
- - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
- {
- /// 判断是否是自己
- if ([annotation isKindOfClass:[POI class]]) {
- MKAnnotationView *view = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:[annotation title] ];
- if (view==nil) {
- view= [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation title]] autorelease];
- }
- else
- {
- view.annotation=annotation;
- }
- POI *pview= annotation;
- if ([pview.doroname isEqual:@"你大爷"]) {
- [view setImage:[UIImage imageNamed:@"poi.png"]];
- view.canShowCallout=YES;
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
- view.rightCalloutAccessoryView=btn;
- }
- else
- {
- [view setImage:[UIImage imageNamed:@"大小_选中.png"]];
- view.canShowCallout=YES;
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
- view.rightCalloutAccessoryView=btn;
- }
- return view;
- }
- else
- {
- POI *Mapannotation = annotation;
- Mapannotation.title=@"当前位置";
- return nil;
- }
- }
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { /// 判断是否是自己if ([annotation isKindOfClass:[POI class]]) {MKAnnotationView *view = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:[annotation title] ]; if (view==nil) {view= [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation title]] autorelease]; } else{view.annotation=annotation;} POI *pview= annotation; if ([pview.doroname isEqual:@"你大爷"]) {[view setImage:[UIImage imageNamed:@"poi.png"]]; view.canShowCallout=YES; UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];view.rightCalloutAccessoryView=btn;} else{[view setImage:[UIImage imageNamed:@"大小_选中.png"]]; view.canShowCallout=YES; UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];view.rightCalloutAccessoryView=btn;}return view;} else{ POI *Mapannotation = annotation; Mapannotation.title=@"当前位置"; return nil; }}
截图说明,因为我圈是加在中间的, 所以会这样。
转载于:https://www.cnblogs.com/DamonTang/archive/2012/07/12/2588347.html
IOS开发-地图 (mapkit)实验相关推荐
- iOS开发--地图与定位
iOS开发--地图与定位 概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们 ...
- iOS开发地图的代码和方法
类方法 说明 + (BOOL)locationServicesEnabled; 是否启用定位服务,通常如果用户没有启用定位服务可以提示用户打开定位服务 + (CLAuthorizationStatus ...
- IOS开发之——MapKit地图
1.概述 插入MapView,设置Delegate(一般为Controller),Annotations记录兴趣位置点(AnnotationView用来显示兴趣位置点),annotation是可选的, ...
- 转-iOS开发系列--地图与定位
来自: http://www.cnblogs.com/kenshincui/p/4125570.html#autoid-3-4-0 概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功 ...
- iOS学习笔记-地图MapKit入门
代码地址如下: http://www.demodashi.com/demo/11682.html 这篇文章还是翻译自raywenderlich,用Objective-C改写了代码.没有逐字翻译,如有错 ...
- iOS开发那些事-iOS6苹果地图有用开发
在iOS 6之后,不再使用谷歌地图了,而是使用苹果自己的地图,可是API编程接口没有太大的变化.开发者不须要再学习非常多新东西就能开发地图应用,这是负责任的做法.因此本节介绍的内容也相同适用于iOS5 ...
- IOS开发百度地图API入门到精通-用点生成路线,导航,气泡响应
(转)IOS开发百度地图API入门到精通-用点生成路线,导航,气泡响应 IOS百度地图API开发自定义气泡,点击气泡自动生成路线,以及拖拽 IOS百度地图开发POISearch搜索附近停车场,附近加油 ...
- IOS开发百度地图API
IOS百度地图API开发自定义气泡,点击气泡自动生成路线,以及拖拽 IOS百度地图开发POISearch搜索附近停车场,附近加油站 IOS百度地图视角跳到用户当前位置 IOS百度地图开发实时路况 IO ...
- ios开发中如何调用苹果自带地图导航
前段时间一直在赶项目,在外包公司工作就是命苦,天天加班不说,工作都是和工期合同挂钩的,稍微逾期就有可能被扣奖金,不谈这些伤脑筋的事情了,让我们说说iOS开发中如何调用苹果手机自带的地图. 学习如逆水行 ...
最新文章
- python对笔记本电脑的要求-Python的用法笔记本
- UA MATH564 概率论II 连续型随机变量1
- 利用nodeJs anywhere搭建本地服务器环境
- Cass2008 for CAD2006 安装
- BZOJ 1529: [POI2005]ska Piggy banks( 并查集 )
- 开源数据屏蔽 数据加密_数据屏蔽或更改行为信息
- 如何在 Serverless K8s 集群中低成本运行 Spark 数据计算?
- 解决 Illegal DefaultValue null for parameter type integer 异常
- python入门视频教程-Python入门视频全套教程
- 证件照电子版该如何在手机上制作
- RC电路 波形分析
- 动态分析Android App之动态调试(一)
- PuTTY key format too new怎么解决?
- 微信视频号打造带货闭环:主播叫苦连天
- 最优化及其运用 学习笔记(二)
- 基于envoy的分布式网关-contour
- pymol安装与分子对接前预处理蛋白质
- Linux C哲学家吃饭问题
- 复现NC图表:相关性分析气泡图(热图)---同时展示正负调控关系和显著性
- 富士胶片消毒喷雾及湿巾在日本证实可抑制新冠病毒感染
热门文章
- python的工资为什么这么低-现在为什么 Python 这么火?
- python新手小项目-推荐:一个适合于Python新手的入门练手项目
- 为何python不好找工作k-给急着找工作的人一些建议,别在被骗了!
- python从入门到精通怎么样-Python 从入门到精通:一个月就够了
- python填写excel-Python|读、写Excel文件(三种模块三种方式)
- 如何自学python爬虫-小白如何快速学习Python爬虫?
- python界面翻译-我用40行python代码写一个桌面翻译器,很nice
- python测试开发自学教程-自动化平台测试开发:Python测试开发实战_PDF电子书
- python读取文件名-python读取文件名并改名字的实例
- 编程软件python中的if用法-给Python初学者的一些编程技巧