您的位置:首页 > 移动开发 > IOS开发

iOS地图 很多坐标点 怎样显示所有坐标点 并使屏幕居中

2016-01-20 10:48 507 查看
最近做地图遇到的问题记录一下


(1) 地图 有很多坐标点是怎么找到中心点并使屏幕居中的代码。

CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;

CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;

for(VEDestionationMapModel *annotation in AnnosArr) {
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}

MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
for (int i=0; i<AnnosArr.count; i++) {

VEListMapModel *model=AnnosArr[i];
[self.mapView addAnnotation:model];

}


(2)怎么监测地图的缩放比(在mapKit的代理方法中检测)

//获得地图缩放比的方法

-(float)mapScale{

float scale = self.mapView.bounds.size.width / self.mapView.visibleMapRect.size.width;

return scale;

}


//在该代理方法中监测缩放比并执行相应的操作

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{

NSLog(@"scale==%lf",[self mapScale]);

if (firstScale>[self mapScale]) {

[self.mapView removeAnnotations:self.mapView.annotations];
[self loadDateWithParam:firstParam];

}

}


(3)图片样式的问题

如下图所示我们要做到如下图所示的样式。



难点(1)是怎么设置这个圆角

这种一边内圆角,一边外凸的圆角,搜了好久好像没有方法做出来(可能是我读书少,但是聪明的人类总有解决方案的)

我想到了一个办法把这个图分为两部分

一边图片


另一边倒半边圆角

如下图


倒半边圆角代码附上

/**
*  半边倒角
*  @param label 要倒角的
*  @return mask
*/
-(CAShapeLayer *)makOneSideRoundingCorner:(UIView *)label{

UIBezierPath *maskPath=[UIBezierPath bezierPathWithRoundedRect:label.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = label.bounds;
maskLayer.path = maskPath.CGPath;
return maskLayer;

}


做出效果图如下



难点(2)怎么解决复用的问题

`-(MKAnnotationView )mapView:(MKMapView )mapView viewForAnnotation:(id)annotation{

if ([annotation isKindOfClass:[VEListMapModel class]]) {//列表

static NSString *annoid=@”listAnnoid”;

VEListMapAnnotationView annoView=(VEListMapAnnotationView )[mapView dequeueReusableAnnotationViewWithIdentifier:annoid];

if (!annoView) {
annoView=[[VEListMapAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annoid];

annoView.image=[UIImage imageNamed:@"mapUnSelected"];
annoView.canShowCallout=NO;

}


//这里解决Label复用问题

UILabel *labe=[annoView viewWithTag:400];

if (labe) {

[labe removeFromSuperview];

}

UILabel *label=[[UILabel alloc]init];
label.backgroundColor=[UIColor colorWithWhite:0.000 alpha:0.5];
label.font=[UIFont systemFontOfSize:18];
label.textColor=[UIColor whiteColor];
label.textAlignment=NSTextAlignmentCenter;
label.tag=400;

VEListMapModel *anno=(VEListMapModel *)annotation;
label.text=anno.detail;
CGFloat size=[CommonTools labelSize:anno.detail fontsize:22.0];
label.frame=CGRectMake(CGRectGetWidth(annoView.frame), 5,size ,30 );
[annoView addSubview:label];

label.layer.mask = [self makOneSideRoundingCorner:label];
annoView.annotation=annotation;
return annoView;


`
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: