gpt4 book ai didi

iphone - 使用 MapKit 显示特定区域

转载 作者:可可西里 更新时间:2023-11-01 04:22:59 25 4
gpt4 key购买 nike

我想知道是否可以使用 Map Kit 仅在 map 上显示特定区域而不是完整的世界地图。就像如果我想在我的应用程序中显示亚洲 map , map 工具包会隐藏 map 的剩余部分。

最佳答案

要处理“ map 工具包隐藏 map 的剩余部分”要求,您可以做的一件事是创建一个覆盖整个世界的黑色多边形叠加层,并在亚洲(或您喜欢的任何地方)上进行裁剪。

例如,您在何处初始化 map (例如,在 viewDidLoad 中):

CLLocationCoordinate2D asiaCoords[4] 
= { {55,60}, {55,150}, {0,150}, {0,60} };
//change or add coordinates (and update count below) as needed
self.asiaOverlay = [MKPolygon polygonWithCoordinates:asiaCoords count:4];

CLLocationCoordinate2D worldCoords[4]
= { {90,-180}, {90,180}, {-90,180}, {-90,-180} };
MKPolygon *worldOverlay
= [MKPolygon polygonWithCoordinates:worldCoords
count:4
interiorPolygons:[NSArray arrayWithObject:asiaOverlay]];
//the array can have more than one "cutout" if needed

[myMapView addOverlay:worldOverlay];

并实现 viewForOverlay 委托(delegate)方法:

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolygon class]])
{
MKPolygonView *pv = [[[MKPolygonView alloc] initWithPolygon:overlay] autorelease];
pv.fillColor = [UIColor blackColor];
pv.alpha = 1.0;
return pv;
}

return nil;
}

看起来像这样:

enter image description here

如果您还想限制用户滚动到亚洲以外或缩小得太远,那么您也需要手动执行此操作。 Restrict MKMapView scrolling 中描述了一种可能的方法.将该答案中的 theOverlay 替换为 asiaOverlay

关于iphone - 使用 MapKit 显示特定区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8679499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com