這個問題困擾我兩天了,在網絡上搜索的都是利用手機定位,而後顯示到地圖上,雖然原理和過程並沒有二致,但老是不太直觀,推敲很久也不得其解,最後只能在貼吧上請教大神,幸好獲得一位熱心網友的耐心解答,終於解決這個問題。實在是萬分感謝!!!網絡
在storyboard中添加一個mapView控件,和viewController中的mapView屬性關聯起來,設置mapView的delegate是viewController。atom
// // ViewController.m // Map2 // // Created by hehongbo on 15/12/9. // Copyright (c) 2015年 hhb. All rights reserved. // #import "ViewController.h" #import <MapKit/MapKit.h> #import "MapAnnotation.h" @interface ViewController () <MKMapViewDelegate> @property (weak, nonatomic) IBOutlet MKMapView *mapView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 設置代理 self.mapView.delegate = self; CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(10.79871, 106.73293); MapAnnotation *annotation = [[MapAnnotation alloc] init]; annotation.coordinate = coordinate; annotation.title = @"設備當前的位置"; [self.mapView addAnnotation:annotation]; [self.mapView setRegion:MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000)]; } - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MapSample"]; annotationView.canShowCallout = YES; return annotationView; } @end
2.由於須要指示經緯度的位置,使用了MapAnnotation,新建一個annotion類,繼承NSObject,爲這個類設置兩個屬性,CLLocationCoordinate2D、title.
代理
// // MapAnnotation.h // Map2 // // Created by hehongbo on 15/12/9. // Copyright (c) 2015年 hhb. All rights reserved. // #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface MapAnnotation : NSObject <MKAnnotation> @property (nonatomic) CLLocationCoordinate2D coordinate; @property (nonatomic, copy) NSString *title; @end