//
// 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(nonatomic, strong)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
本文介绍如何利用BMapKit在iOS应用中实现地图功能,包括定位、添加标记点及反向地理编码等关键操作。

834

被折叠的 条评论
为什么被折叠?



