ios菜鸟之路:iphone google map 定位的实现
来源:互联网 发布:wincc 数据库远程连接 编辑:程序博客网 时间:2024/06/06 04:23
一、导入MapAnnotations类。
在工程的build phases中的link binary with libraryies中点击+号添加MapAnnotations类。
二、头文件中加载类以及协议
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "MapAnnotations.h"
@interface panyongViewController : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate>
- (IBAction)getphoto:(id)sender;
@property (nonatomic,strong)IBOutlet MKMapView *map;
@property (strong, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@property (nonatomic,strong)IBOutlet CLLocationManager *locManager;
@property(nonatomic) CLLocationCoordinate2D loc;
@property(nonatomic,strong) IBOutlet MapAnnotations *mapAnnotations;
-(void)setCurrentLocation:(CLLocation *)location;
- (IBAction)segmentChange:(id)sender;
@end
三、定位具体实现
//在viewdidload中添加map及locmanager的初始化
map=[[MKMapView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 410.0f)];
map.showsUserLocation=YES;
[self.view addSubview:map];
locManager=[[CLLocationManager alloc]init];
locManager.delegate=self;
locManager.desiredAccuracy=kCLLocationAccuracyBest;
locManager.distanceFilter=10.0f;
[locManager startUpdatingHeading];
mapAnnotations=[[MapAnnotations alloc]initWithCoordinate:loc];
mapAnnotations.title=@"test";
mapAnnotations.subtitle=@"have a try";
[map addAnnotation:mapAnnotations];
//三种地图格式的转换
- (IBAction)segmentChange:(id)sender {
if (segmentControl.selectedSegmentIndex==0) {
map.mapType=MKMapTypeStandard;
}
if (segmentControl.selectedSegmentIndex==1) {
map.mapType=MKMapTypeHybrid;
}
if (segmentControl.selectedSegmentIndex==2) {
map.mapType=MKMapTypeSatellite;
}
}
#pragma mark-
#pragma mark Core Location Delegate Methods
//地图定位的实现
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
loc=[newLocation coordinate];
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.1;
span.longitudeDelta=0.1;
region.span=span;
region.center=loc;
map.mapType=MKMapTypeStandard;
[map setRegion:region animated:YES];
[map regionThatFits:region];
UILabel *label1=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
UILabel *label2=[[UILabel alloc]initWithFrame:CGRectMake(0, 15, 10, 10)];
label1.text=[[NSString alloc]initWithFormat:@"%f",span.latitudeDelta];
label2.text=[[NSString alloc]initWithFormat:@"%f",span.longitudeDelta];
[map addSubview: label1];
[map addSubview: label2];
}
//用户拒绝使用地图服务
-(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, nil];
[alert show];
}
}
//更新用户定位地址
-(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];
}
- ios菜鸟之路:iphone google map 定位的实现
- [iOS菜鸟的学习之路]定位功能的实现
- ios菜鸟之路:iphone开发中UIAlertView开发,及多按键的实现
- ios菜鸟之路:iphone连接webservice服务操作
- iOS学习之Map,定位,标记位置的使用
- IOS学习之Map,定位,标记位置的使用
- iOS学习之Map,定位,标记位置的使用
- iOS学习之Map,定位,标记位置的使用
- iOS学习之Map,定位,标记位置的使用
- iOS学习之Map,定位,标记位置的使用
- iOS学习之Map,定位,标记位置的使用
- iOS学习之Map,定位,标记位置的使用
- iOS学习之Map,定位,标记位置的使用
- iOS学习之Map,定位,标记位置的使用
- iOS学习之Map,定位,标记位置的使用
- iOS学习之Map,定位,标记位置的使用
- iOS学习之Map,定位,标记位置的使用
- iOS学习之Map,定位,标记位置的使用
- Matlab与Access数据库编程指南
- Opencv cvFindContours 函数的详细说明
- 软件行业的 80-20 理论
- PDF文档的矢量图提取详解
- IOS URL带中文报错解决方法
- ios菜鸟之路:iphone google map 定位的实现
- 大小端模式
- 即刻提升jQuery性能的十个技巧
- HTML5 跨域信息交互技术之 postMessage
- BroadcastReceiver入门
- Mysql5.5 安装在Cent os5.6上
- 如何学习HTML5?
- java.lang.ClassCastException: android.app.Application cannot be cast to greendroid.app.MyApplication
- 五行代码终极完美解决从IE6到Chrome所有浏览器的position:fixed;以及闪动问题