码迷,mamicode.com
首页 > 移动开发 > 详细

iOS第三方地图-百度地图中心点定位

时间:2015-09-05 19:19:00      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

使用百度地图定位后,滑动地图,使用反编码确定地图中心店的位置信息

//
//  MapControl.m
//  quyizu
//
//  Created by apple on 15/9/2.
//  Copyright (c) 2015年 waste. All rights reserved.
//

//使用百度地图定位,poi搜索,地理编码功能
#import "MapControl.h"
#import "WJBaiduMapTools.h"
@interface MapControl ()<BMKPoiSearchDelegate,BMKMapViewDelegate,BMKGeoCodeSearchDelegate,BMKLocationServiceDelegate>{
    BMKMapView *_mapView;                //地图
    BMKPoiSearch *_poisearch;            //poi搜索
    BMKGeoCodeSearch  *_geocodesearch;   //geo搜索服务
    CGFloat _longitude;     //经度
    CGFloat _latitude;      //纬度
    UILabel *_labelShow;     //信息显示
    NSInteger _index;       //判断次数
    BMKLocationService* _locService;
    UIImageView *_viewCenterBG;
}

@end

@implementation MapControl

- (instancetype)init
{
    self = [super init];
    if (self) {
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemPressed)];
        self.navigationItem.rightBarButtonItem = rightItem;
    }
    return self;
}

- (void)rightItemPressed {
    [self.navigationController popViewControllerAnimated:YES];
    if ([_delegate respondsToSelector:@selector(mapControlDelegate:isSchool:)]) {
        [_delegate mapControlDelegate:_labelShow.text isSchool:_isSchool];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
     _mapView.delegate = self;
    _geocodesearch.delegate = self;
    _locService.delegate = self;
}
-(void)viewWillDisappear:(BOOL)animated
{
    _mapView.delegate = nil; // 不用时,置nil
    _poisearch.delegate = nil; // 不用时,置nil
    _geocodesearch.delegate = nil;
    _locService.delegate = nil;
}

- (void)dealloc {
    if (_geocodesearch != nil) {
        _geocodesearch = nil;
    }
    if (_mapView) {
        _mapView = nil;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (_isSchool) {
        [self addnavigationTitle:@"上学方便"];
    }else {
        [self addnavigationTitle:@"工作方便"];
    }
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [_mapView setZoomLevel:17];
    [self.view addSubview:_mapView];

    _geocodesearch = [[BMKGeoCodeSearch alloc]init];
    _locService = [[BMKLocationService alloc]init];
    [_locService startUserLocationService];
    [self initCenterView];
}

- (void)initCenterView {
    UIView *viewBG = [[UIView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, 60)];
    [self.view addSubview:viewBG];
    
    UIView *viewBGalpha = [[UIView alloc]initWithFrame:viewBG.bounds];
    viewBGalpha.backgroundColor = [UIColor blackColor];
    viewBGalpha.alpha = 0.4;
    [viewBG addSubview:viewBGalpha];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, ScreenWidth-30, 60)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"地图显示";
    label.numberOfLines = 2;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor whiteColor];
    [viewBG addSubview:label];
    _labelShow = label;
    
    UIImageView *viewCenterBG = [[UIImageView alloc]init];
    viewCenterBG.image = [UIImage imageNamed:@"map_iconBG"];
    viewCenterBG.center = CGPointMake(ScreenWidth/2, ScreenHeight/2);
    viewCenterBG.bounds = CGRectMake(0, 0, 50, 50);
    viewCenterBG.alpha = 0.5;
    _viewCenterBG= viewCenterBG;
    [self.view addSubview:viewCenterBG];
    
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerPressed) userInfo:nil repeats:YES];
    
    UIView *view = [[UIView alloc]init];
    view.center = CGPointMake(ScreenWidth/2, ScreenHeight/2);
    view.bounds = CGRectMake(0, 0, 15, 15);
    view.layer.cornerRadius = 7.5;
    view.layer.borderWidth = 1;
    view.layer.borderColor = [UIColor whiteColor].CGColor;
    view.layer.masksToBounds = YES;
    view.backgroundColor = [UIColor colorWithHexString:@"69c9fa"];
    [self.view addSubview:view];
}

- (void)timerPressed {
    [UIView animateWithDuration:2 animations:^{
        _viewCenterBG.bounds = CGRectMake(0, 0, 100, 100);
        _viewCenterBG.alpha = 0.1;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:2 animations:^{
            _viewCenterBG.bounds = CGRectMake(0, 0, 50, 50);
            _viewCenterBG.alpha = 0.5;
        }];
    }];
}

#pragma mark - 根据经纬度搜索

- (void)reverseGeoPointSearchlongitude:(CGFloat )longitude latitude:(CGFloat)latitude{
    
    CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};
        pt = (CLLocationCoordinate2D){latitude, longitude};
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    reverseGeocodeSearchOption.reverseGeoPoint = pt;
    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
    if(flag)
    {
        NSLog(@"反geo检索发送成功");
    }
    else
    {
        NSLog(@"反geo检索发送失败");
    }
}

#pragma mark - BMKMapViewDelegate


//地图改变完成调用这个接口
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    
    if (_longitude == mapView.centerCoordinate.longitude &&_latitude == mapView.centerCoordinate.latitude) {
        
    }else {
        if (_index !=0) {
            [self reverseGeoPointSearchlongitude:mapView.centerCoordinate.longitude latitude:mapView.centerCoordinate.latitude];
            _longitude = mapView.centerCoordinate.longitude;
            _latitude = mapView.centerCoordinate.latitude;
        }
    }
    NSLog(@"%f,%f",mapView.centerCoordinate.longitude,mapView.centerCoordinate.latitude);
}

#pragma mark - BMKGeoCodeSearchDelegate

// *返回反地理编码搜索结果
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {
    if (error == 0) {
        BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
        item.coordinate = result.location;
        item.title = result.address;
        if (_index == 0) {
            _mapView.centerCoordinate = result.location;
            _index ++;
        }
        NSString* titleStr;
        NSString* showmeg;
        titleStr = @"反向地理编码";
        showmeg = [NSString stringWithFormat:@"%@",item.title];
        _labelShow.text = showmeg;
    }
}
#pragma mark - BMKLocationServiceDelegate

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
    [_locService stopUserLocationService];
    [self reverseGeoPointSearchlongitude:userLocation.location.coordinate.longitude latitude:userLocation.location.coordinate.latitude];
}

@end

技术分享

iOS第三方地图-百度地图中心点定位

标签:

原文地址:http://www.cnblogs.com/hxwj/p/4783596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!