iOS开发--添加定位功能

.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];
}

转载于:https://www.cnblogs.com/sandyLovingCoding/p/5734462.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值