我正在使用Mapkit框架进行项目。最初有佛罗里达州的地图,有许多注释和圆形叠加,显示哪些地区接收交付。我想拥有所有这些,以及MKPolygon来突出整个州。 (通过使用所有坐标的XML文件和Objective-C中的XMLParser完成)
MKCircleView覆盖所有显示正常,我猜是因为它们都是相同的类型。但是,当我尝试实现MKPolygon视图时,它也不允许我这样做。
//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数据的方式,因为在执行它之前没有错误。谢谢
答案 0 :(得分:1)
首先,我想指出您使用的是弃用方法。在iOS7中已弃用MKOverlayView
。除此之外,您需要做的是根据传递给委托的overlay参数确定要返回的覆盖类型。所以,如果我要修改你的代码,这就是我要做的:
-(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;
}