做自(zì)由與創造的先行者

iOS地圖開(kāi)發

iOS開(kāi)發手冊

IOS地圖開(kāi)發

簡介

IOS地圖幫助我們定位位置,IOS地圖使用(yòng) MapKit 框架。

實例步驟

1.創建一個簡單的 View based application

2.選擇項目文(wén)件,然後選擇目标,然後添加MapKit.framework.

3.添加 Corelocation.framework

4.向 ViewController.xib 添加地圖查看(kàn)和(hé)創建 ibOutlet 并且命名爲mapView。

5.通過"File-> New -> File... -> "選擇 Objective C class創建一個新的文(wén)件,單擊下(xià)一步

6."sub class of"爲 NSObject,類作(zuò)命名爲MapAnnotation

7.選擇創建

8.更新MapAnnotation.h ,如下(xià)所示

#import

#import

@interface MapAnnotation : NSObject

@property (nonatomic, strong) NSString *title;

@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;

- (id)initWithTitle:(NSString *)title andCoordinate:

(CLLocationCoordinate2D)coordinate2d;

@end

9.更新MapAnnotation.m ,如下(xià)所示

#import "MapAnnotation.h"

@implementation MapAnnotation

-(id)initWithTitle:(NSString *)title andCoordinate:

(CLLocationCoordinate2D)coordinate2d{

self.title = title;

self.coordinate =coordinate2d;

return self;

}

@end

10.更新ViewController.h ,如下(xià)所示

#import

#import

#import

@interface ViewController : UIViewController

{

MKMapView *mapView;

}

@end

11.更新ViewController.m ,如下(xià)所示

#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 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

網站(zhàn)建設開(kāi)發|APP設計(jì)開(kāi)發|小(xiǎo)程序建設開(kāi)發
下(xià)一篇:iOS相機管理(lǐ)
上(shàng)一篇:iOS應用(yòng)内購買