美文网首页
百度应用地图

百度应用地图

作者: 朕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

    相关文章

      网友评论

          本文标题:百度应用地图

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