gpt4 book ai didi

ios - 使用 Mapkit 实现多个覆盖

转载 作者:行者123 更新时间:2023-11-29 03:02:20 26 4
gpt4 key购买 nike

我正在做一个利用 Mapkit 框架的项目。最初有一张佛罗里达州 map ,上面有许多注释和圆形覆盖图,显示哪些区域接收送货。我想要所有这些,以及一个 MKPolygon 来突出显示整个状态。 (这是通过使用所有坐标的 XML 文件和 Objective-C 中的 XMLParser 来完成的)

MKCircleView 覆盖所有显示效果很好,我猜这是因为它们都是同一类型。但是,当我尝试实现 MKPolygon View 时,它不允许我这样做。

//Region for fort lauderdale delivery area overlay

MKCoordinateRegion fortLauderdale;
fortLauderdale.center.latitude = FTL_LATITUDE;
fortLauderdale.center.longitude = FTL_LONGITUDE;
fortLauderdale.span.latitudeDelta = SPAN_VALUE;
fortLauderdale.span.longitudeDelta = SPAN_VALUE;

//Create a circle for fort lauderdale area
MKCircle *fortLauderdaleCircle= [MKCircle circleWithCenterCoordinate:fortLauderdale.center radius:5000];
//2KM or 1.24 miles
[self.mapView addOverlay:fortLauderdaleCircle];

//Region for miami delivery area overlay

MKCoordinateRegion miami;
miami.center.latitude = MIA_LATITUDE;
miami.center.longitude = MIA_LONGITUDE;
miami.span.latitudeDelta = SPAN_VALUE;
miami.span.longitudeDelta = SPAN_VALUE;

//Create a circle for miami area
MKCircle *miamiCircle = [MKCircle circleWithCenterCoordinate:miami.center radius:5000];
//2KM or 1.24 miles
[self.mapView addOverlay:miamiCircle];

然后在实现它们的方法中我有这个:

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
MKCircleView *circleView = [[MKCircleView alloc]initWithCircle:overlay];
circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5];
circleView.strokeColor =[[UIColor blueColor] colorWithAlphaComponent:0.6];
circleView.lineWidth = 1 ;
return circleView;

//This is the code for the polygon view but I don't know how
//to return it without run-time errors
//MKPolygonView *polygonView = [[MKPolygonView alloc] initWithPolygon:overlay];
//polygonView.fillColor = [[UIColor greenColor] colorWithAlphaComponent:0.2];
//polygonView.strokeColor = [[UIColor greenColor] colorWithAlphaComponent:0.6];
//polygonView.lineWidth = 1;
//return polygonView;
}

这看起来已经很糟糕了,因为我在方法中调用了两个 return 语句,但这又是我问这个问题的原因。我不认为错误在于我解析 XML 数据的方式,因为在实现它之前没有错误。谢谢

最佳答案

首先,我想指出您正在使用已弃用的方法。 MKOverlayView 在 iOS7 中已弃用。除此之外,您需要做的是根据传递给委托(delegate)的覆盖参数确定要返回哪种类型的覆盖。所以,如果我要修改你的代码,我会这样做:

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKCircle class]]) {
MKCircleView *circleView = [[MKCircleView alloc]initWithCircle:overlay];
circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5];
circleView.strokeColor =[[UIColor blueColor] colorWithAlphaComponent:0.6];
circleView.lineWidth = 1 ;
return circleView;
} else if ([overlay isKindOfClass:[MKPolygon class]]) {
MKPolygonView *polygonView = [[MKPolygonView alloc] initWithPolygon:overlay];
polygonView.fillColor = [[UIColor greenColor] colorWithAlphaComponent:0.2];
polygonView.strokeColor = [[UIColor greenColor] colorWithAlphaComponent:0.6];
polygonView.lineWidth = 1;
return polygonView;
}

return nil;
}

关于ios - 使用 Mapkit 实现多个覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23164211/

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