首先要说的是,升级系统Ios 8之后,定位功能进行了升级,开发者在使用定位功能之前,需要在info.plist文件中添加
(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):
NSLocationWhenInUseUsageDescription ,允许在前台使用时获取GPS的描述
NSLocationAlwaysUsageDescription ,允许永久使用GPS的描述)
在使用导入百度地图之后,你需要创建_mapManager全局变量,并通过实现如下代码来启动百度地图引擎或者说激活你的秘钥
<span style="font-size:14px;">-(void)BMKMapManager
{
//---------------百度地图相关-----------
_mapManager = [[BMKMapManager alloc]init];
// 如果要关注网络及授权验证事件,请设定 generalDelegate参数
BOOL ret = [_mapManager start:@"3kG0Qh4SEMPW5oZ77Lm59P5U" generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
}
}</span>
百度地图的定位功能实际是对iOS系统自带定位系统的封装,所以我在使用定位功能时,直接使用了本身的定位方法
首先创建locationManager对象
<span style="font-size:14px;">- (void)locationManager
{
//----------------------------------------------------------定位获取经纬度--------------------------------------------------------
//定位服务管理对象初始化
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 1.f;// 设定最少移动1000米才能刷新
if(iOS8){
[_locationManager requestWhenInUseAuthorization];
}
[_locationManager startUpdatingLocation];
}</span>
值得一提的是,此处的locationManager一定是一个全局变量,否则不能成功定位。
创建之后,要在定位成功之后的代理方法中获取当前位置,代理方法如下:
<span style="font-size:14px;">- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
if(_isTimeLacation == NO){
_isTimeLacation =YES;
CLLocation * currentLocation = [locations lastObject];
NSDictionary * dict1 = BMKConvertBaiduCoorFrom(currentLocation.coordinate, BMK_COORDTYPE_GPS);
_BDCoord = CLLocationCoordinate2DMake(BMKCoorDictionaryDecode(dict1).latitude, BMKCoorDictionaryDecode(dict1).longitude);
NSString * requestForLocation = [NSString stringWithFormat:@"http://api.map.baidu.com/geocoder/v2/?ak=uSEZstk7wzW10kwQbEeHKMpP&callback=renderReverse&location=%@,%@&output=xml&pois=1",[NSString stringWithFormat:@"%f",_BDCoord.latitude],[NSString stringWithFormat:@"%f",_BDCoord.longitude]];
// NSLog(@"url = %@",requestForLocation);
AFHTTPRequestOperationManager * netWorkRequestManager = [[AFHTTPRequestOperationManager alloc] init];
netWorkRequestManager.responseSerializer = [AFXMLParserResponseSerializer serializer];
[netWorkRequestManager GET:requestForLocation parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSXMLParser * xml = responseObject;
xml.delegate = self;
[xml parse];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//请求失败返回经纬度
CGRect noNetworkRect = CGRectMake(([UtilityHelper applicationFrame].size.width-250)/2, 300, 250, 30);
[UIToastView showToastViewWithContent:[NSString stringWithFormat:@"当前位置:(%f,%f)",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude] andRect:noNetworkRect andTime:2.0 andObject:
[UtilityHelper applicationDelegate].window.rootViewController];
_currentCity=[NSString stringWithFormat:@"%f,%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude];
}];
}else{
return;
}
}</span>
以上代理需要强调两点。首先,获取了currentLocation之后,你便可以通过currentLocation.coordinate.latitude,currentLocation.coordinate.longtitude来获取你当前所处位置的经纬度。其次,由于这个经纬度是iOS自带定位功能获取的,也就是说,这是基于高德地图获取的经纬度,因此,如果你的应用需要用百度地图,此时你还需要将此坐标转换为百度地图的坐标。百度地图API已经提供了坐标转换的方法可以直接使用。如上_BDCoor便是转换之后的坐标点。
大多数情况下,我们定位之后并不会直接显示给用户他当前所处位置的坐标点,而是显示用户此时所在位置。那个这个位置如何获取呢。百度提供了Geocoding API,可以将地理位置信息转换为坐标点,也可以将坐标点转换为具体地理位置的。只要传给接口坐标点,就可以将你获得的地理位置进行解析,得到你要的信息。
接口:http://api.map.baidu.com/geocoder/v2/?ak=秘钥&callback=renderReverse&location=%@,%@&output=xml&pois=1
<span style="font-size:14px;"><GeocoderSearchResponse><status>0</status><result><location><lat>39.827590097642</lat><lng>116.30138000173</lng></location><formatted_address>北京市丰台区丰茂南路</formatted_address><business>花乡,看丹桥</business><addressComponent><streetNumber/><street>丰茂南路</street><district>丰台区</district><city>北京市</city><province>北京市</province><direction/><distance/></addressComponent><cityCode>131</cityCode><pois><poi><addr>北京市丰台区六圈路</addr><distance>30</distance><name>中国指数研究院</name><poiType>教育</poiType><tel/><zip/><point><x>116.30126079659</x><y>39.827396155606</y></point><direction>附近</direction><distance>30</distance></poi></span>
<pre name="code" class="objc"><span style="font-size:14px;">- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
newAnnotationView.pinColor = BMKPinAnnotationColorRed;
newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
if([annotation title] != nil){
SLProjectInfoView * projectInfo = [[SLProjectInfoView alloc] initWithFrame:CGRectMake(0, 0, MAINWIDTH-50, 120) projectParcel:[_dataListArray objectAtIndex:[[annotation title] integerValue]]];
BMKActionPaopaoView * paopao = [[BMKActionPaopaoView alloc] initWithCustomView:projectInfo];
newAnnotationView.tag = [[annotation title] integerValue];
newAnnotationView.paopaoView = paopao;
}
newAnnotationView.image = [UIImage imageNamed:@"map_zuobiao@2x.png"];
return newAnnotationView;
}
return nil;
}</span>
该方法主要用来实现自定义大头针样式,每一个大头针的创建都会调用此方法。
大头针的点击事件
<span style="font-size:14px;">- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view;
{
NSLog(@"paopaoclick");
}</span>
地图空白处的点击方法
<span style="font-size:14px;">- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate
{
[_infoView removeFromSuperview];
_infoView = nil;
self.tabBarController.tabBar.hidden = NO;
}</span>
注:LBS包含两层含义,[1]确定移动设备或用户所在的位置。[2]提供与位置信息相关的各类信息服务。
持续更新中。。。