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;
`
相关文章推荐
- iOS设备信息的获取
- iOS开发中如何自定义字体?
- 开启新篇章
- iOS -字符串替换 富文本的简单使用
- iOS优秀博客收录
- AFNetworking
- iOS textField禁止输入指定字
- IOS开发基础知识--碎片32
- iPhone iOS 设备如何 取消 信任此电脑
- iOS-CocoaPods的介绍、安装、使用、问题
- iOS Provisioning Profiles文件路径
- 在Xcode7.x中安装Alcatraz
- 我的iOS开发之路
- iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
- iOS 触摸事件处理
- 【iOS】Plist-XML-JSON数据解析
- iOS 滑动性能优化
- Label显示不同的字体颜色
- iOS高级工程师晋级之路
- IOS自适配利器Masonry使用指南