. h
First in the header file #import <CoreLocation/CoreLocation.h>
Add Cllocationmanagerdelegate Protocol
@property (Strong, nonatomic) Iboutlet Cllocationmanager *mylocationmanager;
. m
Add the following code in-(void) Viewdidload:
Self.mylocationmanager=[[cllocationmanager Alloc]init];
[Self.mylocationmanager requestwheninuseauthorization];
self.mylocationmanager.delegate=self;
Set Precision
Self.mylocationmanager.desiredaccuracy=kcllocationaccuracybest;
Set Location Service Update frequency
self.mylocationmanager.distancefilter=50;
[Self.mylocationmanager startupdatinglocation];
Add method
Location Services
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (nsarray<cllocation *> *) Locations
{
NSLog (@ "GPS is Here");
Cllocation *location=[locations firstobject];//take out first position
cllocationcoordinate2d coordinate=location.coordinate;//Position coordinates
NSLog (@ "Longitude:%f, Latitude:%f, Elevation:%f, heading:%f, walking speed:%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"];
Map Zoom to the point
Get maximum value, minimum value
Set the minimum bounding rectangle for the result of the parameter
Double Xmin=dbl_max;
Double Ymin=dbl_max;
Double Xmax=-dbl_max;
Double Ymax=-dbl_max;
Set the map display range
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];
If you don't need real-time positioning, use it even if you turn off location services
[Self.mylocationmanager stopupdatinglocation];
}
iOS development--Add positioning features