Ios 我可以动态放大/缩小在UIMapView上绘制的非对称多边形吗?

Ios 我可以动态放大/缩小在UIMapView上绘制的非对称多边形吗?,ios,iphone,objective-c,mkmapview,Ios,Iphone,Objective C,Mkmapview,我试图首先在UIMapView上绘制一个非对称多边形,使用MKPolygon作为其动态覆盖。在此之后,最终用户应该能够仅放大/缩小该特定区域,而地图区域的其余部分则保持不变 简单地从最终用户的角度来看,用户应该能够使用手指在地图视图上绘制某个区域,然后仅缩放该特定区域,而其他区域不受缩放操作的影响 据我所知,没有内置的方法可以做到这一点。 此外,官方文件指出: 您可以按原样使用该类来显示地图信息并操作应用程序中的地图内容。可以将地图居中放置在给定坐标上,指定要显示的区域大小,并使用自定义信息对地

我试图首先在UIMapView上绘制一个非对称多边形,使用MKPolygon作为其动态覆盖。在此之后,最终用户应该能够仅放大/缩小该特定区域,而地图区域的其余部分则保持不变


简单地从最终用户的角度来看,用户应该能够使用手指在地图视图上绘制某个区域,然后仅缩放该特定区域,而其他区域不受缩放操作的影响

据我所知,没有内置的方法可以做到这一点。 此外,官方文件指出:

您可以按原样使用该类来显示地图信息并操作应用程序中的地图内容。可以将地图居中放置在给定坐标上,指定要显示的区域大小,并使用自定义信息对地图进行注释

因此,要实现您想要实现的目标,您需要使用两个
MKMapView
,一个在另一个之上

项目链接 我制作了一个简单的项目来向您展示功能,您可以在这里找到它:

截图 在这里,您可以看到内部地图视图在随机多边形中显示缩放的地图。

解释 开始添加两个MKMapView,可以通过编程方式,也可以通过故事板。 在此之后,您可以使用
CAShapeLayer
遮罩最顶部的
MKMapView
,该路径是使用
MKPolygon
中的点设置的

MKPolygon
但是只存储
MKMapPoint
,所以我们需要在CGPoint中转换它们。我们能做什么

执行此操作的有用方法是由
MKMapView
本身提供的,
convertCoordinate:toPointInView:
,它将CLLocationCoordinate2D点转换为正常的CGPoint,例如,我们可以编写如下方法:

/**
 *  Convert between MKMapPoint and CGPoint, to be used as masking path point.
 *
 *  @param point The MKMapPoint to be converted
 *
 *  @return The CGPoint, converted in UIView coords from MKMapPoint provided
 */
- (CGPoint) convertPointToMapView:(MKMapPoint) point
{
    return [self.mainMapView convertCoordinate:MKCoordinateForMapPoint(point) toPointToView:self.mainMapView];
}
// Create a CAShapeLayer to hold masking path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
CGMutablePathRef mask   = CGPathCreateMutable();

// First point...
CGPoint firstPoint      = [self convertPointToMapView:polygon.points[0]];
CGPathMoveToPoint(mask, NULL, firstPoint.x, firstPoint.y);

// Then with some simple CG functions we can draw all the mask
for(NSUInteger i = 0; i < polygon.pointCount - 1; i++)
{
    CGPoint nextPoint   = [self convertPointToMapView:polygon.points[i+1]];
    CGPathAddLineToPoint(mask, NULL, nextPoint.x, nextPoint.y);
}

// Close path
CGPathCloseSubpath(mask);

maskLayer.path  = mask;
CGPathRelease(mask);

// Mask the second MKMapView
self.backgroundMapView.layer.mask           = maskLayer;
给定一个多边形,您只需使用此辅助对象方法创建CAShapeLayer路径,并将其作为遮罩应用于另一个
MKMapView
,如下所示:

/**
 *  Convert between MKMapPoint and CGPoint, to be used as masking path point.
 *
 *  @param point The MKMapPoint to be converted
 *
 *  @return The CGPoint, converted in UIView coords from MKMapPoint provided
 */
- (CGPoint) convertPointToMapView:(MKMapPoint) point
{
    return [self.mainMapView convertCoordinate:MKCoordinateForMapPoint(point) toPointToView:self.mainMapView];
}
// Create a CAShapeLayer to hold masking path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
CGMutablePathRef mask   = CGPathCreateMutable();

// First point...
CGPoint firstPoint      = [self convertPointToMapView:polygon.points[0]];
CGPathMoveToPoint(mask, NULL, firstPoint.x, firstPoint.y);

// Then with some simple CG functions we can draw all the mask
for(NSUInteger i = 0; i < polygon.pointCount - 1; i++)
{
    CGPoint nextPoint   = [self convertPointToMapView:polygon.points[i+1]];
    CGPathAddLineToPoint(mask, NULL, nextPoint.x, nextPoint.y);
}

// Close path
CGPathCloseSubpath(mask);

maskLayer.path  = mask;
CGPathRelease(mask);

