在iOS中验证latlong是否位于MKPolygon内
我正在验证latlong(在iOS中验证latlong是否位于MKPolygon内,ios,objective-c,mkmapview,mapkit,mkpolygon,Ios,Objective C,Mkmapview,Mapkit,Mkpolygon,我正在验证latlong(CLLocationCoordinate2D)是否位于在MKMapview上绘制的MKPolygon内 我使用下面的代码在MKMapview上绘制MKPolygon MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coordinates count:count]; [mapviewcontroller.mapview addOverlay:polygon]; - (MKOverlayRenderer *)m
CLLocationCoordinate2D
)是否位于在MKMapview上绘制的MKPolygon内
我使用下面的代码在MKMapview上绘制MKPolygon
MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coordinates count:count];
[mapviewcontroller.mapview addOverlay:polygon];
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
MKPolygonRenderer *renderer = [[MKPolygonRenderer alloc] initWithPolygon:overlay];
renderer.fillColor = [[UIColor grayColor] colorWithAlphaComponent:0.2];
renderer.strokeColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
renderer.lineWidth = 2;
return renderer;
}
MKPolygon*polygon=[MKPolygon polygon withcoordinates:coordinates count:count];
[mapviewcontroller.mapview addOverlay:polygon];
-(MKOverlayRenderer*)地图视图:(MKMapView*)地图视图渲染器ForOverlay:(id)overlay
{
MKPolygonRenderer*renderer=[[MKPolygonRenderer alloc]initWithPolygon:overlay];
renderer.fillColor=[[UIColor grayColor]colorWithAlphaComponent:0.2];
renderer.strokeColor=[[UIColor blackColor]colorWithAlphaComponent:0.7];
1.lineWidth=2;
返回渲染器;
}
我正在用
CLLocationCoordinate2D sampleLocation = CLLocationCoordinate2DMake(13,80);//13,80 is the latlong of clear colored area of the MKPolygon in the below image.
MKMapPoint mapPoint = MKMapPointForCoordinate(sampleLocation);
CGPoint mapPointAsCGP = CGPointMake(mapPoint.x, mapPoint.y);
for (id<MKOverlay> overlay in mapview.overlays) {
if([overlay isKindOfClass:[MKPolygon class]]){
MKPolygon *polygon = (MKPolygon*) overlay;
CGMutablePathRef mpr = CGPathCreateMutable();
MKMapPoint *polygonPoints = polygon.points;
for (int p=0; p < polygon.pointCount; p++){
MKMapPoint mp = polygonPoints[p];
if (p == 0)
CGPathMoveToPoint(mpr, NULL, mp.x, mp.y);
else
CGPathAddLineToPoint(mpr, NULL, mp.x, mp.y);
}
if(CGPathContainsPoint(mpr , NULL, mapPointAsCGP, FALSE)){
isInside = YES;
}
CGPathRelease(mpr);
}
}
cllocationcoordinate2dsamplelocation=CLLocationCoordinate2DMake(13,80)//13,80是下图中多边形的清晰彩色区域的拉长。
MKMapPoint mapPoint=mkmappointforcoordination(采样位置);
CGPoint mapPointAsCGP=CGPointMake(mapPoint.x,mapPoint.y);
用于(mapview.overlays中的id覆盖){
if([overlay iskindof类:[MKPolygon类]]){
MKPolygon*多边形=(MKPolygon*)覆盖;
CGMutablePathRef mpr=CGPathCreateMutable();
MKMapPoint*polygonPoints=polygon.points;
对于(int p=0;p
它在正常情况下非常有效,但是如果用户像下面的ie那样绘制多边形,MKpolygon会有一些相交点,颜色会在某些区域填充,而在某些区域则会显示清晰的颜色
如果我在MKPolygon中传递透明颜色部分的latlong,它应该返回NO。但是,它返回YES。例如,如果(CGPathContainsPoint(mpr,NULL,mapPointAsCGP,FALSE))
为TRUE
当MKPolygon之间有交点时,如何解决此问题?如果有人建议在清晰的颜色区域填充颜色,效果会更好。如果您有任何建议,我们将不胜感激。所示的图片似乎证明了奇偶填充规则。通过指定
FALSE
作为CGPathContainsPoint
的最终参数,您要求它应用绕组编号规则。尝试传递TRUE
有关teo规则的信息,请参阅,特别是“填充路径”(略低于一半)。Swift 3.0实施:
func checkIf(polygon: MKPolygon, contains point: CGPoint) -> Bool {
let path = CGMutablePath()
let polygonPoints = polygon.points()
for index in 0..<polygon.pointCount {
let mp = polygonPoints[index]
if index == 0 {
path.move(to: CGPoint(x: mp.x, y: mp.y))
} else {
path.addLine(to: CGPoint(x: mp.x, y: mp.y))
}
}
return path.contains(point)
}
func-checkIf(多边形:MKPolygon,包含点:CGPoint)->Bool{
let path=CGMutablePath()
设polygonPoints=polygon.points()
对于0中的索引…工作非常出色!非常感谢Tommy。