美文网首页
百度应用地图

百度应用地图

作者: 朕i | 来源:发表于2018-09-16 20:04 被阅读0次

AppDelegate.M

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

    ProvinceTableViewController *province = [[ProvinceTableViewController alloc] initWithStyle:UITableViewStyleGrouped];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:province

];

province.navigationItem.title = @"省会列表";    

self.window.rootViewController = nav;

    return YES;

}

ProvinceTableViewControlle.M

#import "ProvinceTableViewController.h"

#import "LBSViewController.h"

@interface ProvinceTableViewController ()

{

NSDictionary*_tableDataDic;

}

@end

@implementationProvinceTableViewController

- (void)viewDidLoad

{

    [super viewDidLoad];

    // 路径字符串

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Province" ofType:@"plist"];

    _tableDataDic = [[NSDictionary alloc] initWithContentsOfFile:filePath];

    [self.tableView reloadData];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

    return _tableDataDic.count;

}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

{

    NSArray*provinceArr =_tableDataDic[_tableDataDic.allKeys[section]];

    returnprovinceArr.count;

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

    staticNSString*identifier =@"identifier";

    UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:identifier];

    if(cell ==nil)

    {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

    }

    NSArray* provinceArr =_tableDataDic[_tableDataDic.allKeys[indexPath.section]];

    NSDictionary* provinceDic = provinceArr[indexPath.row];

    cell.textLabel.text= provinceDic[@"province"];

    cell.detailTextLabel.text= provinceDic[@"city"];

    returncell;

}

-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section

{

    return_tableDataDic.allKeys[section];

}

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {

 NSArray* provinceArr =_tableDataDic[_tableDataDic.allKeys[indexPath.section]];

    NSDictionary* provinceDic = provinceArr[indexPath.row];

    NSLog(@"%@%@",provinceDic[@"province"],provinceDic[@"city"]);

    LBSViewController *lbs = [[LBSViewController alloc] init];

    lbs.locationString= [NSStringstringWithFormat:@"%@%@",provinceDic[@"province"],provinceDic[@"city"]];

    lbs.navigationItem.title = lbs.locationString;

    [self.navigationController pushViewController:lbs animated:YES];

}

@end

LBSViewController.M

#import "LBSViewController.h"

#import

#import

@interface LBSViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>

@property(nonatomic,strong)MKMapView *mapView;

@property(nonatomic,strong)CLLocationManager*locManager;  //

@end

@implementationLBSViewController

-(CLLocationManager *)locManager {

    if (!_locManager) {

        _locManager= [[CLLocationManageralloc]init];

        _locManager.activityType = CLActivityTypeFitness;  // 步行导航

        _locManager.delegate=self;

    }

    return _locManager;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.mapView = [[MKMapView alloc] initWithFrame:self.view.frame];

    // 设置地图类型

    self.mapView.mapType = MKMapTypeStandard;  // 默认的平面地图

    self.mapView.delegate=self;

    [self.viewaddSubview:self.mapView];

    // 定位按钮

    UIButton *lbsBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    lbsBtn.frame=CGRectMake(20,self.view.frame.size.height-100,50,50);

    [lbsBtnsetTitle:@"定位" forState:UIControlStateNormal];

    [lbsBtnsetBackgroundColor:[UIColor blueColor]];

    [lbsBtnaddTarget:self action:@selector(getCurrentLoation:) forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:lbsBtn];

    // 获取省会位置

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(getProvinceLocation:)];

    // 申请用户授权

    [self.locManager requestAlwaysAuthorization];  // 申请用户授权

}

#pragma mark ------- CLLocationManagerDelegate --------

-(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray *)locations {

    // 获取当前位置

    CLLocation*curLocation = locations.lastObject;

    // 位置对象,此对象已经采用了MKAnnotation协议

    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];

    // 设置显示位置经纬度

    point.coordinate= curLocation.coordinate;

    //

    point.title=@"当前位置";

    // 地址反向解析

    CLGeocoder*geocoder = [[CLGeocoderalloc]init];

    [geocoderreverseGeocodeLocation:curLocationcompletionHandler:^(NSArray *_Nullableplacemarks,NSError*_Nullableerror) {

        CLPlacemark*place = placemarks.lastObject;

        point.subtitle= place.name;

    }];

    // 添加大头针

    [self.mapViewaddAnnotation:point];

    // 将显示区域缩小

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(curLocation.coordinate, 800, 800);

    [self.mapViewsetRegion:regionanimated:YES];

}

