GMSMarker最初保存userData,但在检索时为(null)

时间:2016-08-12 14:23:26

标签: ios objective-c google-maps parse-platform google-maps-sdk-ios

更新

存储点然后点击它时代码有效。问题是,当我关闭应用程序并重新启动它时,数据已经消失......重新启动应用程序后它的唯一空值。有什么想法吗?

我正在尝试将userData存储在GMSMarker中,然后将其检索以供稍后进行比较。这是我存储的代码:

int r = arc4random_uniform(99999999);

matched = [[NSNumber alloc] initWithBool:NO];

PFGeoPoint *point = [PFGeoPoint geoPointWithLatitude:mapView_.myLocation.coordinate.latitude longitude:mapView_.myLocation.coordinate.longitude];

PFObject *pointstore = [PFObject objectWithClassName:@"MapPoints"];
pointstore[@"title"] = [userdefaults objectForKey:@"pfuser"];
pointstore[@"location"] = point;
pointstore[@"count"] = stored;
pointstore[@"matched"] = matched;
pointstore[@"marker_id"] = [[NSString alloc] initWithFormat:@"%@", [NSNumber numberWithInteger:r]];

[pointstore saveEventually:^(BOOL succeeded, NSError *error) {

    if (error) {
         NSLog(@"Error");
    }
    else {
         GMSMarker *marker3 = [[GMSMarker alloc] init];
         marker3.position = mapView_.myLocation.coordinate;
         marker3.title = [userdefaults objectForKey:@"pfuser"];
         marker3.icon = [UIImage imageNamed:@"marker"];
         marker3.userData = @{@"marker_id":[[NSString alloc] initWithFormat:@"%d", r]};
         NSLog(@"****markerdata****");
         NSLog([[NSNumber numberWithInt:r] description]);
         marker3.map = mapView_;
         NSLog(@"****markerdataafter****");
         NSLog([marker3.userData description]);
     }
}];

最后一条日志消息markerdataafter是正确的,我认为这意味着数据已成功存储。

但是当我检索它时,使用以下代码:

- (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker {

    ...

    NSLog(@"****gettingtapped****");
    NSLog([marker.userData objectForKey:@"marker_id"]);

    ...

它以(null)的形式返回,因此找不到我存储的内容。这个让我困惑了几个小时......任何帮助都表示赞赏。我也想知道是否有更好的方法来处理marker_id

1 个答案:

答案 0 :(得分:0)

我犯了一个愚蠢的错误,就是在初始化时不将userData分配给标记。