博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios中地图
阅读量:6458 次
发布时间:2019-06-23

本文共 6339 字,大约阅读时间需要 21 分钟。

 

参考文章  http://blog.csdn.net/tangaowen/article/details/6527901

http://www.cnblogs.com/tangbinblog/archive/2012/07/11/2586715.html

 

 

http://www.cocoachina.com/newbie/basic/2011/1014/3367.html

#import 
#import
#import
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *latTxt;@property (retain, nonatomic) IBOutlet UITextField *lonTxg;@property (retain, nonatomic) IBOutlet UITextField *heightTxt;@end
#import "ViewController.h"#import "MapMark.h"@interface ViewController ()//定位管理@property(nonatomic,retain)CLLocationManager *manager;@property(nonatomic,retain)MKMapView *mapview;@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    self.manager=[[CLLocationManager alloc] init];    _manager.delegate=self;    _manager.desiredAccuracy=kCLLocationAccuracyBest;    _manager.distanceFilter=1000.0;    [_manager release];    self.mapview=[[MKMapView alloc] initWithFrame:CGRectMake(0, 210, 320, 200)];    _mapview.delegate=self;    _mapview.mapType=MKMapTypeStandard;    _mapview.zoomEnabled=YES;    _mapview.showsUserLocation=YES;//调用cclocaiton    [self.view addSubview:_mapview];    [_mapview release];}-(void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    [_manager startUpdatingLocation];    }-(void)viewDidDisappear:(BOOL)animated{    [super viewDidDisappear:animated];    [_manager stopUpdatingLocation];}#pragma mark -CLLocationManager delegate-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{    NSLog(@"__%@",locations);   CLLocation  *lc= [locations lastObject];    self.latTxt.text=[NSString stringWithFormat:@"%3.5f",lc.coordinate.latitude];    self.lonTxg.text=[NSString stringWithFormat:@"%3.5f",lc.coordinate.longitude];    self.heightTxt.text=[NSString stringWithFormat:@"%3.5f",lc.altitude];        //设置显示区域   MKCoordinateRegion region=MKCoordinateRegionMake(lc.coordinate, MKCoordinateSpanMake(0.05, 0.005));    [_mapview setRegion:region animated:YES];    MapMark *mark=[[MapMark alloc] initwithCoordinate:lc.coordinate];    mark.title=@"小A";    mark.subtitle=@"20";    //    、、39.938, 116.416    CLLocationCoordinate2D l2d;    l2d.latitude=37.78594;    l2d.longitude=-122.40717;    MapMark *mark1=[[MapMark alloc] initwithCoordinate:l2d];    mark1.title=@"小b";    mark1.subtitle=@"20";    [_mapview addAnnotation:mark];//只是把坐标的资料,放入到集合中。    [_mapview addAnnotation:mark1];}#pragma mark -map delegate//这个代理方法,会依照坐标资料的集合进行render-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id
)annotation{ NSLog(@"-->vieforannotation-->%@",annotation); //判断是否刚添加的标注 if ([annotation isKindOfClass:[MapMark class]]) { static NSString *bridgeAnnotation=@"annotation"; //从缓存中取值 MKPinAnnotationView *view=(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:bridgeAnnotation]; if (view==nil) { view=[[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:bridgeAnnotation] autorelease]; view.pinColor=MKPinAnnotationColorPurple; view.animatesDrop=YES; view.canShowCallout=YES; UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame=CGRectMake(0, 0, 50, 50); [btn setTitle:@"click" forState:UIControlStateNormal]; view.rightCalloutAccessoryView=btn; } view.annotation=annotation;//充缓存中取出来重新设置坐标信息 return view; }return nil; }//点击标记-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ NSLog(@"-->%@",[view.annotation class]); if ([view.annotation isMemberOfClass:[MapMark class]]) { UIButton *btn=(UIButton *)[view rightCalloutAccessoryView]; NSLog(@"--->%@",btn); }}#define MINIMUM_ZOOM_ARC 0.014 //approximately 1 miles (1 degree of arc ~= 69 miles)#define ANNOTATION_REGION_PAD_FACTOR 1.15#define MAX_DEGREES_ARC 360//size the mapView region to fit its annotations- (void)zoomMapViewToFitAnnotations:(MKMapView *)mapView animated:(BOOL)animated{ NSArray *annotations = mapView.annotations; int count = [mapView.annotations count]; if ( count == 0) { return; } //bail if no annotations //convert NSArray of id
into an MKCoordinateRegion that can be used to set the map size //can't use NSArray with MKMapPoint because MKMapPoint is not an id MKMapPoint points[count]; //C array of MKMapPoint struct for( int i=0; i
)[annotations objectAtIndex:i] coordinate]; points[i] = MKMapPointForCoordinate(coordinate); } //create MKMapRect from array of MKMapPoint MKMapRect mapRect = [[MKPolygon polygonWithPoints:points count:count] boundingMapRect]; //convert MKCoordinateRegion from MKMapRect MKCoordinateRegion region = MKCoordinateRegionForMapRect(mapRect); //add padding so pins aren't scrunched on the edges region.span.latitudeDelta *= ANNOTATION_REGION_PAD_FACTOR; region.span.longitudeDelta *= ANNOTATION_REGION_PAD_FACTOR; //but padding can't be bigger than the world if( region.span.latitudeDelta > MAX_DEGREES_ARC ) { region.span.latitudeDelta = MAX_DEGREES_ARC; } if( region.span.longitudeDelta > MAX_DEGREES_ARC ){ region.span.longitudeDelta = MAX_DEGREES_ARC; } //and don't zoom in stupid-close on small samples if( region.span.latitudeDelta < MINIMUM_ZOOM_ARC ) { region.span.latitudeDelta = MINIMUM_ZOOM_ARC; } if( region.span.longitudeDelta < MINIMUM_ZOOM_ARC ) { region.span.longitudeDelta = MINIMUM_ZOOM_ARC; } //and if there is a sample of 1 we want the max zoom-in instead of max zoom-out if( count == 1 ) { region.span.latitudeDelta = MINIMUM_ZOOM_ARC; region.span.longitudeDelta = MINIMUM_ZOOM_ARC; } [mapView setRegion:region animated:animated];}- (void)viewWillAppear:(BOOL)animated{ [self zoomMapViewToFitAnnotations:_mapview animated:animated]; //or maybe you would do the call above in the code path that sets the annotations array}- (void)dealloc { [_latTxt release]; [_lonTxg release]; [_heightTxt release]; [super dealloc];}@end

其中

 

转载于:https://www.cnblogs.com/gcb999/p/3272466.html

你可能感兴趣的文章
Android项目——实现时间线程源码
查看>>
招商银行信用卡重要通知:消费提醒服务调整,300元以下消费不再逐笔发送短信...
查看>>
python全栈_002_Python3基础语法
查看>>
C#_delegate - 调用列表
查看>>
交换机二层接口access、trunk、hybird三种模式对VLAN的处理过程
查看>>
jQuery.extend 函数详解
查看>>
[转]Windows的批处理脚本
查看>>
lnmp高人笔记
查看>>
[转载] OpenCV2.4.3 CheatSheet学习(三)
查看>>
C#中跨窗体操作(2)--消息机制
查看>>
子程序框架
查看>>
多维数组元素的地址
查看>>
maven的错误记录
查看>>
数据库运维体系_SZMSD
查看>>
aspose 模板输出
查看>>
福大软工1816 · 第三次作业 - 结对项目1
查看>>
selenium多个窗口切换
查看>>
《单页面应用》所获知识点
查看>>
静态库 调试版本 和发布版本
查看>>
DB2 错误码解析
查看>>