// Mask the second MKMapView
self.backgroundMapView.layer.mask           = maskLayer;
//创建一个CAShapeLayer来保存掩蔽路径
CAShapeLayer*maskLayer=[CAShapeLayer层];
CGMutablePathRef mask=CGPathCreateMutable();
//第一点。。。
CGPoint firstPoint=[self-convertPointToMapView:polygon.points[0]];
CGPathMoveToPoint(掩码,NULL,firstPoint.x,firstPoint.y);
//然后用一些简单的CG函数我们可以画出所有的面具
对于(整数i=0;i
需要注意的一些事项:

  • 多边形
    是一个多边形。如果需要从taps中绘制,可以使用MKMapView方法,该方法与我以前使用的方法
    converCoordinate:toPointInView
    完全相反

  • polygon.points
    MKMapPoint
    的C数组(因此其类型为
    MKMapPoint*


据我所知,没有内置的方法可以做到这一点。 此外,官方文件指出:

您可以按原样使用该类来显示地图信息并操作应用程序中的地图内容。可以将地图居中放置在给定坐标上,指定要显示的区域大小,并使用自定义信息对地图进行注释

因此,要实现您想要实现的目标,您需要使用两个
MKMapView
,一个在另一个之上

项目链接 我制作了一个简单的项目来向您展示功能,您可以在这里找到它:

截图 在这里,您可以看到内部地图视图在随机多边形中显示缩放的地图。

解释 开始添加两个MKMapView,可以通过编程方式,也可以通过故事板。 在此之后,您可以使用
CAShapeLayer
遮罩最顶部的
MKMapView
,该路径是使用
MKPolygon
中的点设置的

MKPolygon
但是只存储
MKMapPoint
,所以我们需要在CGPoint中转换它们。我们能做什么

执行此操作的有用方法是由
MKMapView
本身提供的,
convertCoordinate:toPointInView:
,它将CLLocationCoordinate2D点转换为正常的CGPoint,例如,我们可以编写如下方法:

/**
 *  Convert between MKMapPoint and CGPoint, to be used as masking path point.
 *
 *  @param point The MKMapPoint to be converted
 *
 *  @return The CGPoint, converted in UIView coords from MKMapPoint provided
 */
- (CGPoint) convertPointToMapView:(MKMapPoint) point
{
    return [self.mainMapView convertCoordinate:MKCoordinateForMapPoint(point) toPointToView:self.mainMapView];
}
// Create a CAShapeLayer to hold masking path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
CGMutablePathRef mask   = CGPathCreateMutable();

// First point...
CGPoint firstPoint      = [self convertPointToMapView:polygon.points[0]];
CGPathMoveToPoint(mask, NULL, firstPoint.x, firstPoint.y);

// Then with some simple CG functions we can draw all the mask
for(NSUInteger i = 0; i < polygon.pointCount - 1; i++)
{
    CGPoint nextPoint   = [self convertPointToMapView:polygon.points[i+1]];
    CGPathAddLineToPoint(mask, NULL, nextPoint.x, nextPoint.y);
}

// Close path
CGPathCloseSubpath(mask);

maskLayer.path  = mask;
CGPathRelease(mask);

// Mask the second MKMapView
self.backgroundMapView.layer.mask           = maskLayer;
给定一个多边形,您只需使用此辅助对象方法创建CAShapeLayer路径,并将其作为遮罩应用于另一个
MKMapView
,如下所示:

/**
 *  Convert between MKMapPoint and CGPoint, to be used as masking path point.
 *
 *  @param point The MKMapPoint to be converted
 *
 *  @return The CGPoint, converted in UIView coords from MKMapPoint provided
 */
- (CGPoint) convertPointToMapView:(MKMapPoint) point
{
    return [self.mainMapView convertCoordinate:MKCoordinateForMapPoint(point) toPointToView:self.mainMapView];
}
// Create a CAShapeLayer to hold masking path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
CGMutablePathRef mask   = CGPathCreateMutable();

// First point...
CGPoint firstPoint      = [self convertPointToMapView:polygon.points[0]];
CGPathMoveToPoint(mask, NULL, firstPoint.x, firstPoint.y);

// Then with some simple CG functions we can draw all the mask
for(NSUInteger i = 0; i < polygon.pointCount - 1; i++)
{
    CGPoint nextPoint   = [self convertPointToMapView:polygon.points[i+1]];
    CGPathAddLineToPoint(mask, NULL, nextPoint.x, nextPoint.y);
}

// Close path
CGPathCloseSubpath(mask);

maskLayer.path  = mask;
CGPathRelease(mask);

// Mask the second MKMapView
self.backgroundMapView.layer.mask           = maskLayer;
//创建一个CAShapeLayer来保存掩蔽路径
CAShapeLayer*maskLayer=[CAShapeLayer层];
CGMutablePathRef mask=CGPathCreateMutable();
//第一点。。。
CGPoint firstPoint=[self-convertPointToMapView:polygon.points[0]];
CGPathMoveToPoint(掩码,NULL,firstPoint.x,firstPoint.y);
//然后用一些简单的CG函数我们可以画出所有的面具
对于(整数i=0;i
需要注意的一些事项:

  • 多边形
    是一个多边形。如果需要从taps中绘制,可以使用MKMapView方法,该方法与我以前使用的方法
    converCoordinate:toPointInView
    完全相反

  • polygon.points
    MKMapPoint
    的C数组(因此其类型为
    MKMapPoint*


使用MKPolygon和UIMapView是否可以做到这一点?使用内置函数肯定不可能做到这一点。。。我建议使用一个包含两个地图视图的解决方案,使用多边形作为ma上的某种遮罩