我是 iOS 新手,正在研究谷歌地图。
我想根据键值将我的旅行路线图(带折线)保存到我的核心数据中。
使用 till 进行保存的代码(来自 runkeeper 应用程序)
- (void)saveRun
{
Run *newRun = [NSEntityDescription insertNewObjectForEntityForName:@"Run"
inManagedObjectContext:self.managedObjectContext];
newRun.distance = [NSNumber numberWithFloat:self.distance];
newRun.duration = [NSNumber numberWithInt:self.seconds];
newRun.timestamp = [NSDate date];
NSMutableArray *locationArray = [NSMutableArray array];
for (CLLocation *location in self.locations) {
Location *locationObject = [NSEntityDescription insertNewObjectForEntityForName:@"Location"
inManagedObjectContext:self.managedObjectContext];
locationObject.timestamp = location.timestamp;
locationObject.latitude = [NSNumber numberWithDouble:location.coordinate.latitude];
locationObject.longitude = [NSNumber numberWithDouble:location.coordinate.longitude];
[locationArray addObject:locationObject];
}
newRun.locations = [NSOrderedSet orderedSetWithArray:locationArray];
self.run = newRun;
// Save the context.
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
然后在我想根据键值获取折线详细信息并显示在 MapView 上之后。
用于获取
- (MKCoordinateRegion)mapRegion
{
MKCoordinateRegion region;
Location *initialLoc = self.run.locations.firstObject;
float minLat = initialLoc.latitude.floatValue;
float minLng = initialLoc.longitude.floatValue;
float maxLat = initialLoc.latitude.floatValue;
float maxLng = initialLoc.longitude.floatValue;
for (Location *location in self.run.locations) {
if (location.latitude.floatValue < minLat) {
minLat = location.latitude.floatValue;
}
if (location.longitude.floatValue < minLng) {
minLng = location.longitude.floatValue;
}
if (location.latitude.floatValue > maxLat) {
maxLat = location.latitude.floatValue;
}
if (location.longitude.floatValue > maxLng) {
maxLng = location.longitude.floatValue;
}
}
region.center.latitude = (minLat + maxLat) / 2.0f;
region.center.longitude = (minLng + maxLng) / 2.0f;
region.span.latitudeDelta = (maxLat - minLat) * 1.1f; // 10% padding
region.span.longitudeDelta = (maxLng - minLng) * 1.1f; // 10% padding
return region;
}
- (MKPolyline *)polyLine {
CLLocationCoordinate2D coords[self.run.locations.count];
for (int i = 0; i < self.run.locations.count; i++) {
Location *location = [self.run.locations objectAtIndex:i];
coords[i] = CLLocationCoordinate2DMake(location.latitude.doubleValue,location.longitude.doubleValue);
}
return [MKPolyline polylineWithCoordinates:coords count:self.run.locations.count];
}
- (void)loadMap
{
if (self.run.locations.count > 0) {
self.mapView.hidden = NO;
// set the map bounds
[self.mapView setRegion:[self mapRegion]];
// make the line(s!) on the map
NSArray *colorSegmentArray = [MathController colorSegmentsForLocations:self.run.locations.array];
[self.mapView addOverlays:colorSegmentArray];
//[self.mapView addOverlay:[self polyLine]];
[self.mapView addAnnotations:[[BadgeController defaultController] annotationsForRun:self.run]];
} else {
// no locations were found!
self.mapView.hidden = YES;
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"Sorry, this run has no locations saved."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
主要是使用键值保存路径并仅使用该键值获取。
任何人都可以帮助我。