我正在MapKit中绘制多个点之间的路线。
-(void) drawRoute
{
// Removing previous lines
if (routeLine) {
[self.mapView removeOverlay:routeLine];
}
// Count how many points are in the array now
NSInteger numberOfPoints = [coordinatesArray count];
if (numberOfPoints > 1)
{
// Draw a line between two coordinates in the main array of coordinates
for (int i=0;i<([coordinatesArray count]-1);) {
CLLocationCoordinate2D routeCoordinates[2];
// Establish first and last point
NSArray *coordinatePoint1 = [coordinatesArray objectAtIndex:i];
NSArray *coordinatePoint2 = [coordinatesArray objectAtIndex:i+1];
// Get latitude and longitude of the first and last points
double latitudePoint1 = [[coordinatePoint1 objectAtIndex:0]doubleValue];
double longitudePoint1 = [[coordinatePoint1 objectAtIndex:1]doubleValue];
double latitudePoint2 = [[coordinatePoint2 objectAtIndex:0]doubleValue];
double longitudePoint2 = [[coordinatePoint2 objectAtIndex:1]doubleValue];
// Put the coordinates on the route array
routeCoordinates[0] = CLLocationCoordinate2DMake(latitudePoint1, longitudePoint1);
routeCoordinates[1] = CLLocationCoordinate2DMake(latitudePoint2, longitudePoint2);
// Draw the line
routeLine = [MKPolyline polylineWithCoordinates:routeCoordinates count:2];
i++;
// Show the line on the map
[self.mapView addOverlay:routeLine];
}
}
}
数组&#34; coordinatesArray&#34;是使用文本字段创建的。因此,每当我在字段中输入不同的点时,新路由将按预期绘制,除了一行之外,旧路由将被删除。一条旧线永远不会被删除。我应该不使用removeOverlay吗?谢谢!