IOS学习之Map,定位,标记位置的使用
IOS上使用地图比Android要方便,只需要新建一个MKMapView,addSubView即可。这次要实现的效果如下:
有标注(大头针),定位,地图。
1、添加地图
1.1 新一个Single View app ,选择默认项,创建后,在ViewController.h
[cpp]
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@inte易做图ce ViewController : UIViewController
<MKMapViewDelegate, CLLocationManagerDelegate> {
MKMapView *map;
CLLocationManager *locationManager;
}
@end
1.2在ViewController.m中添加
[cpp]
- (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;
注意,如果此时你编译有错误,请拉到博客最后查看 :5、 遇到的问题
2、定位到指定经纬度
[cpp]
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];
这样,就我们就定位的了故宫了。
3、添加标注大头针
3.1 新建一个标注类:CustomAnnotation
按Command+N,继承NSObject。在CustomAnnotation.h 和CustomAnnotation.m文件添加如下代码:
[cpp]
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@inte易做图ce 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
[cpp]
#import "CustomAnnotation.h"
@implementation CustomAnnotation
@synthesize coordinate, title, subtitle;
-(id) initWithCoordinate:(CLLocationCoordinate2D) coords
{
if (self = [super init]) {
coordinate = coords;
}
return self;
}
@end
3.1 使用大头针,
新建个方法添加大头针的
[cpp]
-(void)createAnnotationWithCoords:(CLLocationCoordinate2D) coords {
CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithCoordinate:
coords];
annotation.title = @"标题";
annotation.subtitle = @"子标题";
[map addAnnotation:annotation];
}
调用
[cpp]
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];
这样我们就把大头针定位在故宫了
4、定位到当前位置并获取当前经纬度
前面我们已经添加了locationManager,现在在DidViewLoad里直接调用
[cpp]
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
实现协议方法收到定位成功后的经纬度
[cpp]
- (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);
}
运行,允许获取当前位置,打印log
[cpp]
2012-06-28 23:58:32.237 MapDemo[8202:11603] Lat: 39.9011 Lng: 116.3000
如果不允许:打印出错误日志
[cpp]
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.)"
定位后,移动到当前位置:
[cpp]
- (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];
}
作者:totogo2010
补充:移动开发 , 其他 ,