如何在iOS地图上叠加圆圈 [英] How to overlay a circle on an iOS map

查看:191
本文介绍了如何在iOS地图上叠加圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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(...)似乎想要在地图点测量宽度和高度。如何将半径转换为?

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

最后我将其呈现如下:

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.

推荐答案

你应该使用MKCircle。做类似的事情:

You should be using MKCircle instead. Do something like:

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

并采用下面的MKMapViewDelegate方法并执行以下操作:

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

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay

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

这篇关于如何在iOS地图上叠加圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