如何将坐标添加到动态NSMutableArray的CLLocationCoordinate2D?

问题描述:

我有一个MapView,我想添加批注和从定义的坐标,我从文本框添加并存储在NSMutableArray中的路线。如何将坐标添加到动态NSMutableArray的CLLocationCoordinate2D?

现在我能显示来自多个坐标的路线,但只有当我将其插入我的代码如下:

-(void)loadMap{ 

    int Coordinates; 
    //MAP 
    CLLocationCoordinate2D coordinateArray[Coordinates]; 
    coordinateArray[0] = CLLocationCoordinate2DMake(LatA, LongA); 
    coordinateArray[1] = CLLocationCoordinate2DMake(LatB, LongB); 
    coordinateArray[2] = CLLocationCoordinate2DMake(LatC, LongC); 
    coordinateArray[3] = CLLocationCoordinate2DMake(LatD, LongD); 


    self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:Coordinates]; 
    [MapViewHome setVisibleMapRect:[self.routeLine boundingMapRect]]; //If you want the route to be visible 
    [MapViewHome addOverlay:self.routeLine]; 
    MapViewHome.mapType = MKMapTypeHybrid; 

    [self zoomToFitMapAnnotations:MapViewHome]; 
} 

添加注释我这样做:

-(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 purpleColor]; 
      self.routeLineView.strokeColor = [UIColor purpleColor]; 
      self.routeLineView.lineWidth = 5; 

     } 

     return self.routeLineView; 
    } 

    return nil; 
} 

-(void)AddAnotations{ 


    DeparturePoint = [[MKPointAnnotation alloc] init]; 
    DeparturePoint.coordinate = CLLocationCoordinate2DMake(LatA, LongA); 
    DeparturePoint.title = [NSString stringWithFormat:@"A"]; 
    [MapViewHome addAnnotation:DeparturePoint]; 

    ArrivalPoint = [[MKPointAnnotation alloc] init]; 
    ArrivalPoint.coordinate = CLLocationCoordinate2DMake(LatB, LongB); 
    ArrivalPoint.title = [NSString stringWithFormat:@"B"]; 

    [MapViewHome addAnnotation:ArrivalPoint]; 

    C = [[MKPointAnnotation alloc] init]; 
    C.coordinate = CLLocationCoordinate2DMake(LatC, LongC); 
    C.title = [NSString stringWithFormat:@"C"]; 

    [MapViewHome addAnnotation:C]; 

    D = [[MKPointAnnotation alloc] init]; 
    D.coordinate = CLLocationCoordinate2DMake(LatD, LongD); 
    D.title = [NSString stringWithFormat:@"D"]; 

    [MapViewHome addAnnotation:D]; 



} 

现在我想在LoadMap函数中插入我的Dynamic NSMutableArray以刷新mapView并获得更长的路线!任何想法 ?

+0

我真的不了解你的问题 - 为什么你不能循环你的数组? –

+0

@NilsZiehn请给我一个例子! – Kingofmit

+0

你可以请说出你的问题实际上是什么?最后你会说“现在我想要在LoadMap函数中插入我的动态NSMutableArray以刷新mapView并获得更长的路线!任何想法?”从我在代码中看到的你已经在使用它。 –

这是我能想到的第一个解决方案...

首先我们包裹CLLocationCoordinate2D成一个对象。为此,我制作了一个名为KBLocationWrapper的包装类。这里的接口:

@interface KBLocationWrapper : NSObject 
@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@end 

下产生NSMutableArray ...

NSMutableArray *locationCoordinatesArray = [NSMutableArray array]; 

然后添加每个坐标通过对象包装的数组...

KBLocationWrapper *locationWrapper = [[KBLocationWrapper alloc] init]; 
locationWrapper.coordinate = CLLocationCoordinate2DMake(latitude, longitude); 
[locationCoordinatesArray addObject:locationWrapper]; 

最后,搞清楚如何将locationCoordinatesArray转换为-loadMap方法,然后遍历每个对象并将coordinate属性映射到它小号各地方在coordinateArray ...(我会写这个功能的单独的方法,但为了演示它会直接进入-loadMap

-(void)loadMap{ 

    .... 

    int Coordinates = (int)[locationCoordinatesArray count]; 

    CLLocationCoordinate2D coordinateArray[Coordinates]; 

    // loop through coordinates 
    for (int i = 0; i < Coordinates; ++i) { 
     // write data from the CLLocationCoordinate2D stored in the wrapper 
     // to the primitive data array 'coordinateArray' 
     coordinateArray[i] = [locationCoordinatesArray[i] coordinate]; 
    } 


    // then generate the routeLine. 
    self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:Coordinates]; 

    ... 

} 
+0

