2013-05-07 13 views
5

Mam promień i lokalizację.Jak nakładać kółko na mapie iOS

Oto jak próbuję uzyskać prostokąt ograniczający koła.

- (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(...) wydaje się, że chce szerokości i wysokości zmierzonej w punktach mapy. Jak przekonwertować promień do tego?

W końcu mam co czyni je tak:

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

Promień nie wydają się równe metrów na mapie w końcu, a także odległość nie wydaje się być mierzona prawidłowo. Jak to zrobić dobrze?

Byłbym bardzo wdzięczny za każdą podpowiedź.

Odpowiedz

14

Zamiast tego powinieneś używać MKCircle. Zrobić coś takiego:

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

I przyjąć metodę MKMapViewDelegate poniżej i zrobić coś takiego:

- (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

Dzięki, dokładnie to, czego szukałem :) – kadrian

+0

Bez problemu. Chętnie pomoże! –

+0

Dziękuję. Działa dobrze. – Raja

6

Dla iOS7:

samo co tdevoy w viewDidLoad:

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

Ale metoda delegata (ponieważ mapView: viewForOverlay: jest przestarzała):

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

    return circleR; 
} 
Powiązane problemy