iOS-Kartenentwicklung
Einführung
iOS-Karten helfen uns, unseren Standort zu ermitteln. iOS-Karten verwenden das MapKit-Framework.
Beispielschritte
1. Erstellen Sie eine einfache View-basierte Anwendung
2. Wählen Sie die Projektdatei aus, wählen Sie dann das Target aus und fügen Sie dann MapKit.framework hinzu.
3. Fügen Sie CoreLocation.framework hinzu
4. Fügen Sie ViewController.xib eine Kartenansicht hinzu, erstellen Sie ein IBOutlet und nennen Sie es mapView.
5. Wählen Sie über "File-> New -> File... -> " Objective C class aus, um eine neue Datei zu erstellen, und klicken Sie auf Next
6. Setzen Sie "sub class of" auf NSObject und nennen Sie die Klasse MapAnnotation
7. Wählen Sie Create
8. Aktualisieren Sie MapAnnotation.h wie unten gezeigt
#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; @end
9. Aktualisieren Sie MapAnnotation.m wie unten gezeigt
#import "MapAnnotation.h"
@implementation MapAnnotation
-(id)initWithTitle:(NSString *)title andCoordinate:
(CLLocationCoordinate2D)coordinate2d{
self.title = title;
self.coordinate =coordinate2d;
return self;
}
@end
10. Aktualisieren Sie ViewController.h wie unten gezeigt
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<MKMapViewDelegate>
{
MKMapView *mapView;
}
@end
11. Aktualisieren Sie ViewController.m wie unten gezeigt
#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.
}
@end
Ausgabe
Beim Ausführen der Anwendung sieht das Ausgabeergebnis wie folgt aus

Wenn wir die Karte nach oben scrollen, sieht das Ausgabeergebnis wie folgt aus
