如何从 Dynamic NSMutableArray 添加坐标到 CLLocationCoordinate2D?
How to add Coordinates to CLLocationCoordinate2D from Dynamic NSMutableArray?
我有一个 MapView,我想在其中添加注释和一条来自我从文本字段添加并存储在 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 并获得更长的路线!有什么想法吗?
这是我能想到的第一个解决方案...
首先我们将 CLLocationCoordinate2D
包装到一个对象中。为此,我制作了一个名为 KBLocationWrapper
的包装器 class。这是界面:
@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];
...
}
当我从另一个 Unwind 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);
}
然后我添加注释并按照提交的方式加载地图:
-(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];
}
我有一个 MapView,我想在其中添加注释和一条来自我从文本字段添加并存储在 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 并获得更长的路线!有什么想法吗?
这是我能想到的第一个解决方案...
首先我们将 CLLocationCoordinate2D
包装到一个对象中。为此,我制作了一个名为 KBLocationWrapper
的包装器 class。这是界面:
@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];
...
}
当我从另一个 Unwind 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);
}
然后我添加注释并按照提交的方式加载地图:
-(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];
}