Reputation: 184
The problem I'm having it that when I long touch the map it saves the data to Core Data and I can retrieve this data by NSLogs but I cannot figure out how to create multiple map markers from this data. Can anyone give me an example of a for loop for drawing these markers?
-(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];
}
}
Upvotes: 1
Views: 1452
Reputation: 184
After banging my head against the wall for hours and taking a few shots it finally came to me and was very simple. I stuck the data from core data in separate arrays and indexes the markers then indexed the arrays in a variable. Anyway here is the code. If anyone else has a better way of doing this please let me know. I am still learning Objective C and only started a couple months ago so there probably is a better solution but this at least works.
-(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];
// Alert style customization
[[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];
} else {
// this is where you would handle any actions for "Cancel"
}
}
- (void) saveMarker{
NSManagedObjectContext *context = [self managedObjectContext];
// Create a new managed object
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;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
[self fetchMarkers];
}
- (void) fetchMarkers {
// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Marker"];
self.markers = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
NSMutableArray *title = [self.markers valueForKey:@"Title"];
NSMutableArray *snippet = [self.markers valueForKey:@"Snippet"];
NSMutableArray *latitude = [self.markers valueForKey:@"Latitude"];
NSMutableArray *longitude = [self.markers valueForKey:@"Longitude"];
for (int i = 0; i < [title count]; i++){
GMSMarker *mkr = [[GMSMarker alloc] init];
double lat = [[latitude objectAtIndex:i] doubleValue];
double lng = [[longitude objectAtIndex:i] doubleValue];
NSString *T = [title objectAtIndex:i];
NSString *S = [snippet objectAtIndex:i];
[mkr setPosition:CLLocationCoordinate2DMake(lat, lng)];
[mkr setAnimated:YES];
[mkr setTitle:T];
[mkr setSnippet:S];
[mkr setMap:self.mapView1];
}
Upvotes: 1