IOS system map implementation and positioning

Source: Internet
Author: User

IOS system map implementation and positioning

 

1: add the library CoreLocation. framework and 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 * currentlong.pdf; // longitude

-(Void) viewDidLoad {
[Super viewDidLoad];
Self. title = @ "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;
// Locate every several meters (set to any movement here)
_ 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 current user location and the location service is called at this time)
_ 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. currentlong1_doublevalue];
NSString * lacationName = @ "current location ";
BasicMapAnnotation * anno = [[BasicMapAnnotation alloc] initWithLatitude: lat andlongpolling: 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. longpolling );
Self. currentLatitude = [NSString stringWithFormat: @ "%. 4f", userLocation. location. coordinate. latitude];
Self. currentlongpolling = [NSString stringWithFormat: @ "%. 4f", userLocation. location. coordinate. longpolling];
[Self initLocationData];
// Set the map display range (if you do not set the region, the area range is automatically displayed and the current user location is 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 ) Annotation {
If ([annotation isKindOfClass: [BasicMapAnnotation class]) {
Static NSString * key = @ "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 annotation"];
Imageview. frame = frame;
[AnnotationView addSubview: imageview];
}
Return annotationView;
}
Return nil;
}
-(Void) didReceiveMemoryWarning {
[Super didReceiveMemoryWarning];
}

Related Article

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.