0

我在 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];
      }
    }
 }

数组“coordinatesArray”是使用文本字段创建的。因此,每当我在字段中输入不同的点时,都会按预期绘制新路线,并删除除一条线之外的旧路线。一条旧线永远不会被删除。我不应该使用 removeOverlay 吗?谢谢!

4

0 回答 0