鍍金池/ 教程/ iOS/ IOS - 訪問地圖
iOS - Switches(切換/開關(guān))
iOS - Labels(標(biāo)簽)
iOS - Table View(表格視圖)
IOS - 攝像頭管理
iOS - Text View(文本視圖)
IOS - 開發(fā)環(huán)境配置
iOS教程
iOS - Twitter & Facebook
iOS - UI元素
iOS - iAd 整合
IOS - 應(yīng)用程序調(diào)試
iOS - Split View(分割視圖)
iOS - Status Bar(狀態(tài)欄)
iOS - Navigation Bar(導(dǎo)航欄)
iOS - Tab bar(標(biāo)簽欄)
IOS - 文件處理
IOS - 自動(dòng)布局
iOS - Image View(圖像視圖)
iOS - 應(yīng)用程序內(nèi)購(gòu)買
iOS - Pickers(選取器)
iOS - Delegates實(shí)例
iOS - 創(chuàng)建第一個(gè)iPhone應(yīng)用
iOS (iPhone, iPad)教程
iOS - 發(fā)送電子郵箱(Email)
iOS - View Transitions(視圖轉(zhuǎn)換)
iOS - 內(nèi)存管理
iOS - Icons(圖標(biāo))
iOS - 音頻和視頻
iOS - Storyboards(演示圖板演)
iOS - Buttons(按鈕)
iOS - Text Field(文本域)
iOS - Sliders(滑動(dòng)條)
iOS - Scroll View(滾動(dòng)視圖)
IOS - 輸入類型 文本字段
iOS - 位置處理
iOS - Accelerometer(加速度傳感器)
IOS - 快速入門
iOS - SQLite 數(shù)據(jù)庫(kù)
iOS - GameKit
IOS - 訪問地圖
iOS - Objective-C基礎(chǔ)
iOS - Toolbar(工具欄)
IOS - 動(dòng)作和插座(Outlets)
iOS - Alerts(警示)
IOS - 通用應(yīng)用程序

IOS - 訪問地圖

簡(jiǎn)介

地圖總是幫助我們找到地方。地圖整合到 iOS MapKit框架工作。

 

涉及的步驟

1. 創(chuàng)建一個(gè)簡(jiǎn)單的應(yīng)用程序

2. 選擇項(xiàng)目文件,然后選擇目標(biāo),添加MapKit.framework

3. 我們還應(yīng)該加上Corelocation.framework

4.添加一個(gè)IBOutlet ViewController.xib 創(chuàng)建一個(gè)MapView,并將它命名為MapView類

5. 現(xiàn)在創(chuàng)建一個(gè)新的文件,通過選擇 File-> New -> File... -> select Objective C 類然后點(diǎn)擊下一步

6. 命名類名為 MapAnnotation 并繼承自 NSObject

7. 選擇創(chuàng)建

8. 更新內(nèi)容 MapAnnotation.h 如下

#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. 現(xiàn)在更新 MapAnnotation.m 如下

#import "MapAnnotation.h"

@implementation MapAnnotation
-(id)initWithTitle:(NSString *)title andCoordinate:
 (CLLocationCoordinate2D)coordinate2d{    
    self.title = title;
    self.coordinate =coordinate2d;
    return self;
}
@end

11. 現(xiàn)在更新 ViewController.h 如下

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<MKMapViewDelegate>
{
    MKMapView *mapView;
}
@end

12. 現(xiàn)在更新 ViewController.m 如下

#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 yiibai 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
            
上一篇:iOS - iAd 整合下一篇:iOS - GameKit