2014-03-30 4 views
5

Ich füge ein MKCircle Overlay zu meinem mapview hinzu und ich möchte wissen, ob ein Punkt (Tap in Bildschirm) innerhalb des Kreises ist. Dies ist mein Code:Ermitteln, ob ein Punkt innerhalb von MKCircle in iPhone Sdk ios 7 ist

- (BOOL)pointInsideOverlay:(CLLocationCoordinate2D)tapPoint overlay:(id<MKOverlay>)overlay { 

    BOOL isInside = FALSE; 
    MKPolygonView *polygonView = (MKPolygonView *)[self.mapView viewForOverlay:overlay]; 
    MKMapPoint mapPoint = MKMapPointForCoordinate(tapPoint); 
    CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint]; 
    BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polygonView.path, NULL, polygonViewPoint, NO); 

    if (mapCoordinateIsInPolygon) { 
     isInside = TRUE; 
    } 
    return isInside; 
} 

viewForOverlay, pointForMapPoint & path sind veraltet. Ist das das Problem?

Vielen Dank.

+0

Für einen Kreis müssen Sie CGPathContainsPoint nicht verwenden (aber für Polygone in iOS 7, siehe http://StackOverflow.com/Questions/19014926/detecting-a-point-in-a-mkpolygon-broke- with-ios7-cgpathcontainspoint). Wie die Antwort sagt, können Sie einfach überprüfen, ob die Entfernung vom Mittelpunkt kleiner oder gleich dem Radius ist. Anstatt jedoch die pythagoräische (flache) Entfernung zu verwenden, können Sie CLLocations distanceFromLocation-Methode oder MapKits MKMetersBetweenMapPoints-Funktion verwenden, die für die gekrümmte Oberfläche der Erde verantwortlich sind. – Anna

+0

Danke. Ich verwende distanceFromLocation, um das zu tun. Vielen Dank :) – aminovic09

Antwort

11

Diese apporach sollte auch funktionieren, mit MKCircleRenderer:

MKCircleRenderer *circleRenderer = (MKCircleRenderer *)[mapview rendererForOverlay:circleOverlay]; 
    [circleRenderer invalidatePath]; 

    MKMapPoint mapPoint = MKMapPointForCoordinate(tapPoint); 
    CGPoint circlePoint = [circleRenderer pointForMapPoint:mapPoint]; 
    BOOL mapCoordinateIsInCircle = CGPathContainsPoint(circleRenderer.path, NULL, circlePoint, NO); 

    if (mapCoordinateIsInCircle) 

    { 
     //do something 
    } 
+0

Vielen Dank Harri :) – aminovic09

+0

Gern geschehen! Froh, dass es geholfen hat. – Hari

+1

Was macht InvalidatePath und warum ist es notwendig? – MonkeyBonkey

0

Wenn Sie die Mitte des Kreises und dem Radius kennen, dann können Sie etwas tun, wie ...

-(BOOL)isPoint:(CGPoint)point insideCircleCentre:(CGPoint)centre radius:(CGFloat)radius 
{ 
    CGFloat dx = point.x - centre.x; 
    CGFloat dy = point.y - centre.y; 

    CGFloat distance = sqrtf(dx * dx + dy * dy); 

    return distance <= radius; 
} 
+2

Vielen Dank. Ich benutze distanceFromLocation es zu tun - (BOOL) isPoint: (CLLocationCoordinate2D) Punkt insideCircleCentre: (CLLocationCoordinate2D) Mittenradius: (CGFloat) Radius { CLLocation * pointALocation = [[CLLocation alloc] initWithLatitude: point.latitude Länge: Punkt .Längengrad]; CLLocation * pointBLocation = [[CLLocation alloc] initWithLatitude: Mitte.Länge Länge: Mitte.Langlücke]; double distanceMeters = [pointALortungsdistanzFromLocation: pointBLocation]; if (distanceMeters> radius) { Rückgabe NO; } else { Rückgabe JA; } } – aminovic09

0

Fogmeisters Antwort falsch, da das Einheitensystem zum Ausdrücken von Punkt und Radius unterschiedlich ist. Der Punkt wird als Grad ausgedrückt und ein Radius wird typischerweise in Metern ausgedrückt. Aminovic09's Antwort ist korrekt und ist die empfohlene Antwort, da es iOS's eigene Entfernungsberechnung ermöglicht.

Verwandte Themen