我完全按照你做的那样工作!但是当我添加2个以上坐标时,我无法从“Self.routeline”中看到第2条任何想法? – Kingofmit

+0

我可以看看你的代码吗? – jperl

+0

我会将它作为答案发布,因为我无法在此展示它! – Kingofmit

我对象添加到NSMutableArray的“NSLat”当我从另一个放松视图控制器:所提交

- (IBAction)UnwindPoiint:(UIStoryboardSegue *)segue { 


    float latitude; 
    float longitude; 
    NSString*PointName; 
    NSString*coordinates; 

    AddPointViewController *messageViewController = segue.sourceViewController; 
    PointName = messageViewController.PointBack; 
    latitude = [messageViewController.PointLatitude floatValue]; 
    longitude = [messageViewController.PointLongitude floatValue]; 



    KBLocationWrapper *locationWrapper = [[KBLocationWrapper alloc] init]; 
    locationWrapper.coordinate = CLLocationCoordinate2DMake(latitude, longitude); 
    [NSLat addObject:locationWrapper]; 

    coordinates = [NSString stringWithFormat:@"%f,%f,%@",latitude,longitude,PointName]; 

    // [NSLat addObject:coordinates]; 


    [self AddAnotations]; 
    [self loadMap]; 

    [FlightLogTable reloadData]; 


    NSLog(@"%@",coordinates); 
    NSLog(@"%lu",(unsigned long)NSLat.count); 


} 

我添加批注&负载地图:

-(void)AddAnotations{ 


     for(int idx = 0; idx < NSLat.count; idx++) 
     { 
      // break the string down even further to latitude and longitude fields. 
      NSString* currentPointString = [NSLat objectAtIndex:idx]; 
      NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]]; 

      CLLocationDegrees latitude = [[latLonArr objectAtIndex:0] doubleValue]; 
      CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue]; 
      NSString*Name = [NSString stringWithFormat:@"%@",[latLonArr objectAtIndex:2]]; 


      DeparturePoint = [[MKPointAnnotation alloc] init]; 
      DeparturePoint.coordinate = CLLocationCoordinate2DMake(latitude, longitude); 
      DeparturePoint.title = Name; 
      [MapViewHome addAnnotation:DeparturePoint]; 


     } 

     [self loadMap]; 

    } 

    -(void)zoomToFitMapAnnotations:(MKMapView*)mapView 
    { 
     if([mapView.annotations count] == 0) 
      return; 

     CLLocationCoordinate2D topLeftCoord; 
     topLeftCoord.latitude = -90; 
     topLeftCoord.longitude = 180; 

     CLLocationCoordinate2D bottomRightCoord; 
     bottomRightCoord.latitude = 90; 
     bottomRightCoord.longitude = -180; 



     for(MKPointAnnotation*annotation in MapViewHome.annotations) 
     { 
      topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude); 
      topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude); 

      bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude); 
      bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude); 
     } 

     MKCoordinateRegion region; 
     region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5; 
     region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5; 
     region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 2; // Add a little extra space on the sides 
     region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 2; // Add a little extra space on the sides 

     region = [mapView regionThatFits:region]; 
     [MapViewHome setRegion:region animated:YES]; 
    } 




    -(void)loadMap{ 

     int Coordinates = (int)[NSLat count]; 

    CLLocationCoordinate2D coordinateArray[Coordinates]; 

    // loop through coordinates 
    for (int i = 0; i < Coordinates; ++i) { 
     // write data from the CLLocationCoordinate2D stored in the wrapper 
     // to the primitive data array 'coordinateArray' 
     coordinateArray[i] = [NSLat[i] coordinate]; 
    } 


    // then generate the routeLine. 
    self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:Coordinates]; 

     [MapViewHome setVisibleMapRect:[self.routeLine boundingMapRect]]; //If you want the route to be visible 
     [MapViewHome addOverlay:self.routeLine]; 
     MapViewHome.mapType = MKMapTypeHybrid; 

     [self zoomToFitMapAnnotations:MapViewHome]; 

    }