标签:
.h
首先在头文件中#import <CoreLocation/CoreLocation.h>
添加CLLocationManagerDelegate协议
@property (strong, nonatomic) IBOutlet CLLocationManager *myLocationManager;
.m
在- (void)viewDidLoad添加以下代码:
self.myLocationManager=[[CLLocationManager alloc]init];
[self.myLocationManager requestWhenInUseAuthorization];
self.myLocationManager.delegate=self;
//设置精度
self.myLocationManager.desiredAccuracy=kCLLocationAccuracyBest;
//设置定位服务更新频率
self.myLocationManager.distanceFilter=50;
[self.myLocationManager startUpdatingLocation];
添加方法
//定位服务
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
NSLog(@"GPS is here");
CLLocation *location=[locations firstObject];//取出第一个位置
CLLocationCoordinate2D coordinate=location.coordinate;//位置坐标
NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);
float a=coordinate.latitude;
float b=coordinate.longitude;
AGSPoint *point=[AGSPoint new];
point=[AGSPoint pointWithX:b y:a spatialReference:self.mapView.spatialReference];
AGSSimpleMarkerSymbol *mySymbol=[AGSSimpleMarkerSymbol simpleMarkerSymbol];
mySymbol.color = [UIColor blueColor];
mySymbol.outline.color=[UIColor redColor];
mySymbol.outline.width=1;
AGSGraphic *g=[AGSGraphic new];
g=[AGSGraphic graphicWithGeometry:point symbol:mySymbol attributes:nil];
[self.graphicsLayer addGraphic:g];
[self.mapView addMapLayer:self.graphicsLayer withName:@"GPS"];
//地图放大到点
//获取最大值,最小值
//设置参数求结果的最小外接矩形
double xmin=DBL_MAX;
double ymin=DBL_MAX;
double xmax=-DBL_MAX;
double ymax=-DBL_MAX;
//设置地图显示范围
xmin=b-0.01;
ymin=a-0.01;
xmax=b+0.01;
ymax=a+0.01;
AGSMutableEnvelope *extent=[AGSMutableEnvelope envelopeWithXmin:xmin ymin:ymin xmax:xmax ymax:ymax spatialReference:self.mapView.spatialReference];
[extent expandByFactor:1.5];
[self.mapView zoomToEnvelope:extent animated:YES];
//如果不需要实时定位,使用完即使关闭定位服务
[self.myLocationManager stopUpdatingLocation];
}
标签:
原文地址:http://www.cnblogs.com/sandyLovingCoding/p/5734462.html