1. 配置的问题ios
其实我我的以为高德地图没有百度地图好用,可是最初开始就是用的高德地图,而后有些问题,可是不甘心放弃,因此就一直用高德地图,本身解决了那些问题。git
最近买了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.xcode
原来是IOS9中更改了加密方式,解决办法以下,在info.plist中添加App Transport Security Settings:网络
而后就可按着官方文档进行环境的配置。可是运行时并不会定位,是由于ios8中系统corelocation框架发生了改变,中必须在info.plist加入一个string字段来才能定位,app
NSLocationAlwaysUsageDescription 运行时持续定位。框架
NSLocationWhenInUseUsageDescription 进入后台就中止定位。 ide
都会弹出个提示框让用户决定是否赞成定位。函数
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; }
效果图以下: