我遇到的问题是,当我长时间触摸地图时,它会将数据保存到核心数据中,我可以通过 NSLogs 检索这些数据,但我无法弄清楚如何从这些数据中创建多个地图标记。谁能给我一个用于绘制这些标记的for循环示例?
-(void) mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate{
location = coordinate;
[self alertview1];
}
- (void) alertview1 {
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Save Map Location" message:@"Enter Title & Description" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[av setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[[av textFieldAtIndex:1] setSecureTextEntry:NO];
[[av textFieldAtIndex:0] setPlaceholder:@"Title"];
[[av textFieldAtIndex:1] setPlaceholder:@"Description"];
[av show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex != alertView.cancelButtonIndex) {
markerTitle = [alertView textFieldAtIndex:0].text;
markerSnippet = [alertView textFieldAtIndex:1].text;
NSLog(@"1 %@", [alertView textFieldAtIndex:0].text);
NSLog(@"2 %@", [alertView textFieldAtIndex:1].text);
[self saveMarker];
}
- (void) saveMarker{
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Marker" inManagedObjectContext:context];
[newDevice setValue:[NSNumber numberWithDouble:location.latitude] forKey:@"latitude"];
[newDevice setValue:[NSNumber numberWithDouble:location.longitude] forKey:@"longitude"];
[newDevice setValue:markerTitle forKey:@"title"];
[newDevice setValue:markerSnippet forKey:@"snippet"];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
[self fetchMarkers];
}
- (void) fetchMarkers {
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Marker"];
self.markers = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
NSArray *title = [self.markers valueForKey:@"Title"];
NSArray *snippet = [self.markers valueForKey:@"Snippet"];
NSArray *latitude = [self.markers valueForKey:@"Latitude"];
NSArray *longitude = [self.markers valueForKey:@"Longitude"];
NSLog (@"%@", title);
NSLog (@"%@", snippet);
NSLog (@"%@", latitude);
NSLog (@"%@", longitude);
double lat = [latitude doubleValue];
double lng = [longitude doubleValue];
for (GMSMarker *marker in title) {
GMSMarker *mkr = [[GMSMarker alloc] init];
[mkr setPosition:CLLocationCoordinate2DMake(lat,lng)];
[mkr setAnimated:YES];
[mkr setTitle:title];
[mkr setSnippet:snippet];
[mkr setMap:self.mapView1];
}
}