1

我遇到的问题是,当我长时间触摸地图时,它会将数据保存到核心数据中,我可以通过 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];

}

}
4

1 回答 1

1

在把我的头撞在墙上几个小时并拍了几张之后,它终于来了,而且非常简单。我将核心数据中的数据保存在单独的数组中,并对标记进行索引,然后在变量中索引数组。无论如何,这里是代码。如果其他人有更好的方法,请告诉我。我仍在学习Objective C,几个月前才开始学习,所以可能有更好的解决方案,但这至少有效。

    -(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];

    }
于 2013-07-11T14:01:02.327 回答