首页 > 代码库 > iOS MKMapView嵌入地图

iOS MKMapView嵌入地图

要看到那个google的地图,在实现上也相当简便。嵌入地图时需要MKMapView这个类,
它有很多方法和属性,不过如果只是想得到基本的定位功能的话,只需实例化一个对像然后加到当前的
view上就可以了。
<一>先介绍一下,它的几个常用的属性。
 region 用来设置地图的那一部份被显示,它是一个结构体,定义如下:
  typedef struct{
          CLLocationCoordinate2D center;//表示显示的中心
          MKCoordinateSpan span;        //表示比例
}MKCoordinateRegion;
对于MKCoordinateSpan其定义如下:
typedef struct{
CLLocationDegrees latitudeDelta;//这类型在前一节中讲过了,是double型的
CLLocationDegrees longitudeDlta;
}MKCoordinateSpan;
再看一下maptype属性,它用于设置地图的类型,如下所示:
      MapType属性值                 描述
    MKMapTypeStandard           表示标准的街道级地图
    MKMapTypeSatellite          表示卫星图
    MKMapTypeHybird             表示上面两者的混合
其余的就不再一一介绍了,去看看相关的文档即可,在这里已经可以把地图弄出来了。
<二>下面我们把上一节中的代码改一下:
    .h头文件
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface CoreLocationViewController : UIViewController
<CLLocationManagerDelegate,MKMapViewDelegate>{
        MKMapView *map;
        CLLocationManager *locManager;
        CLLocationCoordinate2D loc;
}
@property (nonatomic, retain) MKMapView *map;
@property (nonatomic, retain) CLLocationManager *locManager;
- (void)setCurrentLocation:(CLLocation *)location;
@end
    .m源文件
#import "CoreLocationViewController.h"
@implementation CoreLocationViewController
@synthesize map;
@synthesize locManager;
- (void)viewDidLoad {
        map = [[MKMapView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 411.0f)];
        map.showsUserLocation = YES;
        [self.view addSubview:map];
        locManager = [[CLLocationManager alloc] init];
        locManager.delegate = self;
        locManager.desiredAccuracy = kCLLocationAccuracyBest;
        locManager.distanceFilter = 100;
        [locManager startUpdatingLocation];
        [super viewDidLoad];
}
 
- (void)dealloc {
        [map release];
        [locManager release];
        [super dealloc];
}
#pragma mark -
#pragma mark Core Location Delegate Methods
- (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
                   fromLocation:(CLLocation *)oldLocation {
        NSLog(@"---------------");
        loc = [newLocation coordinate];
        MKCoordinateRegion region;
        MKCoordinateSpan span;
        span.latitudeDelta=0.1; //zoom level
        span.longitudeDelta=0.1; //zoom level
        NSLog(@"%f",loc.latitude);
        NSLog(@"%f",loc.longitude);
        region.span=span;
        region.center=loc;
        // map.showsUserLocation=NO;
         map.mapType = MKMapTypeStandard;
        [map setRegion:region animated:YES];
        [map regionThatFits:region];
}
- (void)locationManager:(CLLocationManager *)manager
           didFailWithError:(NSError *)error{
        NSString *errorMessage;
        if ([error code] == kCLErrorDenied){
                errorMessage = @"被拒绝访问";
        }
        if ([error code] == kCLErrorLocationUnknown) {
                errorMessage = @"";
        }
        UIAlertView *alert = [[UIAlertView alloc]
                                                  initWithTitle:nil
                                                  message:errorMessage
                                                  delegate:self
                                                  cancelButtonTitle:@"纭畾"
                                                  otherButtonTitles:nil];
        [alert show];
        [alert release];
}
- (void)setCurrentLocation:(CLLocation *)location {
        MKCoordinateRegion region ;
        region.center = location.coordinate;
        region.span.longitudeDelta = 0.15f;
        region.span.latitudeDelta = 0.15f;
        [map setRegion:region animated:YES];
}
@end

iOS MKMapView嵌入地图