我有地图视图,我喜欢使用叠加方法绘制线条,当我按下按钮时,我添加了两个位置纬度和经度来绘制它们之间的线,但是当我第二次按下我提供的新坐标和新线时在地图上有吸引力,但之前的叠加路径消失了,但我也需要在同一张地图上预先路径
这是我的代码。
- (IBAction)refreshMapMethod:(id)sender
{
int kk=[[NSUserDefaults standardUserDefaults]integerForKey:@"ham"];
if (kk==1)
{
CLLocationCoordinate2D coordinateArray[2];
coordinateArray[0] = CLLocationCoordinate2DMake(12.915181,+77.626055);
coordinateArray[1] = CLLocationCoordinate2DMake(12.892156, +77.582188);
self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
[self.myMapView setVisibleMapRect:[self.routeLine boundingMapRect]];
[self.myMapView addOverlay:self.routeLine];
[[NSUserDefaults standardUserDefaults]setInteger:2 forKey:@"ham" ];
}
if (kk==2)
{
CLLocationCoordinate2D coordinateArray[2];
coordinateArray[0] = CLLocationCoordinate2DMake(12.892156,+77.426055);
coordinateArray[1] = CLLocationCoordinate2DMake(12.892156, +77.582188);
self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
[self.myMapView setVisibleMapRect:[self.routeLine boundingMapRect]];
[self.myMapView addOverlay:self.routeLine];
[[NSUserDefaults standardUserDefaults]setInteger:3 forKey:@"ham" ];
}
if (kk==3)
{
CLLocationCoordinate2D coordinateArray[2];
coordinateArray[0] = CLLocationCoordinate2DMake(12.892156, +77.382188);
coordinateArray[1] = CLLocationCoordinate2DMake(12.892156, +77.282188);
self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
[self.myMapView setVisibleMapRect:[self.routeLine boundingMapRect]];
[self.myMapView addOverlay:self.routeLine];
[[NSUserDefaults standardUserDefaults]setInteger:3 forKey:@"ham" ];
}
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude= 12.915181;
zoomLocation.longitude=77.626055;
MKCoordinateRegion viewRegion=MKCoordinateRegionMakeWithDistance(zoomLocation, 3*METERS_PER_MILE, 3*METERS_PER_MILE);
[self.myMapView setRegion:viewRegion animated:YES];
[[NSUserDefaults standardUserDefaults]setInteger:2 forKey:@"change" ];
}
调用叠加方法..
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if(overlay == self.routeLine)
{
if(nil == self.routeLineView)
{
self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
self.routeLineView.fillColor = [UIColor redColor];
self.routeLineView.strokeColor = [UIColor redColor];
self.routeLineView.lineWidth = 5;
}
return self.routeLineView;
}
return nil;
}
请以同样的方式或其他任何替代方式向我提供解决方案。
比你。
答案 0 :(得分:1)
这是因为您只允许viewForOverlay
函数返回self.routeline
的视图,而您只有其中一个。{1}}。对viewForOverlay
的每次其他调用都将返回nil,因此不会被绘制。您需要做的是绘制所有叠加层。
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
MKPolylineView* routeLineView = [[MKPolylineView alloc] initWithPolyline:(MKPolyLine)overlay];
routeLineView.fillColor = [UIColor redColor];
routeLineView.strokeColor = [UIColor redColor];
routeLineView.lineWidth = 5;
return routeLineView;
}
你可能需要做更多的事情,例如首先检查叠加层实际上是折线,但这应该足以让你前进。