iOS实现简单的地图

1、创建一个 MKMapView 的类,然后把他以一个子视图的形式添加到你的控制器文件中。 如下是一个视图控制器创建了一个 MKMapView 并全屏显示的.h 文件例子。 

#import
#import
@interface Creating_a_Map_ViewViewController : UIViewController 

@property (nonatomic, strong) MKMapView *myMapView;
@end

这个是一个有 MKMapkit 类型变量的根视图控制器的例子,然后在视图控制器的实现文(.m 文件),我们将初始化地图并设置它的类型为卫星模式,如下。

 #import "Creating_a_Map_ViewViewController.h"

@implementation Creating_a_Map_ViewViewController 

- (void)didReceiveMemoryWarning{
[
super didReceiveMemoryWarning];
}

- (void)viewDidLoad{
[
super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor]; 

self.myMapView = [[MKMapView alloc] initWithFrame:self.view.bounds];

 

self.myMapView.mapType = MKMapTypeSatellite

UIViewAutoresizingFlexibleHeight;

[self.view addSubview:self.myMapView];

}

我们可以通过 MKMapView  mapType 属性来切换地图的展现形式。有如下的属性值可 用:

MKMapTypeStandard

用这个类型来显示普通地图(这个是默认的)。

MKMapTypeSatellite

用这个来类型来显示卫星云图

MKMapTypeHybrid

用这个类型来显示普通地图覆盖于卫星云图之上,这个地图的展现形式属于复合形式。 

2、处理 Map 视图上的事件

将遵循 MKMapViewDelegate 协议的 delegate 对象赋值给 MKMapView 实例对象的 delegate 属性。

self.myMapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
 

self.myMapView.mapType = MKMapTypeSatellite;

self.myMapView.delegate = self

self.myMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[self.view addSubview:self.myMapView]; 

把上面这段代码添加到一个视图控制器的 viewDidLoad 方法里面,很容易就能运行。 不过前提是你需要在头文件中添加类似如下的内容

#import
#import
@interface Handling_the_Events_of_a_Map_ViewViewController : UIViewController <</span>MKMapViewDelegate>

@property (nonatomic, strong) MKMapView *myMapView; @end 

3、精确定位设备的位置

 你想要获得设备的经纬度,就得使用 CLLocationManager 这个类。

if ([CLLocationManager locationServicesEnabled])

self.myLocationManager = [[CLLocationManager alloc] init]; 

self.myLocationManager.delegate = self;

self.myLocationManager.purpose =

@"To provide functionality based on user's current location."

[self.myLocationManager startUpdatingLocation];
}
 else {

NSLog(@"Location services are not enabled");

 在上面这段代码中,myLocationManager  CLLocationManager 的一个属性,当前这个 类也是LocationManager 的一个 delegate

 任何实现 CLLocationManager 类的协议实现者都必须要遵循 CLLocationManagerDelegate 这个协议中的方法。

 下面的代码是如何在 view controller 头文件中声明一个 CLLocationManager 对象(CLLocationManager 对象的创建不一定要在 view controller )

 import

#import
@interface Pinpointing_the_Location_of_a_DeviceViewController
: UIViewController <</span>CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *myLocationManager;

@end

View controller 的实现代码如下: 


#import "Pinpointing_the_Location_of_a_DeviceViewController.h"

@implementation Pinpointing_the_Location_of_a_DeviceViewController 

- (void)didReceiveMemoryWarning

{
[
super didReceiveMemoryWarning];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocationfromLocation:(CLLocation *)oldLocation{

NSLog(@"Latitude = %f", newLocation.coordinate.latitude); 

NSLog(@"Longitude = %f", newLocation.coordinate.longitude);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

}
- (void)viewDidLoad {
[
super viewDidLoad]; 

if ([CLLocationManager locationServicesEnabled])

{ self.myLocationManager = [[CLLocationManager alloc] init];

self.myLocationManager.delegate = self

self.myLocationManager.purpose =
@"To provide functionality based on user's current location."

[self.myLocationManager startUpdatingLocation];
}
 else {

NSLog(@"Location services are not enabled");
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation :(UIInterfaceOrientation)interfaceOrientation{

return YES;

}

@end 

 如 上 代 码 , CLLocationManager  startUpdateLocation 方 法 通 过 它 的 代 理didUpdateToLocation:fromLocation:  locationManager:didFailWithError:方法来报告用户定位 成功或失败。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值