• iOS


    这段时间,在做百度地图的开发过程中,会遇到一些小问题。做点记录。

    开发前期,需要两个链接:

    申请密钥:http://lbsyun.baidu.com/index.php?title=iossdk/guide/key

    配置开发环境:http://lbsyun.baidu.com/index.php?title=iossdk/guide/buildproject

    申请到密钥的时候,要注意BundleID和Xcode的一致。

    使用效果:

     

    为了减少页面的资源,在主要页面里,只放一张静态图,通过调用百度的静态API生成。用户需要查看详情的时候,再跳到地图的导航。

    生成静态图API地址:http://lbsyun.baidu.com/index.php?title=static

            NSString *strCerter = [NSString stringWithFormat:@"%@,%@",_houseModel.map.lng,_houseModel.map.lat];
            NSString *mapUrl = [NSString stringWithFormat:@"http://api.map.baidu.com/staticimage?center=%@&width=%f&height=%@&zoom=11&markers=%@&markerStyles=l,A",strCerter,RFSCREEN_W,@"200",strCerter];
            
            UIImageView *mapImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, RFSCREEN_W, 200)];
            [mapImageView sd_setImageWithURL:[NSURL URLWithString:mapUrl]];
            [cell addSubview:mapImageView];

    转跳到MapController就很简单了,代码如:

    //
    //  RFHouseMapViewController.m
    //  RFHouse
    //
    //  Created by jiangys on 16/3/29.
    //  Copyright © 2016年 Jiangys. All rights reserved.
    //
    
    #import "RFHouseMapViewController.h"
    #import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
    #import <BaiduMapAPI_Map/BMKMapComponent.h>
    #import <BaiduMapAPI_Search/BMKSearchComponent.h>
    
    @interface RFHouseMapViewController ()<BMKMapViewDelegate, BMKGeoCodeSearchDelegate,BMKGeneralDelegate>
    /** 百度地图 */
    @property (nonatomic, strong)  BMKMapView *mapView;
    @property (nonatomic, strong)  BMKGeoCodeSearch *geocodesearch;
    @property (nonatomic, strong)  BMKMapManager *mapManager;
    @end
    
    @implementation RFHouseMapViewController
    
    #pragma mark -- 生命周期
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor = Color_White;
        
        // 要使用百度地图,请先启动BaiduMapManager
        _mapManager = [[BMKMapManager alloc]init];
        BOOL ret = [_mapManager start:@"rUvg0vdlCsdI227Z6I1LoV8xGhZ0ZZwb" generalDelegate:self];
        if (!ret) {
            NSLog(@"manager start failed!");
        }
        
        // 初始化地图
        _geocodesearch = [[BMKGeoCodeSearch alloc]init];
        _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, RFSCREEN_W, RFSCREEN_H)];
        [_mapView setZoomLevel:14];
        [self.view addSubview:_mapView];
        
        [self reverseGeocodeWithLng:_lng lat:_lat];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    -(void)viewWillAppear:(BOOL)animated {
        [_mapView viewWillAppear];
        _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
        _geocodesearch.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
    }
    
    -(void)viewWillDisappear:(BOOL)animated {
        [_mapView viewWillDisappear];
        _mapView.delegate = nil; // 不用时,置nil
        _geocodesearch.delegate = nil; // 不用时,置nil
    }
    
    - (void)dealloc {
        if (_geocodesearch != nil) {
            _geocodesearch = nil;
        }
        if (_mapView) {
            _mapView = nil;
        }
    }
    
    #pragma mark 百度地图API
    
    // 百度地图:反向geo检索
    -(void)reverseGeocodeWithLng:(CGFloat)lng lat:(CGFloat)lat
    {
        CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};
        pt = (CLLocationCoordinate2D){lat, lng};
        BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
        reverseGeocodeSearchOption.reverseGeoPoint = pt;
        BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
        if(flag)
        {
            RFLog(@"反geo检索发送成功");
        }
        else
        {
            RFLog(@"反geo检索发送失败");
        }
    }
    
    // 百度地图:根据anntation生成对应的View
    - (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
    {
        NSString *AnnotationViewID = @"annotationViewID";
        //根据指定标识查找一个可被复用的标注View,一般在delegate中使用,用此函数来代替新申请一个View
        BMKAnnotationView *annotationView = [view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
        if (annotationView == nil) {
            annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
            ((BMKPinAnnotationView*)annotationView).pinColor = BMKPinAnnotationColorRed;
            ((BMKPinAnnotationView*)annotationView).animatesDrop = YES;
        }
        
        annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5));
        annotationView.annotation = annotation;
        annotationView.canShowCallout = TRUE;
        return annotationView;
    }
    
    // 百度地图:反向地址编码
    -(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;
        }
    }
    
    - (void)onGetNetworkState:(int)iError
    {
        if (0 == iError) {
            NSLog(@"联网成功");
        }
        else{
            NSLog(@"onGetNetworkState %d",iError);
        }
        
    }
    
    - (void)onGetPermissionState:(int)iError
    {
        if (0 == iError) {
            NSLog(@"授权成功");
        }
        else {
            NSLog(@"onGetPermissionState %d",iError);
        }
    }
    
    @end

    要注意Start这里填的是百度的"百度地图iOS SDK开发密钥"

    BOOL ret = [_mapManager start:@"rUvg0vdlCsdI227Z6I1LoV8xGhZ0ZZwb" generalDelegate:self];
  • 相关阅读:
    原生js 实现 map
    前端安全学习
    前端性能优化
    前端的跨域请求方法使用场景及各自的局限性
    7-7 12-24小时制(15 分)
    7-4 BCD解密(10 分)
    7-2 然后是几点(15 分)
    厘米换算英尺英寸
    鸡兔同笼
    数组:经典计数
  • 原文地址:https://www.cnblogs.com/jys509/p/5389365.html
Copyright © 2020-2023  润新知