Map,定位,標記位置的使用

IOS上使用地圖比Android要方便,只須要新建一個MKMapView,addSubView便可。此次要實現的效果以下:git

有標註(大頭針),定位,地圖。

 

一、添加地圖

1.1 新一個Single View app ,選擇默認項,建立後,在ViewController.h github

  1. #import <UIKit/UIKit.h>  
  2. #import <MapKit/MapKit.h>  
  3. #import <CoreLocation/CoreLocation.h>  
  4.   
  5. @interface ViewController : UIViewController   
  6. <MKMapViewDelegate, CLLocationManagerDelegate> {  
  7.     MKMapView *map;  
  8.     CLLocationManager *locationManager;  
  9. }  
  10. @end  

1.2在ViewController.m中添加app

  1. - (void)viewDidLoad  
  2. {  
  3.     map = [[MKMapView alloc] initWithFrame:[self.view bounds]];  
  4.     map.showsUserLocation = YES;  
  5.     map.mapType = MKMapTypeSatellite;  
  6.     [self.view addSubview:map];  
  7.   
  8.   
  9.     [super viewDidLoad];  
  10.     // Do any additional setup after loading the view, typically from a nib.  
  11. }  
運行:

OMG,看到的是世界地圖。怎麼定位到指定的位置呢?好比定位回來偉大的祖國首都?atom

這裏map.mapType =MKMapTypeSatellite;我用到是衛星地圖,可使用標準的地圖,spa

map.mapType =MKMapTypeStandard;.net


注意,若是此時你編譯有錯誤,請拉到博客最後查看 :五、 遇到的問題日誌

二、定位到指定經緯度

  1. CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352,116.397105);  
  2.       
  3.     float zoomLevel = 0.02;  
  4.     MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));  
  5.     [map setRegion:[map regionThatFits:region] animated:YES];  
  6.           

這樣,就咱們就定位的了故宮了。

三、添加標註大頭針

3.1 新建一個標註類:CustomAnnotationcode

按Command+N,繼承NSObject。在CustomAnnotation.h 和CustomAnnotation.m文件添加以下代碼:orm

  1. #import <Foundation/Foundation.h>  
  2. #import <MapKit/MapKit.h>  
  3.   
  4. @interface CustomAnnotation : NSObject   
  5. <MKAnnotation>  
  6. {  
  7.     CLLocationCoordinate2D coordinate;  
  8.     NSString *title;  
  9.     NSString *subtitle;  
  10. }  
  11. -(id) initWithCoordinate:(CLLocationCoordinate2D) coords;  
  12.   
  13. @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;  
  14. @property (nonatomic, retain) NSString *title;  
  15. @property (nonatomic, retain) NSString *subtitle;  
  16.   
  17. @end  
  1. #import "CustomAnnotation.h"  
  2.   
  3. @implementation CustomAnnotation  
  4. @synthesize coordinate, title, subtitle;  
  5.   
  6. -(id) initWithCoordinate:(CLLocationCoordinate2D) coords  
  7. {  
  8.     if (self = [super init]) {  
  9.         coordinate = coords;  
  10.     }  
  11.     return self;  
  12. }  
  13. @end  

3.1 使用大頭針,

新建個方法添加大頭針的blog

  1. -(void)createAnnotationWithCoords:(CLLocationCoordinate2D) coords {  
  2.     CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithCoordinate:   
  3.                                     coords];  
  4.     annotation.title = @"標題";  
  5.     annotation.subtitle = @"子標題";  
  6.     [map addAnnotation:annotation];  
  7. }  

調用
  1. CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352,116.397105);  
  2.       
  3.     float zoomLevel = 0.02;  
  4.     MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));  
  5.     [map setRegion:[map regionThatFits:region] animated:YES];  
  6.   
  7.       
  8.     [self createAnnotationWithCoords:coords];  
這樣咱們就把大頭針定位在故宮了


四、定位到當前位置並獲取當前經緯度

前面咱們已經添加了locationManager,如今在DidViewLoad裏直接調用

  1. locationManager = [[CLLocationManager alloc] init];  
  2.     locationManager.delegate = self;  
  3.     [locationManager startUpdatingLocation];  

 

實現協議方法收到定位成功後的經緯度
  1. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {  
  2.     [locationManager stopUpdatingLocation];  
  3.       
  4.     NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];  
  5.     NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];  
  6.     NSLog(@"Lat: %@  Lng: %@", strLat, strLng);  
  7.   
  8. }  
  9.   
  10. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {  
  11.     NSLog(@"locError:%@", error);  
  12.   
  13. }  

運行,容許獲取當前位置,打印log

  1. 2012-06-28 23:58:32.237 MapDemo[8202:11603] Lat: 39.9011  Lng: 116.3000  
若是不容許:打印出錯誤日誌
  1. 2012-06-28 23:25:03.109 MapDemo[7531:11603] locError:Error Domain=kCLErrorDomain Code=1 "The operation couldn’t be completed. (kCLErrorDomain error 1.)"  

定位後,移動到當前位置:

  1. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {  
  2.     [locationManager stopUpdatingLocation];  
  3.       
  4.     NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];  
  5.     NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];  
  6.     NSLog(@"Lat: %@  Lng: %@", strLat, strLng);  
  7.       
  8.     CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(newLocation.coordinate.latitude,newLocation.coordinate.longitude);  
  9.     float zoomLevel = 0.02;  
  10.     MKCoordinateRegion region = MKCoordinateRegionMake(coords,MKCoordinateSpanMake(zoomLevel, zoomLevel));  
  11.     [map setRegion:[map regionThatFits:region] animated:YES];  
  12. }  


定位到了當前位置。

五、會遇到的問題:

運行是發現了編譯錯誤:

Undefined symbols for architecture i386:

  "_CLLocationCoordinate2DMake", referenced from:

      -[ViewController viewDidLoad] in ViewController.o

      -[ViewController locationManager:didUpdateToLocation:fromLocation:] in ViewController.o

  "_OBJC_CLASS_$_MKMapView", referenced from:

      objc-class-ref in ViewController.o

  "_OBJC_CLASS_$_CLLocationManager", referenced from:

      objc-class-ref in ViewController.o

ld: symbol(s) not found for architecture i386

clang: error: linker command failed with exit code 1 (use -v to see invocation)

這是爲何呢?沒有添加對應的FrameWork。我使用的是4.3.2版本的XCode,添加方法以下:

選擇項目,TARGETS ,點加號,添加兩個framework

就行了。

如何發送IOS模擬器經緯度?

5.0以上的模擬器才能用這個功能,打開模擬:

這樣就能發送模擬的當前位置了。

 

 

例子代碼:http://download.csdn.net/detail/totogo2010/4400001點擊打開連接

https://github.com/schelling/YcDemo/tree/master/MapDemo

相關文章
相關標籤/搜索