-1

我在完成这项工作时遇到问题,我使用核心数据来获取用户列表,

NSFetchrequets 额外的用户位置,地点列表和邮政编码

图片

NSFetchRequest *request =[NSFetchRequest fetchRequestWithEntityName:@"SiteLocation"]; //request all objects
NSArray *fetchedObjects = [self.srmDatabase.managedObjectContext executeFetchRequest:request error:nil];

所以我有一个实体“SiteLocation”用 FetchedResultController 加载 tableview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     //NSLog(@"CELL");
    static NSString *CellIdentifier = @"Site Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    SiteLocation * siteLocation = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = siteLocation.siteName;

    return cell;
}

然后我将选定的行发送到 MapViewController didSelectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
   NSLog(@"didselect sitePC");

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {

    UserDetails *userinfo = [[self fetchedResultsController] objectAtIndexPath:indexPath];
    self.mapview.sitePC = userinfo; //sitePC is (id)
    NSLog(@"object selected= %@", userinfo);
} 
}

2012-10-04 13:27:46.401 SRMAB[459:c07] didselect sitePC = <SiteLocation: 0x1d80ad20> (entity: SiteLocation; id: 0x1d86b210 <x-coredata://523E1EB6-01DF-4889-B1A1-2E38E92D385E/SiteLocation/p120> ; data: {
    projectName =     (
        "0x1d82bc20 <x-coredata://523E1EB6-01DF-4889-B1A1-2E38E92D385E/UserDetails/p144>"
    );
    siteName = "Bloomberg Place 1";
    sitePostCode = "W1B 5AU";
})

在 MapViewController 上,这会将 sitePostcode 转换为 PlaceMark

-(void)myMapview
{
    NSLog(@"mymapview");

    NSString *addressString = [self.sitePC valueForKey:@"sitePostCode"];

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder geocodeAddressString:addressString completionHandler:^(NSArray *placemarks, NSError *anError)
     {
         NSLog(@"Placemark count:%d",[placemarks count]);

         for(CLPlacemark *placemark1 in placemarks)
         {
             NSLog(@"Placemark: %@",placemark1);
             //[self displayPlacemarks:placemarks];
             CLLocationCoordinate2D zoomLocation;
             zoomLocation.latitude = placemark1.location.coordinate.latitude;
             zoomLocation.longitude= placemark1.location.coordinate.longitude;

             MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 1000, 1000);
             MKCoordinateRegion adjustedRegion = [self.mapview regionThatFits:viewRegion];
             [self.mapview setRegion:adjustedRegion animated:YES];

             MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
             pa.coordinate = placemark1.location.coordinate;
             pa.title = [self.sitePC valueForKey:@"siteName"];
             [self.mapview addAnnotation:pa];
         }

我希望这是有道理的。

加载时如何在地图上显示所有位置?

问题已更新,因为原始问题是拼写错误

4

1 回答 1

1

reason: '[<SiteLocation 0x1d80ad20> valueForUndefinedKey:]: the entity SiteLocation is not key value coding-compliant for the key "sitePostcode".'

@"sitePostcode" 与 @"sitePostCode" 不同。您在调用-valueForKeyin时使用前者-myMapView,但您的 Core Data 模型图片显示的是后者。

更新:要在地图上显示位置,请使用地图注释或叠加。例如,您可以MKPinAnnotation在地图中添加一个以指示地图上的位置。有关详细信息,请参阅注释地图。我看到你已经在使用了MKPointAnnotation,所以不清楚你的问题是什么。也许您正试图一次显示所有注释 - 如果这是问题所在,您只需要计算限制所有位置的矩形并缩放地图以包含该区域。

于 2012-10-04T13:35:12.620 回答