百度地图

流程:http://lbsyun.baidu.com/index.php?title=iossdk/guide/hellobaidumap

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    _mapView = [[BMKMapView alloc] init];
    _mapView.frame = self.view.bounds;
    _mapView.delegate = self;
    [self.view addSubview:_mapView];
    
  //地图设置
//    _mapView.mapType = BMKMapTypeSatellite;  //卫星地图
//    _mapView.trafficEnabled = YES;//路况信息
//    _mapView.baiduHeatMapEnabled = YES;//(热力图)哪个地方人多 哪个地方人少
//    _mapView.buildingsEnabled = YES;//3D楼宇
    //俯视角度(-45~0)
    _mapView.overlooking = -30;
    // 地图旋转角度,在手机上当前可使用的范围为-180~180度
//    _mapView.rotation = 90;
    
    //地图缩放(3~21,值越大缩放越明显)
//    _mapView.zoomLevel = 21;
    //是否支持缩放
//    _mapView.zoomEnabled = YES;
    //是否支持移动
//    _mapView.scrollEnabled = NO;
    //是否关闭所有手势
//    _mapView.gesturesEnabled = NO;
    //设置预留
    _mapView.mapPadding = UIEdgeInsetsMake(0, 0, 28, 0);
    
    //指南针位置
    _mapView.compassPosition = CGPointMake(10, 100);
    
    //设置比例尺的位置和开关
    _mapView.showMapScaleBar = YES;
    _mapView.mapScaleBarPosition = CGPointMake(_mapView.frame.size.width-70, _mapView.frame.size.height - 68);
    

    //地图定位
    [self locationService];
}

 

//地图定位
- (void)locationService
{
    //地图定位
    _locationService = [[BMKLocationService alloc] init];
    _locationService.delegate = self;
    //开启定位服务
    [_locationService startUserLocationService];
    //设置定位状态
    _mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading;
    //显示用户位置
    _mapView.showsUserLocation = YES;
    
    
    //标注点(纬度,经度)
    BMKPointAnnotation *pointAnnotation = [[BMKPointAnnotation alloc] init];
    CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(40.0431260000,116.4770600000);
    // 标注点坐标
    pointAnnotation.coordinate = coor;
    // 设置标题和副标题
    pointAnnotation.title = @"标注点1";
    pointAnnotation.subtitle = @"奥林匹克森林公园";
    [_mapView addAnnotation:pointAnnotation];

    
    
    //
    BMKCoordinateRegion regin;
    regin.center = coor;
    BMKCoordinateSpan span;
    span.latitudeDelta = 0.01;// 值越精细地图显示越大
    span.longitudeDelta = 0.01;
    regin.span = span;
    //显示范围
    _mapView.region = regin;
    
    
    
    //长按拖拽
    UILongPressGestureRecognizer *lp = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lpClick:)];
    [_mapView addGestureRecognizer:lp];
 
    
}

 

#pragma mark - 标注新点 -
- (void)lpClick:(UILongPressGestureRecognizer *)lp
{
    if(lp.state == UIGestureRecognizerStateBegan)
    {
        // 标注点
        BMKPointAnnotation *pointAnnotation = [[BMKPointAnnotation alloc] init];
        // 获取手指点击的坐标
        CGPoint point = [lp locationInView:lp.view];
        // 将坐标转化为_mapView中的经纬度
        CLLocationCoordinate2D coor = [_mapView convertPoint:point toCoordinateFromView:_mapView];
        // 标注点坐标
        pointAnnotation.coordinate = coor;
        // 设置标题和副标题
        pointAnnotation.title = @"新标注点";
        pointAnnotation.subtitle = @"新标注点副标题";
        [_mapView addAnnotation:pointAnnotation];
    }
}

 

#pragma mark - 覆盖物 -
- (void)fugaiwu
{
    // 覆盖物(折线、圆形、弧形等)
    CLLocationCoordinate2D  coords[3] = {0};
    coords[0].latitude = 39.9374;
    coords[0].longitude = 116.350;
    coords[1].latitude = 39.9170;
    coords[1].longitude = 116.360;
    coords[2].latitude = 39.9479;
    coords[2].longitude = 116.373;
    //线
    //    BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coords count:3];
    //    [_mapView addOverlay:polyline];
    //弧线
    BMKArcline *arcline = [BMKArcline arclineWithCoordinates:coords];
    [_mapView addOverlay:arcline];

}

// 覆盖物协议方法
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay
{
    //判断是否是折线
    if ([overlay isKindOfClass:[BMKPolyline class]])
    {
        BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
        polylineView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];
        polylineView.lineWidth = 5.0;
        return polylineView;
    }
    else if ([overlay isKindOfClass:[BMKArcline class]])
    {
        // 判断是否是弧线
        BMKArclineView* arclineView = [[BMKArclineView alloc] initWithOverlay:overlay];
        arclineView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
        arclineView.lineWidth = 5.0;
        
        return arclineView;
    }
    return nil;
}
#pragma mark - POI检索 -

- (void)poiSeacrh
{
    UIButton *poiBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    poiBtn.frame = CGRectMake(10, 100, 100, 30);
    poiBtn.backgroundColor = [UIColor redColor];
    [poiBtn setTitle:@"POI检索" forState:UIControlStateNormal];
    [poiBtn addTarget:self action:@selector(poiBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:poiBtn];
    
    // POI检索
    _poiSearch = [[BMKPoiSearch alloc] init];
    _poiSearch.delegate = self;

}
- (void)poiBtnClick
{
    //在城市中搜索
   /*  BMKCitySearchOption *cityOption = [[BMKCitySearchOption alloc] init];
    cityOption.pageIndex = 0;
    cityOption.pageCapacity = 10;//显示10个地方
    cityOption.city = @"北京";
    cityOption.keyword = @"餐厅";
    BOOL flag = [_poiSearch poiSearchInCity:cityOption];
    if(flag)
        NSLog(@"城市内检索发送成功");
    else
        NSLog(@"城市内检索发送失败");
  */
    
    //在附近中搜索
    BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
    option.pageIndex = 0;
    option.pageCapacity = 10;
    CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(40.0431260000,116.4770600000);
    option.location = coor;
    option.keyword = @"美食";
    BOOL flag1 = [_poiSearch poiSearchNearBy:option];
    if(flag1)
        NSLog(@"周边检索发送成功");
    else
        NSLog(@"周边检索发送失败");
    
    // 地理编码
    _geoCodeSearch = [[BMKGeoCodeSearch alloc] init];
    _geoCodeSearch.delegate = self;
    BMKGeoCodeSearchOption *geocodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];
    geocodeSearchOption.city= @"北京";
    geocodeSearchOption.address = @"宝盛北里";
    BOOL flag = [_geoCodeSearch geoCode:geocodeSearchOption];
    if(flag)
        NSLog(@"geo检索发送成功");
    else
        NSLog(@"geo检索发送失败");
    
    
    // 反向地理编码
    CLLocationCoordinate2D pt = CLLocationCoordinate2DMake(40.0431260000,116.4770600000);
    
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    reverseGeocodeSearchOption.reverseGeoPoint = pt;
    BOOL flag2 = [_geoCodeSearch reverseGeoCode:reverseGeocodeSearchOption];
    if(flag2)
        NSLog(@"反geo检索发送成功");
    else
        NSLog(@"反geo检索发送失败");


    
}
//实现PoiSearchDeleage处理回调结果,检索结束后的结果
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult*)result errorCode:(BMKSearchErrorCode)error
{
    // 清除屏幕中所有的annotation
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    
    if (error == BMK_SEARCH_NO_ERROR)
    {
        NSMutableArray *annotations = [NSMutableArray array];
        for (int i = 0; i < result.poiInfoList.count; i++)
        {
            BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];
            BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
            item.coordinate = poi.pt;
            item.title = poi.name;
            [annotations addObject:item];
        }
        [_mapView addAnnotations:annotations];
        [_mapView showAnnotations:annotations animated:YES];
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR)
    {
        NSLog(@"起始点有歧义");
    }
    else
    {
        // 各种情况的判断。。。
    }
}
// 反GEO检索
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    if (error == 0) {
        BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
        item.coordinate = result.location;
        item.title =  result.address;
        [_mapView addAnnotation:item];
        _mapView.centerCoordinate = result.location;
        NSString* titleStr;
        NSString* showmeg;
        titleStr = @"反向地理编码";
        showmeg = [NSString stringWithFormat:@"%@",item.title];
        
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:titleStr message:showmeg delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
        [myAlertView show];
    }
}
// GEO检索
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    if (error == 0)
    {
        BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
        item.coordinate = result.location;
        item.title = result.address;
        [_mapView addAnnotation:item];
        _mapView.centerCoordinate = result.location;
        NSString* titleStr;
        NSString* showmeg;
        
        titleStr = @"正向地理编码";
        showmeg = [NSString stringWithFormat:@"经度:%f,纬度:%f",item.coordinate.latitude,item.coordinate.longitude];
        
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:titleStr message:showmeg delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
        [myAlertView show];
    }
}
// 标注点复用函数
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    NSString *AnnotationViewID = @"renameMark";
    BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    if (annotationView == nil)
    {
        annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
    }
    // 设置颜色
    annotationView.pinColor = BMKPinAnnotationColorPurple;
    // 从天上掉下效果
    annotationView.animatesDrop = YES;
    // 设置可拖拽
    annotationView.draggable = YES;
    // 设置图片
    annotationView.image = [UIImage imageNamed:@"3.png"];
    // 设置附属视图
    UIView *leftView = [[UIView alloc] init];
    leftView.frame = CGRectMake(0, 0, 20, 20);
    leftView.backgroundColor = [UIColor redColor];
    annotationView.leftCalloutAccessoryView = leftView;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    btn.frame = CGRectMake(0, 0, 20, 20);
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    annotationView.rightCalloutAccessoryView = btn;
    return annotationView;
}
#pragma mark -BMKLocationServiceDelegate-
//将要开始定位时调用
- (void)willStartLocatingUser
{
    NSLog(@"star location");
}

//用户改变方向时调用
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    
}

//用户改变位置时候调用
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    NSLog(@"%f : %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    [_mapView updateLocationData:userLocation];
    
}

//定位结束时调用
- (void)didStopLocatingUser
{
    
}

//定位失败时调用
- (void)didFailToLocateUserWithError:(NSError *)error
{
    
}


// 当点击annotation view弹出的泡泡时,调用此接口
- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view;
{
    NSLog(@"paopaoclick");
}

 

posted on 2016-04-06 18:56  风过无痕CL  阅读(441)  评论(0编辑  收藏  举报

导航