2013-05-07 11 views
5

Ich habe einen Radius und einen Ort.So überlagern Sie einen Kreis auf einer iOS-Karte

So versuche ich, das umschließende Rechteck des Kreises zu erhalten.

- (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(...) scheint in Karte Punkten gemessen Breite und Höhe zu wollen. Wie konvertiere ich den Radius in das?

Am Ende ich mache es so:

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

Der Radius nicht gleich Meter auf der Karte am Ende scheint und auch der Abstand scheint nicht richtig gemessen werden. Wie geht es richtig?

Ich wäre wirklich dankbar für jeden Hinweis.

Antwort

14

Sie sollten stattdessen MKCircle verwenden. So etwas wie:

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

Und nehmen die MKMapViewDelegate Methode unten und etwas tun, wie folgt aus:

- (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; 
} 
+0

Danke, genau das, was ich gesucht habe :) – kadrian

+0

Kein Problem. Freue mich zu helfen! –

+0

Vielen Dank. Es funktioniert gut. – Raja

6

Für iOS7:

Gleiche wie tdevoy in viewDidLoad:

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

Aber die Delegate-Methode (da mapView: viewForOverlay: veraltet ist):

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

    return circleR; 
} 
Verwandte Themen