iOS - Zugriff auf Karten
Karten sind immer hilfreich, um Orte zu finden. Karten werden mithilfe des MapKit-Frameworks in iOS integriert.
Beteiligte Schritte
Step 1 - Erstellen Sie eine einfache ansichtsbasierte Anwendung.
Step 2 - Wählen Sie Ihre Projektdatei aus, wählen Sie dann Ziele aus und fügen Sie MapKit.framework hinzu.
Step 3 - Wir sollten auch Corelocation.framework hinzufügen.
Step 4 - Fügen Sie ViewController.xib eine MapView hinzu, erstellen Sie ein ibOutlet und benennen Sie es als mapView.
Step 5 - Erstellen Sie eine neue Datei, indem Sie Datei → Neu → Datei ... → Ziel C-Klasse auswählen und auf Weiter klicken.
Step 6 - Benennen Sie die Klasse als MapAnnotation mit "Unterklasse von" als NSObject.
Step 7 - Wählen Sie erstellen.
Step 8 - Aktualisieren Sie MapAnnotation.h wie folgt -
#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 - Update MapAnnotation.m wie folgt -
#import "MapAnnotation.h"
@implementation MapAnnotation
-(id)initWithTitle:(NSString *)title andCoordinate:
   (CLLocationCoordinate2D)coordinate2d {
  
   self.title = title;
   self.coordinate =coordinate2d;
   return self;
}
@endStep 10 - Update ViewController.h wie folgt -
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<MKMapViewDelegate> {
   MKMapView *mapView;
}
@endStep 11 - Update ViewController.m wie folgt -
#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.
}
@endAusgabe
Wenn wir die Anwendung ausführen, erhalten wir die Ausgabe wie unten gezeigt -
 
                Wenn wir die Karte nach oben scrollen, erhalten wir die Ausgabe wie unten gezeigt -
