iOS development--Add positioning features

Source: Internet
Author: User

. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.