登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

查看: 208|回复: 6

iOS中 百度地图详解 韩俊强的博文,王博文

[复制链接]

224

主题

538

帖子

1148

金钱

手工艺人

发表于 2018-1-11 11:45:25 | 显示全部楼层 |阅读模式

        

        需要准备工作按照下图引进类库

        

        需要添加

        

        添加的两个字符串为:NSLocationWhenInUseUsageDescription / NSLocationAlwaysUsageDescription

        

        默认定位设置:

        

        

        设置工作准备完毕上代码:

        指示根视图:

        

[Java] 查看源文件 复制代码
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:23/255.0 green:180/255.0 blue:237/255.0 alpha:1]];
    self.window.rootViewController = [MapViewController new];

        

        MapViewController.m//设置需要的属性

        

[Java] 查看源文件 复制代码
#import MapViewController.h
#import 
#import Mypoint.h
#import 
#interface MapViewController ()
#property (nonatomic, strong) MKMapView *mapView;
//经度
#property (nonatomic, strong) UITextField *longitudetext;
//纬度
#property (nonatomic, strong) UITextField *latitudeText;
//经度
#property (nonatomic, strong) UILabel *longitudeLabel;
//纬度
#property (nonatomic, strong) UILabel *latitudelabel;
//防止标注的button[
#property (nonatomic, strong) UIButton *button;
//地址输入
#property (nonatomic, strong) UITextField *destination;
//输入地址查询地图
#property (nonatomic, retain) UIButton *searchButton;
//可以获取设备当前的经纬度信息
#property (nonatomic, strong) CLLocationManager *locManager;
#end

#implementation MapViewController

        

        


调用:
[Java] 查看源文件 复制代码
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.locManager = [[CLLocationManager alloc]init];
    //代理
    _locManager.delegate = self;
    //定位精度
    _locManager.desiredAccuracy = kCLLocationAccuracyBest;
    //定位频率,10米定位一次
    CLLocationDistance distance = 10.0;
    _locManager.distanceFilter = distance;
    //更新位置
    [_locManager requestAlwaysAuthorization];
    [self.locManager startUpdatingLocation];
    //查询两个地点之间的距离
    [self countDistance];
    //调用布置视图
    [self configureView];
    [self setMapView];
}

        //布置视图

        

[Java] 查看源文件 复制代码
- (void)configureView{
    //经度
    self.longitudeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 40, 30)];
    self.longitudeLabel.text = #经度;
    
    self.longitudetext = [[UITextField alloc]initWithFrame:CGRectMake(40, 20, 120, 30)];
    self.longitudetext.borderStyle = UITextBorderStyleRoundedRect;
    //纬度
    self.latitudelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 40, 30)];
    self.latitudelabel.text = #纬度;
    
    self.latitudeText = [[UITextField alloc]initWithFrame:CGRectMake(40, 50, 120, 30)];
    self.latitudeText.borderStyle = UITextBorderStyleRoundedRect;
    //放置标注按钮
    self.button = [UIButton buttonWithType: (UIButtonTypeSystem)];
    self.button.frame = CGRectMake(30, 73, 100, 30);
    [self.button setTitle:#放置标注 forState: (UIControlStateNormal)];
    [self.button addTarget:self action:#selector(annotationAction: ) forControlEvents: (UIControlEventTouchUpInside)];
    //地址输入
    self.destination = [[UITextField alloc]initWithFrame:CGRectMake(200, 26, 100, 30)];
    self.destination.borderStyle = UITextBorderStyleRoundedRect;
    //查询按钮
    self.searchButton = [UIButton buttonWithType: (UIButtonTypeSystem)];
    self.searchButton.frame = CGRectMake(200, 46, 100, 30);
    [self.searchButton setTitle:#查询 forState: (UIControlStateNormal)];
    [self.searchButton addTarget:self action:#selector(detailSearchAction: ) forControlEvents: (UIControlEventTouchUpInside)];
    
    [self.view addSubview:self.button];
    [self.view addSubview:self.latitudelabel];
    [self.view addSubview:self.longitudeLabel];
    [self.view addSubview:self.longitudetext];
    [self.view addSubview:self.latitudeText];
    [self.view addSubview:self.searchButton];
    [self.view addSubview:self.destination];
}


[Java] 查看源文件 复制代码
- (void)countDistance{
    CLLocation *loc1 = [[CLLocation alloc]initWithLatitude:34 longitude:113];
    CLLocation *loc2 = [[CLLocation alloc]initWithLatitude:35 longitude:113];
    CLLocationDistance distance = [loc1 distanceFromLocation:loc2];
    NSLog(#(%#) 和 (%#)的距离为: %f, loc1, loc2, distance);
}

        

        #pragma mark - CLLocationManagerDelegate Methods

        // 此方法会被频繁调用

[Java] 查看源文件 复制代码
- (void)locationManager: (CLLocationManager *)manager didUpdateLocations: (NSArray *)locations
{
//    NSLog(#didUpdateLocations---%lu, (unsigned long)locations.count);
    // 用来表示某个位置的地理信息, 比如经纬度, 海拔等等
    CLLocation *location = locations.lastObject;
    // location.coordinate.latitude  维度
    // location.coordinate.longitude 经度
    NSLog(#经度 == %f, 维度 == %f, location.coordinate.longitude, location.coordinate.latitude);
    self.longitudetext.text = [NSString stringWithFormat:#%f,location.coordinate.longitude];
    self.latitudeText.text = [NSString stringWithFormat:#%f,location.coordinate.latitude];
    // 停止更新位置(不用定位服务时马上停止, 因为非常耗电)
//    [manager stopUpdatingLocation];
}

        //调出地图

[Java] 查看源文件 复制代码
- (void)setMapView{
    //创建地图视图,初始化参数
    //MKMapTypeStandard 显示街道和道路
    //MKMapTypeSatellite 显示卫星
    //MKMapTypeHybrid 显示混合地图
    self.mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 100, 320, 460)];
    [self.mapView setMapType:MKMapTypeStandard];
    //显示用户当前的坐标,打开地图有相应的提示
    self.mapView.showsUserLocation = YES;
    //设置地图代理
    self.mapView.delegate = self;
    [self.view addSubview:self.mapView];
}

        #pragma mark 根据输入的经纬度确定位置

        //放置标注

[Java] 查看源文件 复制代码
//放置标注
- (void)annotationAction: (UIButton *)sender{
    //创建CLLocation 设置经纬度
    CLLocationCoordinate2D location = CLLocationCoordinate2DMake([[self.latitudeText text]floatValue], [[self.longitudetext text]floatValue] );
    //创建标题
    NSString *title = [NSString stringWithFormat:#%f,%f,location.latitude,location.longitude];
    Mypoint *myPoint = [[Mypoint alloc]initWithCoordinate:location andTitle:title];
    //添加标注
    [self.mapView addAnnotation:myPoint];
    //放大到标注的位置
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 0.01, 0.01);
    [self.mapView setRegion:region];
    [self showLocation];
}

        //根据输入的经纬度显示位置

[mw_shl_code=java,true]
//根据输入的经纬度显示位置
- (void)showLocation{
    //创建CLLocation 设置经纬度
    CLLocationCoordinate2D location = CLLocationCoordinate2DMake([[self.

21

主题

7219

帖子

2762

金钱

手工艺人

发表于 2018-1-11 11:57:35 | 显示全部楼层
感谢分享,code4app有你更精彩

16

主题

7203

帖子

1630

金钱

手工艺人

发表于 2018-1-11 12:06:37 | 显示全部楼层
code4app好的代码demo真的很多,谢谢啦~

21

主题

7187

帖子

2812

金钱

手工艺人

发表于 2018-1-11 12:17:16 | 显示全部楼层
膜拜大神~

0

主题

7164

帖子

-535

金钱

限制会员

发表于 2018-1-11 12:21:52 | 显示全部楼层
支持,感谢,祝code4app越来越好~

23

主题

7171

帖子

1047

金钱

iOS大神

Rank: 6Rank: 6

分享大神

发表于 2018-1-11 12:36:52 | 显示全部楼层
帮帮顶顶!!

15

主题

7167

帖子

1798

金钱

手工艺人

发表于 2018-1-11 12:56:20 | 显示全部楼层
mark,收藏了
*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

关闭

每日头条

通过邮件订阅最新 Code4App 信息
上一条 /4 下一条

广告投放| Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

快速回复 返回顶部 返回列表