iOS MKMapView嵌入地图

本文介绍了如何在iOS应用中使用MKMapView类进行地图的嵌入和设置,包括设置地图区域、类型以及获取用户位置等功能。通过设置MKMapView的属性和代理方法,实现了标准地图、卫星图和混合图的切换,并展示了如何更新和显示用户的位置。
摘要由CSDN通过智能技术生成
要看到那个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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值