#pragma mark ----- MKMapViewDelegate -----

// 设置锚点样式

-(MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id)annotation {

    staticNSString*identifier =@"pin";

    MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if(pin ==nil) {

        // 做一个大头针

        pin = [[MKPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:identifier];

    }

    pin.pinTintColor = [MKPinAnnotationView purplePinColor];

    pin.animatesDrop = YES;

    pin.canShowCallout = YES;

    returnpin;

}

#pragma mark ---------------- UI触发事件 -------------------

// 获取省会位置

-(void)getProvinceLocation:(id)sender {

    // 传递过来的省会的字符串做一个地址解析

    CLGeocoder *g = [[CLGeocoder alloc] init];

    [ggeocodeAddressString:self.locationStringcompletionHandler:^(NSArray *_Nullableplacemarks,NSError*_Nullableerror) {

        CLPlacemark*place = placemarks.lastObject;

        CLLocationCoordinate2D coor = place.location.coordinate;

        dispatch_async(dispatch_get_main_queue(), ^{

            // 位置对象,此对象已经采用了MKAnnotation协议

            MKPointAnnotation *point = [[MKPointAnnotation alloc] init];

            // 设置显示位置经纬度

            point.coordinate= coor;

            [self.mapViewaddAnnotation:point];

            // 将显示区域缩小

            MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coor, 800, 800);

            [self.mapViewsetRegion:regionanimated:YES];

        });

    }];

}

// 获取当前位置

-(void)getCurrentLoation:(id)sender {

    //

    NSLog(@"获取当前位置");

    [self.locManager startUpdatingLocation];  // 开始更新位置信息

}

Privacy - Location Always and When In Use Usage Description

Privacy - Location Always Usage Description

Privacy - Location Usage Description

Privacy - Location When In Use Usage Description

相关文章

  • 百度地图调用api

    1.注册百度地图用户 2.百度地图中创建应用,获取ak值 3.调用 (1)调用百度地图api 在应用中填写自己获取...

  • 0825_基于lbs的服务应用开发

    新内容(基于lbs的服务应用开发) lbs公开接口: 百度地图 腾讯地图 Google地图 百度地图接口简单使用 ...

  • 39.iOS百度地图(手把手教)

    一.进入百度地图官方(注册应用) 1.1.百度地图官方 1.2.创建应用FBABCE88-01DC-4183-A8...

  • 地图

    地图分类 百度 腾讯 谷歌 苹果本身的地图 百度地图 申请密钥 在百度开发者中心添加应用 在终端里输入pod se...

  • 在 HTTPS 项目中使用百度地图 API

    百度地图 API 产品简介 百度地图 JavaScript API 是一套由 JavaScript 语言编写的应用...

  • 百度地图集成_集成SDK和基本功能实现

    1.百度地图导入项目中。。。。 1.注册百度地图,成为其开发者 注册百度地图开发者 2.创建应用:申请获得密匙 创...

  • iOS 跳转 百度地图 高德地图 自带地图

    在做地图跳转操作之前请先在plist文件添加相应的配置参数 App跳转百度地图应用 百度地图文档路线规划 2.2....

  • 百度地图API之麻点图

    关于百度地图批量上传POI数据并在浏览器显示 step1:百度地图开发者身份,创建应用(应用类别是服务端,不是浏览...

  • 百度地图的集成(转载)

    如何快速集成百度地图: 注册百度开发者帐号=》创建应用=》下载SDK=》集成开发=》测试应用=》发布应用 1、注册...

  • 百度地图的集成

    在集成百度地图的时候,一定要记得将Bundle Identifier 与百度地图中我的应用里的安全码一致,否则地图...

网友评论

      本文标题:百度应用地图

      本文链接:https://www.haomeiwen.com/subject/itvfnftx.html