热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在iOS地图上覆盖一个圆圈-HowtooverlayacircleonaniOSmap

Ivegotaradiusandalocation.我有一个半径和一个位置。ThisishowImtryingtogettheboundingrectangle

I've got a radius and a location.

我有一个半径和一个位置。

This is how I'm trying to get the bounding rectangle of the circle.

这就是我想要得到圆的边界矩形的方法。

- (MKMapRect)boundingMapRect{

    CLLocationCoordinate2D tmp;
    MKCoordinateSpan radiusSpan = MKCoordinateRegionMakeWithDistance(self.coordinate, 0, self.radius).span;
    tmp.latitude = self.coordinate.latitude - radiusSpan.longitudeDelta;
    tmp.lOngitude= self.coordinate.longitude - radiusSpanSpan.longitudeDelta;

    MKMapPoint upperLeft = MKMapPointForCoordinate(tmp);
    MKMapRect bounds = MKMapRectMake(upperLeft.x, upperLeft.y, self.radius * 2, self.radius * 2);

    return bounds;
}

MKMapRectMake(...) seems to want width and height measured in Map points. How do I convert the radius to that?

MKMapRectMake(…)似乎想要用映射点测量宽度和高度。如何把半径转换成这个?

In the end I'm rendering it like this:

最后我把它画成这样:

MKMapRect theMapRect = [self.overlay boundingMapRect];
CGRect theRect = [self rectForMapRect:theMapRect];
CGContextAddEllipseInRect(ctx, theRect);
CGContextFillPath(ctx);

The radius doesn't seem to equal meters on the map in the end and also the distance doesn't seem to be measured correctly. How to do it right?

在地图上,半径似乎不等于米,而且距离似乎也没有被正确测量。怎么做才对呢?

I would be really thankful for every hint.

我会感激每一个暗示。

2 个解决方案

#1


14  

You should be using MKCircle instead. Do something like:

您应该使用MKCircle代替。做些什么:

CLLocationDistance fenceDistance = 300;
CLLocationCoordinate2D circleMiddlePoint = CLLocationCoordinate2DMake(yourLocation.latitude, yourLocation.longitude);
MKCircle *circle = [MKCircle circleWithCenterCoordinate:circleMiddlePoint radius:fenceDistance];
[yourMapView addOverlay: circle];

And adopt the MKMapViewDelegate Method below and do something like this:

并采用下面的MKMapViewDelegate方法,完成如下操作:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay

{
     MKCircleView *circleView = [[[MKCircleView alloc] initWithCircle:(MKCircle *)overlay] autorelease];
     circleView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.9];
     return circleView;
 }

#2


6  

For iOS7 :

iOS7:

Same as tdevoy in viewDidLoad :

与viewDidLoad中的tdevoy相同:

CLLocationDistance fenceDistance = 300;
CLLocationCoordinate2D circleMiddlePoint = CLLocationCoordinate2DMake(yourLocation.latitude, yourLocation.longitude);
MKCircle *circle = [MKCircle circleWithCenterCoordinate:circleMiddlePoint radius:fenceDistance];
[yourMapView addOverlay: circle];

But the delegate method (because mapView:viewForOverlay: is deprecated) :

但是委托方法(因为mapView:viewForOverlay:不赞成):

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id )overlay
{
    MKCircleRenderer *circleR = [[MKCircleRenderer alloc] initWithCircle:(MKCircle *)overlay];
    circleR.fillColor = [UIColor greenColor];

    return circleR;
}

推荐阅读
author-avatar
漫路细雨中_575
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有