簡體   English   中英

如何從Dynamic NSMutableArray將坐標添加到CLLocationCoordinate2D?

[英]How to add Coordinates to CLLocationCoordinate2D from Dynamic NSMutableArray?

我有一個MapView,我想在其中添加注釋和來自定義的坐標的路線,該坐標是從Textfield添加並存儲在NSMutableArray中的。

現在,我可以顯示來自多個坐標的路線,但是只有在將它們按如下方式插入到我的代碼中時:

-(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並獲得更長的Route! 任何想法 ?

這是我想到的第一個解決方案...

首先,我們將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];

    ...

}

當我從另一個ViewController展開時,我向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);


}

我添加注釋並加載Map歸檔:

 -(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];

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM