ios百度地图选择地址返回经纬度

//

//  MapViewController.h

//  MusicLove

//

//  Created by niuxinghua on 15/1/30.

//  Copyright (c) 2015 Hjojo. All rights reserved.

//


#import <UIKit/UIKit.h>

#import "BMapKit.h"

#import "HJPagerViewController.h"

@interface MapViewController : UIViewController<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>

@property(nonatomicstrong)BMKLocationService* localService;

@property(nonatomic,strong)BMKMapView* _mapView;

@property(nonatomic,strong) BMKLocationService* _locService;

@property(nonatomic,strong)BMKPointAnnotation* item;

@property(nonatomic,assign)float latitude;

@property(nonatomic,assign)float longitude;

@property(nonatomic,assign)CLLocationCoordinate2D coor;

@property(nonatomic)id<locatebackdelegete> locatedelegate;

@end

//

//  MapViewController.m

//  MusicLove

//

//  Created by niuxinghua on 15/1/30.

//  Copyright (c) 2015 Hjojo. All rights reserved.

//


#import "MapViewController.h"

#import "AFNetworking.h"

@interface MapViewController ()


@end


@implementation MapViewController

-(void)viewWillAppear:(BOOL)animated{

    [__mapView viewWillAppear];

   }

- (void)viewDidLoad {

    [super viewDidLoad];

    self.tabBarController.hidesBottomBarWhenPushed=YES;

    __locService = [[BMKLocationService alloc]init];

    __mapView=[[BMKMapView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];

    [__locService startUserLocationService];

    __mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放

    __locService.delegate = self;

    __mapView.showsUserLocation = NO;//先关闭显示的定位图层

    __mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态

    __mapView.showsUserLocation = YES;//显示定位图层

    NSLog(@"%@",__mapView);

    [self.view addSubview:__mapView];

    self.item = [[BMKPointAnnotation alloc]init];


}

/**

 

 *点中底图空白处会回调此接口

 

 *@param mapview 地图View

 

 *@param coordinate 空白处坐标点的经纬度

 

 */

- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate


{

    

    NSLog(@"onClickedMapBlank-latitude==%f,longitude==%f",coordinate.latitude,coordinate.longitude);

    _latitude=coordinate.latitude;

    _longitude=coordinate.longitude;

    _coor=coordinate;

//    NSString* showmeg = [NSString stringWithFormat:@"您点击了地图空白处(blank click).\r\n当前经度:%f,当前纬度:%f,\r\nZoomLevel=%d;RotateAngle=%d;OverlookAngle=%d", coordinate.longitude,coordinate.latitude,(int)_mapView.zoomLevel,_mapView.rotation,_mapView.overlooking];

    [self addPointAnnotation];

//    

    

}

- (void)mapView:(BMKMapView *)mapView onClickedMapPoi:(BMKMapPoi*)mapPoi{

    NSLog(@"%@",mapPoi.text);

    [self.locatedelegate locateback:mapPoi.text];

    //mapPoi.pt.latitude

    [self.navigationController popViewControllerAnimated:YES];


    

}


- (void)selectAnnotation:(id <BMKAnnotation>)annotation animated:(BOOL)animated{

    

    BMKGeoCodeSearch *geocodesearch = [[BMKGeoCodeSearch alloc]init];

    geocodesearch.delegate = self;

    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];

    reverseGeocodeSearchOption.reverseGeoPoint = annotation.coordinate;

    BOOL flag = [geocodesearch reverseGeoCode:reverseGeocodeSearchOption];

    if(flag)

    {

        NSLog(@"geo检索发送成功");

    }

    else

    {

        NSLog(@"geo检索发送失败");

    }


    

}

- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view{

    NSLog(@"确定了");

//    NSString* url=@"http://api.map.baidu.com/geocoder/v2/";

//    NSString*  urlstr=[NSString stringWithFormat:@"http://api.map.baidu.com/geocoder/v2/ak=1fbxfN3rtbqyiuGB2to51fNg&callback=renderReverse&location=%f,%f&output=json&pois=1",_latitude,_longitude];

//    NSLog(@"%@",urlstr);

//    NSString* locate=[NSString stringWithFormat:@"%f,%f",_latitude,_longitude];

//    NSDictionary *parameter=@{@"ak": @"1fbxfN3rtbqyiuGB2to51fNg",@"callback": @"renderReverse",@"location":locate,@"output":@"json",@"pois":@"1"};

//     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

//    [manager GET:url parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {

//                NSLog(@"%@", responseObject);

//                // 提问:NSURLConnection异步方法回调,是在子线程

//        //        // 得到回调之后,通常更新UI,是在主线程

//        //        NSLog(@"%@", [NSThread currentThread]);

//        

//                NSLog(@".......%@",responseObject);

//            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

//                NSLog(@"%@", error);

//            }];

    BMKGeoCodeSearch *geocodesearch = [[BMKGeoCodeSearch alloc]init];

    geocodesearch.delegate = self;

    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];

    reverseGeocodeSearchOption.reverseGeoPoint = [view.annotation coordinate];

    BOOL flag = [geocodesearch reverseGeoCode:reverseGeocodeSearchOption];

    if(flag)

    {

        NSLog(@"geo检索发送成功");

    }

    else

    {

        NSLog(@"geo检索发送失败");

    }


    

}

- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{

    NSLog(@"%@....",result.address);

    [self.locatedelegate locateback:result.address];

    [self.navigationController popViewControllerAnimated:YES];

}





//添加标注


- (void)addPointAnnotation


{

    _coor.latitude = _latitude;

    

    _coor.longitude = _longitude;

    

    _item.coordinate = _coor;

    

    _item.title = @"选择了此处";

    

    [__mapView addAnnotation:_item];

    

}

-(void)viewWillDisappear:(BOOL)animated{

    [__mapView viewWillDisappear];

    [__locService stopUserLocationService];

    __mapView.delegate = nil; // 不用时,置nil

    __locService.delegate = nil;

}

/**

 *在地图View将要启动定位时,会调用此函数

 *@param mapView 地图View

 */

- (void)willStartLocatingUser

{

    NSLog(@"start locate");

    __mapView.showsUserLocation = YES;//显示定位图层

}


/**

 *用户方向更新后,会调用此函数

 *@param userLocation 新的用户位置

 */

- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation

{

   // [__mapView updateLocationData:userLocation];

   // NSLog(@"heading is %@",userLocation.heading);

}


/**

 *用户位置更新后,会调用此函数

 *@param userLocation 新的用户位置

 */

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation

{

    //    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

    [__mapView updateLocationData:userLocation];

}


/**

 *在地图View停止定位后,会调用此函数

 *@param mapView 地图View

 */

- (void)didStopLocatingUser

{

    NSLog(@"stop locate");

}


/**

 *定位失败后,会调用此函数

 *@param mapView 地图View

 *@param error 错误号,参考CLError.h中定义的错误号

 */

- (void)didFailToLocateUserWithError:(NSError *)error

{

    NSLog(@"%@",error);

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值