【问题标题】:Retrieving Position, Title, and Snippet for Multiple Markers Using Google Maps for iOS使用 iOS 版 Google 地图检索多个标记的位置、标题和片段
【发布时间】:2013-07-09 22:40:54
【问题描述】:

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

}

}

【问题讨论】:

    标签: iphone ios google-maps ios6 google-maps-sdk-ios


    【解决方案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-09-28
      • 2015-11-27
      • 2018-08-10
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      相关资源
      最近更新 更多