IOS开发学习记录之高德地图的学习1 定位,ios9的配置,添加标记点等。

1. 配置的问题

其实我个人觉得高德地图没有百度地图好用,但是最初开始就是用的高德地图,然后有些问题,但是不甘心放弃,所以就一直用高德地图,自己解决了那些问题。

最近买了macbook,装的xcode7,结果就不能定位了。提示如下:      

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

  原来是IOS9中更改了加密方式,解决办法如下,在info.plist中添加App Transport Security Settings:

 

然后就可按着官方文档进行环境的配置。但是运行时并不会定位,是因为ios8中系统corelocation框架发生了改变,中必须在info.plist加入一个string字段来才能定位,

NSLocationAlwaysUsageDescription  运行时持续定位。

NSLocationWhenInUseUsageDescription  进入后台就停止定位。  

都会弹出个提示框让用户决定是否同意定位。

 

2.定位的实现

  

/*首先必须使地图可以显示用户当前位置,即可以在地图中心显示一个小圆点*/
/*然后执行定位的代理方法,在地位完成时获取当前地址编码*/
/*然后进行一个地理反编码的操作,通过地图搜索API,获取当前地址的详细信息*/
/*这里我学着用了下masonry,可以自己设置布局*/
#import "HomeViewController.h"
#import "Masonry.h"
@interface HomeViewController ()
@property (nonatomic,retain) MAMapView *mapView;
@end

@implementation HomeViewController
@synthesize mapView=_mapView;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    // Do any additional setup after loading the view from its nib.
}
-(void)initView{
    UIView *superview=self.view;
    UIEdgeInsets padding=UIEdgeInsetsMake(10, 10, 10, 10);
    mapView=[[MAMapView alloc]init];
    search=[[AMapSearchAPI alloc]init];
    mapView.delegate=self;
    search.delegate=self;
    [superview addSubview:mapView];

    [mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler
        make.left.equalTo(superview.mas_left).with.offset(padding.left);
        make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
        make.right.equalTo(superview.mas_right).with.offset(-padding.right);
    }];
    mapView.showsUserLocation=YES; //一定要可以显示用户地位点才可以
}
#pragma mark  定位
//开始定位时执行,将定位方式设置为跟随模式即可定位
- (void)mapViewWillStartLocatingUser:(MAMapView *)mapview{
    mapView.userTrackingMode=MAUserTrackingModeFollow;
}
//定位失败时执行
-(void)mapView:(MAMapView *)mapView didFailToLocateUserWithError:(NSError *)error{
     NSLog(@"%@",error);
}
//用户地址发生更新后执行
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    //将用户地址传入参数currentLocation
    currentLocation = [userLocation.location copy];
    //  NSLog(@"_currentLoaction:%@",currentLocation);
    //获得当前地理编码后,进行反编码,获得当前定位点的信息
    [self reGeoAction];
}
//地理反编码函数
-(void)reGeoAction
{
    if (currentLocation) {
        //生成一个地理反编码的搜索
        AMapReGeocodeSearchRequest *request = [[AMapReGeocodeSearchRequest alloc] init];
        
        request.location = [AMapGeoPoint locationWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude];
        [search AMapReGoecodeSearch:request];
    }
}
// 反编码回调函数
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    //将搜索结果传给用户定位点,这样呼出气泡就可以显示出详细信息
    NSString *title = response.regeocode.addressComponent.city;
    if (title.length ==0) {
        title = response.regeocode.addressComponent.province;
    }
    mapView.userLocation.title = title;
    mapView.userLocation.subtitle = response.regeocode.formattedAddress;
}
//反编码时会进行一个搜索操作,搜索失败时会执行这个方法
- (void)searchRequest:(id)request didFailWithError:(NSError *)error
{
    NSLog(@"request:%@,error:%@",request,error);
}

 

效果图如下:

 

3 插入标记点

 

  插入标记点,其实就是向mapview添加一个个annotation,可以生成一个数组,然后存入这些annotation,一次性添加到地图上。

  MAPointAnnotation是MAAnnotation的子类,它有CLLocationCoordinate2D,title,subtitle等属性,分别指坐标,标题和副标题。标题和副标题就是默认显示在弹出气泡中的。默认下气泡是可以弹出的。关于气泡的自定义等设置,在下次博文中讲解。

#pragma mark 设置标记点
//先网络请求获取需要的标记点的坐标,title和subtitle数据
-(void)requestData{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    NSString *urlstr;
    if ([mapType isEqualToString:@"CDZ"]) {
        urlstr =checkCDZUrl;
    }
    else if ([mapType isEqualToString:@"WXZ"]){
        urlstr=checkWXZUrl;
    }
    NSDictionary *parameters=@{@"PoleState":@"",@"PoleAddress":@"武汉",@"PoleName":@"",@"PoleStyle":@""};
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:urlstr parameters:parameters error:nil];
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            _AnnotationsArr=[NSMutableArray new];
            _DataArr=[responseObject objectForKey:@"Content"];
            NSDictionary *dic;
            for (int i=0;i<_DataArr.count;i++) {
                dic=_DataArr[i];
        //这里我们在循环中每次新生成一个MAPointAnnotation来做标记点
                MAPointAnnotation *poi=[[MAPointAnnotation alloc]init];
        //给标记点传入title和subtitle
                poi.title=[dic objectForKey:@"PoleName"];
                poi.subtitle=[dic objectForKey:@"PoleAdress"];
                NSString *degreeStr=[dic objectForKey:@"PoleFix"];
        //最重要的是坐标
                NSArray *components = [degreeStr componentsSeparatedByString:@","];
                CLLocationDegrees latitude=[components[0]doubleValue];
                CLLocationDegrees longitude=[components[1]doubleValue];
                poi.coordinate=CLLocationCoordinate2DMake(latitude,longitude);
                //然后将这些标记点存入可变数组中
                [_AnnotationsArr addObject:poi];
            }
            //再将全部标记点一次插入地图
                [mapView addAnnotations:_AnnotationsArr];
        }}];
    [dataTask resume];
}
-(void)SearchPoi{
    
}
#pragma mark MAMapView-delegate
//这是标记点实现的代理方法,就如同tableview的cellfor方法,可以再该方法中置
//标记点的样式等等属性,自定义标记点也要在此方法中实现
-(MAAnnotationView*)mapView:(MAMapView *)mapview viewForAnnotation:(id<MAAnnotation>)annotation{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        
        static NSString *reuseId=@"identifier";
        MAPinAnnotationView *an=(MAPinAnnotationView*)[mapview dequeueReusableAnnotationViewWithIdentifier:reuseId];
        if (an==nil) {
            an=[[MAPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:reuseId];
        }
    //点击可以弹出气泡
        an.canShowCallout=YES;
    //自己设置标记点的图片,这里我用了一个分类来改变素材的大小
        UIImage *image=[UIImage imageNamed:@"map-marker1"];
        CGSize imagesize=CGSizeMake(30, 30);
        an.image=[image imageByScalingToSize:(imagesize)];
        return an;
    }
    return nil;
}

 

效果图如下:

转载于:https://www.cnblogs.com/JoeyCao/p/5392203.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值