天天看點

iOS學習之Map,定位,标記位置的使用如何發送IOS模拟器經緯度?

iOS上使用地圖比Android要友善,隻需要建立一個MKMapView,addSubView即可。這次要實作的效果如下:

iOS學習之Map,定位,标記位置的使用如何發送IOS模拟器經緯度?

有标注(大頭針),定位,地圖。

1、添加地圖

1.1 新一個Single View app ,選擇預設項,建立後,在ViewController.h 

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController 
<MKMapViewDelegate, CLLocationManagerDelegate> {
    MKMapView *map;
    CLLocationManager *locationManager;
}
@end
           

1.2在ViewController.m中添加

- (void)viewDidLoad
{
    map = [[MKMapView alloc] initWithFrame:[self.view bounds]];
	map.showsUserLocation = YES;
	map.mapType = MKMapTypeSatellite;
    [self.view addSubview:map];


    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}
           

運作:

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

這裡map.mapType =MKMapTypeSatellite;我用到是衛星地圖,可以使用标準的地圖,

map.mapType =MKMapTypeStandard;

iOS學習之Map,定位,标記位置的使用如何發送IOS模拟器經緯度?

注意,如果此時你編譯有錯誤,請拉到部落格最後檢視 :5、 遇到的問題

2、定位到指定經緯度

CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352,116.397105);
	
	float zoomLevel = 0.02;
	MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));
	[map setRegion:[map regionThatFits:region] animated:YES];
		
           

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

iOS學習之Map,定位,标記位置的使用如何發送IOS模拟器經緯度?

3、添加标注大頭針

3.1 建立一個标注類:CustomAnnotation

按Command+N,繼承NSObject。在CustomAnnotation.h 和CustomAnnotation.m檔案添加如下代碼:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface CustomAnnotation : NSObject 
<MKAnnotation>
{
	CLLocationCoordinate2D coordinate;
	NSString *title;
	NSString *subtitle;
}
-(id) initWithCoordinate:(CLLocationCoordinate2D) coords;

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;

@end
           
#import "CustomAnnotation.h"

@implementation CustomAnnotation
@synthesize coordinate, title, subtitle;

-(id) initWithCoordinate:(CLLocationCoordinate2D) coords
{
	if (self = [super init]) {
		coordinate = coords;
	}
	return self;
}
@end
           

3.1 使用大頭針,

建立個方法添加大頭針的

-(void)createAnnotationWithCoords:(CLLocationCoordinate2D) coords {
	CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithCoordinate: 
									coords];
	annotation.title = @"标題";
	annotation.subtitle = @"子标題";
	[map addAnnotation:annotation];
}
           

調用

CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352,116.397105);
	
	float zoomLevel = 0.02;
	MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));
	[map setRegion:[map regionThatFits:region] animated:YES];

	
	[self createAnnotationWithCoords:coords];
           

這樣我們就把大頭針定位在故宮了

iOS學習之Map,定位,标記位置的使用如何發送IOS模拟器經緯度?

4、定位到目前位置并擷取目前經緯度

前面我們已經添加了locationManager,現在在DidViewLoad裡直接調用

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

實作協定方法收到定位成功後的經緯度

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [locationManager stopUpdatingLocation];
    
    NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];
    NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];
    NSLog(@"Lat: %@  Lng: %@", strLat, strLng);

}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"locError:%@", error);

}
           
iOS學習之Map,定位,标記位置的使用如何發送IOS模拟器經緯度?

運作,允許擷取目前位置,列印log

2012-06-28 23:58:32.237 MapDemo[8202:11603] Lat: 39.9011  Lng: 116.3000
           

如果不允許:列印出錯誤日志

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.)"
           

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

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [locationManager stopUpdatingLocation];
    
    NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];
    NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];
    NSLog(@"Lat: %@  Lng: %@", strLat, strLng);
    
    CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(newLocation.coordinate.latitude,newLocation.coordinate.longitude);
	float zoomLevel = 0.02;
	MKCoordinateRegion region = MKCoordinateRegionMake(coords,MKCoordinateSpanMake(zoomLevel, zoomLevel));
	[map setRegion:[map regionThatFits:region] animated:YES];
}
           
iOS學習之Map,定位,标記位置的使用如何發送IOS模拟器經緯度?

定位到了目前位置。

5、會遇到的問題:

運作是發現了編譯錯誤:

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,添加方法如下:

iOS學習之Map,定位,标記位置的使用如何發送IOS模拟器經緯度?

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

iOS學習之Map,定位,标記位置的使用如何發送IOS模拟器經緯度?
iOS學習之Map,定位,标記位置的使用如何發送IOS模拟器經緯度?

就好了。

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

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

iOS學習之Map,定位,标記位置的使用如何發送IOS模拟器經緯度?
iOS學習之Map,定位,标記位置的使用如何發送IOS模拟器經緯度?

這樣就能發送模拟的目前位置了。

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

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

著作權聲明:本文由http://blog.csdn.net/totogo2010/原創,歡迎轉載分享。請尊重作者勞動,轉載時保留該聲明和作者部落格連結,謝謝!