IOS system map implementation and positioning

Source: Internet
Author: User


1: Join the library corelocation.framework,mapkit.framework;

2: @property (nonatomic, strong) Cllocationmanager *locationmanager;
@property (nonatomic, strong) Mkmapview *mapview;
@property (nonatomic, strong) cllocation *checkinlocation;
@property (Strong, nonatomic) NSString *currentlatitude; Latitude
@property (Strong, nonatomic) NSString *currentlongitude; Longitude

-(void) Viewdidload {
[Super Viewdidload];
[Email protected] "map";
[Self Initrightbarbutton];
[Self setuplocationmanager];
[Self setupmapview];
}
-(void) initrightbarbutton{
uibutton* _btnright = [UIButton buttonwithtype:uibuttontypecustom];
[_btnright setframe:cgrectmake (0, 0, 39, 34)];
UIImage *imgnormal = [UIImage imagenamed:@ "Gpsbtn_dianji"];
UIImage *imgselect = [UIImage imagenamed:@ "gpsbtn"];
[_btnright Setbackgroundimage:imgnormal Forstate:uicontrolstatenormal];
[_btnright Setbackgroundimage:imgselect forstate:uicontrolstateselected];
_btnright.showstouchwhenhighlighted = YES;
[_btnright addtarget:self Action: @selector (showuserlocation:) forcontrolevents:uicontroleventtouchupinside];
Uibarbuttonitem *buttonitem = [[Uibarbuttonitem alloc] initwithcustomview:_btnright];
Self.navigationItem.rightBarButtonItem = Buttonitem;
}
-(void) setuplocationmanager{
_locationmanager=[[cllocationmanager Alloc]init];
_locationmanager.desiredaccuracy=kcllocationaccuracybestfornavigation;
How many meters are positioned once (this is set to move no matter what)
_locationmanager.distancefilter=kcldistancefilternone;
After IOS 8.0
if ([Self.locationmanager respondstoselector: @selector (requestwheninuseauthorization)]) {
[Self.locationmanager requestwheninuseauthorization];
[Self.locationmanager startupdatinglocation];
}
}
-(void) setupmapview{
_mapview=[[mkmapview Alloc]initwithframe:self.view.bounds];
[Self.view Addsubview:_mapview];
_mapview.delegate=self;
User Location Tracking (user location tracking is used to mark the user's current location, and the location service is called)
_mapview.usertrackingmode=mkusertrackingmodefollowwithheading;
_mapview.maptype=mkmaptypestandard;
[Self initlocationdata];
}
-(void) initlocationdata{
Nsmutablearray *arr=[[nsmutablearray Alloc]init];
for (int i=0; i<1; i++) {
Cllocationdegrees Lat=[self.currentlatitude Doublevalue];
Cllocationdegrees Longi=[self.currentlongitude Doublevalue];
NSString *[email protected] "current position";
Basicmapannotation *anno=[[basicmapannotation Alloc]initwithlatitude:lat Andlongitude:longi];
Anno.title=lacationname;
Anno.index=i;
[Arr Addobject:anno];
}
[_mapview Showannotations:arr Animated:yes];
}
-(void) Showuserlocation: (ID) sender{
Mkcoordinatespan Span=mkcoordinatespanmake (0.01, 0.02);
Mkcoordinateregion Region=mkcoordinateregionmake (_mapview.userlocation.coordinate, span);
[_mapview setregion:region Animated:yes];
}
-(void) Mapview: (Mkmapview *) Mapview didupdateuserlocation: (mkuserlocation *) userlocation{
NSLog (@ "Latitude:%f Longitude:%f", userlocation.location.coordinate.latitude,userlocation.location.coordinate.longitude);
Self.currentlatitude = [NSString stringwithformat:@ "%.4f", UserLocation.location.coordinate.latitude];
Self.currentlongitude = [NSString stringwithformat:@ "%.4f", UserLocation.location.coordinate.longitude];
[Self initlocationdata];
Set the map display range (assuming that you do not make a locale, you actively display the area range and specify the current user location as the Map center point)
Mkcoordinatespan Span=mkcoordinatespanmake (0.01, 0.01);
Mkcoordinateregion Region=mkcoordinateregionmake (_mapview.region.center, span);
[_mapview setregion:region animated:true];
}
-(Mkannotationview *) Mapview: (Mkmapview *) Mapview viewforannotation: (id<mkannotation>) annotation{
if ([Annotation iskindofclass:[basicmapannotation class]]) {
Static nsstring *[email protected] "Annotationkey";
Mkannotationview *annotationview=[_mapview Dequeuereusableannotationviewwithidentifier:key];
Basicmapannotation *aa= (basicmapannotation *) annotation;
if (!annotationview) {
Annotationview=[[mkannotationview Alloc]initwithannotation:aa Reuseidentifier:key];
CGRect Frame=annotationview.frame;
frame.size.width=14;
frame.size.height=35;
Annotationview.frame=frame;
Annotationview.canshowcallout=yes;
Uiimageview*imageview=[[uiimageview alloc]initwithimage:[uiimage imagenamed:@ "map Callout"];
Imageview.frame=frame;
[Annotationview Addsubview:imageview];
}
return annotationview;
}
return nil;
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
}

IOS system map implementation and positioning

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.