iOS - dostęp do map
Mapy są zawsze pomocne w lokalizowaniu miejsc. Mapy są zintegrowane z iOS przy użyciu struktury MapKit.
Wymagane kroki
Step 1 - Utwórz prostą aplikację opartą na widoku.
Step 2 - Wybierz plik projektu, a następnie wybierz cele, a następnie dodaj MapKit.framework.
Step 3 - Powinniśmy również dodać Corelocation.framework.
Step 4 - Dodaj MapView do ViewController.xib i utwórz ibOutlet i nazwij go mapView.
Step 5 - Utwórz nowy plik, wybierając Plik → Nowy → Plik ... → wybierz klasę celu C i kliknij Dalej.
Step 6 - Nazwij klasę MapAnnotation z „podklasą” jako NSObject.
Step 7 - Wybierz utwórz.
Step 8 - Zaktualizuj MapAnnotation.h w następujący sposób -
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MapAnnotation : NSObject<MKAnnotation>
@property (nonatomic, strong) NSString *title;
@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString *)title andCoordinate:
   (CLLocationCoordinate2D)coordinate2d;
@endStep 9 - Aktualizacja MapAnnotation.m w następujący sposób -
#import "MapAnnotation.h"
@implementation MapAnnotation
-(id)initWithTitle:(NSString *)title andCoordinate:
   (CLLocationCoordinate2D)coordinate2d {
  
   self.title = title;
   self.coordinate =coordinate2d;
   return self;
}
@endStep 10 - Aktualizacja ViewController.h w następujący sposób -
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<MKMapViewDelegate> {
   MKMapView *mapView;
}
@endStep 11 - Aktualizacja ViewController.m w następujący sposób -
#import "ViewController.h"
#import "MapAnnotation.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
   [super viewDidLoad];
   mapView = [[MKMapView alloc]initWithFrame:
   CGRectMake(10, 100, 300, 300)];
   mapView.delegate = self;
   mapView.centerCoordinate = CLLocationCoordinate2DMake(37.32, -122.03);
   mapView.mapType = MKMapTypeHybrid;
   CLLocationCoordinate2D location;
   location.latitude = (double) 37.332768;
   location.longitude = (double) -122.030039;
   
   // Add the annotation to our map view
   MapAnnotation *newAnnotation = [[MapAnnotation alloc]
   initWithTitle:@"Apple Head quaters" andCoordinate:location];
   [mapView addAnnotation:newAnnotation];
   CLLocationCoordinate2D location2;
   location2.latitude = (double) 37.35239;
   location2.longitude = (double) -122.025919;
   MapAnnotation *newAnnotation2 = [[MapAnnotation alloc] 
   initWithTitle:@"Test annotation" andCoordinate:location2];
   [mapView addAnnotation:newAnnotation2];
   [self.view addSubview:mapView];
}
// When a map annotation point is added, zoom to it (1500 range)
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
   MKAnnotationView *annotationView = [views objectAtIndex:0];
   id <MKAnnotation> mp = [annotationView annotation];
   MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance
   ([mp coordinate], 1500, 1500);
   [mv setRegion:region animated:YES];
   [mv selectAnnotation:mp animated:YES];
}
- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}
@endWynik
Po uruchomieniu aplikacji otrzymamy dane wyjściowe, jak pokazano poniżej -
 
                Kiedy przewiniemy mapę w górę, otrzymamy dane wyjściowe, jak pokazano poniżej -
