简体   繁体   English

如何从Dynamic NSMutableArray将坐标添加到CLLocationCoordinate2D?

[英]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. 我有一个MapView,我想在其中添加注释和来自定义的坐标的路线,该坐标是从Textfield添加并存储在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 ! 现在,我想在LoadMap函数中插入我的Dynamic NSMutableArray,以刷新mapView并获得更长的Route! Any Idea ? 任何想法 ?

Here's the first solution I could think of... 这是我想到的第一个解决方案...

First we wrap the CLLocationCoordinate2D into an object. 首先,我们将CLLocationCoordinate2D包装到一个对象中。 For this I've made a wrapper class called KBLocationWrapper . 为此,我制作了一个名为KBLocationWrapper的包装器类。 Here's the interface: 这是界面:

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

Next generate the NSMutableArray ... 接下来生成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 ) 最后,找出如何你会得到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];

    ...

}

I add an object to NSMutableArray "NSLat" when i Unwind from another ViewController: 当我从另一个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);


}

And I add Annotations & load Map as filed : 我添加注释并加载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