简体   繁体   中英

How to add Coordinates to CLLocationCoordinate2D from Dynamic NSMutableArray?

I have a MapView in which I would like to add annotations and a route from defined coordinates that I add from a Textfield and store in an NSMutableArray.

Now I'm able to show the route from multiple coordinates but only when I insert them in my code as follow :

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

To Add Annotations I do this:

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



}

NOW I would like to get Insert my Dynamic NSMutableArray in the LoadMap function in order to refresh the mapView and get a longer Route ! Any Idea ?

Here's the first solution I could think of...

First we wrap the CLLocationCoordinate2D into an object. For this I've made a wrapper class called KBLocationWrapper . Here's the interface:

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

Next generate the NSMutableArray ...

NSMutableArray *locationCoordinatesArray = [NSMutableArray array];

Then add each coordinate to the array via the object wrapper...

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

Finally, figure out how you're going to get the locationCoordinatesArray into the -loadMap method, and then loop through each object and map the coordinate property to its respective place in coordinateArray ... (I would write a separate method for this functionality, but for demonstration purposes it's going straight into -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];

    ...

}

I add an object to NSMutableArray "NSLat" when i Unwind from another ViewController:

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


}

And I add Annotations & load Map as filed :

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM