高德地图
运用场景:先获取本机经纬度 ,从服务端获取周围的小区和停车场的经纬度,通过服务端返回的type赋值给大头针的title来判断是小区还是停车场
1,首先实例化地图
#import
#import
#import
#import
@property (nonatomic, strong) MAMapView *mapView;
#pragma mark - 懒加载
-(MAMapView *)mapView{
if (!_mapView){
_mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64-44)];
_mapView.backgroundColor = [UIColor whiteColor];
_mapView.delegate = self;
_mapView.zoomEnabled = YES;
_mapView.mapType = MAMapTypeStandard;
[_mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES];
_mapView.showsCompass= NO;
_mapView.compassOrigin= CGPointMake(_mapView.compassOrigin.x, 22);
_mapView.showsScale= NO;
_mapView.scaleOrigin= CGPointMake(_mapView.scaleOrigin.x, 22);
[_mapView setZoomLevel:16 animated:YES];
_mapView.showsUserLocation = YES;
[[LWLocationManager sharedManager]reverseGeocodeLocationSuccess:^(NSString *locationString) {
NSLog(@"locationString == %@",locationString);
} error:^(NSError *error) {
}];
}
return _mapView;
}
-(void)DatasinMap{//处理附近小区/停车场数据坐标
NSMutableArray *xiaoquArr = [NSMutableArray array];
/** 后台数据 **/
for(MLFoundCarModel *model in self.dataArray){
MAPointAnnotation *pointAnimate = [[MAPointAnnotation alloc]init];//创建大头针 循环创建
pointAnimate.coordinate = CLLocationCoordinate2DMake([model.latitude floatValue],[model.longitude floatValue]);
pointAnimate.title = model.ID;//把ID传给pointAnimate的title 进行判断是哪个大头针 这个比较重要
[xiaoquArr addObject:pointAnimate];//把大头针装到一个数组
}
[self.mapView addAnnotations:xiaoquArr];//再将数组给到mapview
}
/** 自定义大头针 **/
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id
if(([annotation isKindOfClass:[MAUserLocation class]])){//本机经纬度
static NSString *userReuseIndetifier = @"userPointReuseIndetifier";
MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:userReuseIndetifier];
if (annotationView == nil){
annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:userReuseIndetifier];
}
annotationView.canShowCallout = NO;
annotationView.animatesDrop = YES;
UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(-20, -20, 100, 20)];
name.adjustsFontSizeToFitWidth = YES;
name.textColor = White_Color;
name.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3f];
name.layer.masksToBounds = YES;
name.layer.cornerRadius = 8.0f;
name.textAlignment = NSTextAlignmentCenter;
name.font = [UIFont fontWithName:@"Arial" size:11];
if(_types != 1){//搜索页面进来 隐藏
annotationView.image = [UIImage imageNamed:@"parkingspaces_mine_pressit"];
[annotationView addSubview:name];
}
double latitude = [[MLUserModel sharedUserInfo].latitude doubleValue];
double longitude = [[MLUserModel sharedUserInfo].longitude doubleValue];
CLLocation *location = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
//根据经纬度解析成位置
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemark,NSError *error){
CLPlacemark *mark = [placemark objectAtIndex:0];
if(_types != 1){
NSLog(@"%@%@%@%@%@",mark.subLocality,mark.thoroughfare,mark.name,mark.locality,mark.subLocality);
_bottomView.myLocationTX.text = [NSString stringWithFormat:@"%@%@",mark.subLocality,mark.thoroughfare];
name.text = [NSString stringWithFormat:@"%@%@",mark.subLocality,mark.thoroughfare];//赋值
}
}];
return annotationView;
}
if ([annotation isKindOfClass:[MAPointAnnotation class]]){//自定义后台返回的坐标大头针
static NSString *customReuseIndetifier = @"customReuseIndetifier";
//MLCustomAnnotationView自定义的 参考下一篇
MLCustomAnnotationView *annotationView = (MLCustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier];
if (annotationView == nil){
annotationView = [[MLCustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:customReuseIndetifier];
annotationView.canShowCallout = NO;
annotationView.draggable = YES;
annotationView.delegate = self;
annotationView.calloutOffset = CGPointMake(0, -5);
if(_types == 1){//搜索页面传过去的经纬度 进行大头针自定义
if([annotation.title isEqualToString:@"search"]){
annotationView.portrait = [UIImage imageNamed:@"parkingspaces_mine_pressit"];
annotationView.titles.text = _address;
}
}
//处理小区和停车场图片数据
for(MLFoundCarModel *model in _dataArray){
if([model.ID isEqualToString:annotation.title]){//判断是哪个大头针
annotationView.titles.text = model.number;
if([model.type isEqualToString:@"1"]){//小区
annotationView.portrait = [UIImage imageNamed:@"parking_mapposition_district"];
}else{//停车场
annotationView.portrait = [UIImage imageNamed:@"parking_mapposition_parkinglot"];
}
}
}
}
return annotationView;
}
return nil;
}
#pragma mark 在自定义大头针view里面写了代理,点击大头针跳转详情页面
-(void)clickAnnotionView:(NSString *)ID subtitle:(NSString *)subtitle{
MLRentSeatDetailsViewController *rentvc = [[MLRentSeatDetailsViewController alloc]init];
rentvc.carId = ID;
[self.navigationController pushViewController:rentvc animated:YES];
}
#pragma mark 显示具体位置
- (void)zoomToMapPoints:(MKMapView *)mapView annotations:(NSArray*)annotations{
double minLat = 360.0f, maxLat = -360.0f;
double minLon = 360.0f, maxLon = -360.0f;
for (MKPointAnnotation *annotation in annotations) {
if ( annotation.coordinate.latitude < minLat ) minLat = annotation.coordinate.latitude;
if ( annotation.coordinate.latitude > maxLat ) maxLat = annotation.coordinate.latitude;
if ( annotation.coordinate.longitude < minLon ) minLon = annotation.coordinate.longitude;
if ( annotation.coordinate.longitude > maxLon ) maxLon = annotation.coordinate.longitude;
}
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((minLat + maxLat) / 2.0, (minLon + maxLon) / 2.0);
MKCoordinateSpan span = MKCoordinateSpanMake(maxLat - minLat, maxLon - minLon);
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[mapView setRegion:region animated:YES];
}
#pragma mark - MAMapViewDelegate
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
//创建一个经纬度点:
MAPointAnnotation *point = [[MAPointAnnotation alloc] init];
//设置点的经纬度
point.coordinate = userLocation.location.coordinate;
}
//地图放大
CGFloat oldZoom = self.mapView.zoomLevel;
[self.mapView setZoomLevel:(oldZoom + 1) animated:YES];
//放小
CGFloat oldZoom = self.mapView.zoomLevel;
[self.mapView setZoomLevel:(oldZoom - 1) animated:YES];
//定位到当前位置
if(self.mapView.userLocation.updating && self.mapView.userLocation.location) {
[self.mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate animated:YES];
}
}