2012-10-04 195 views
-1

我有問題得到這個工作,用核心數據,來獲取用戶的名單我,核心數據和CLGeocoder

NSFetchrequets額外的用戶位置,地方名單和郵編

Image

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

所以我有一個實體 「SITELOCATION」 裝載的tableview與FetchedResultController

- (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轉換爲標

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

我希望這是有意義的。

如何在加載時顯示地圖上的所有位置?

問題,因爲原來的問題是更新一個拼寫錯誤

回答

1

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

@ 「sitePostcode」 是不一樣的@ 「sitePostCode」。您可以在-myMapView-valueForKey調用中使用前者,但Core Data模型的圖片顯示後者。

更新:要顯示地圖上的位置,請使用地圖註記或覆蓋圖。例如,您可以將MKPinAnnotation添加到地圖以指示地圖上的位置。有關更多信息,請參閱Annotating Maps。我看到你已經在使用MKPointAnnotation,所以目前還不清楚你的問題是什麼。也許你試圖一次顯示所有的註釋 - 如果這是問題,那麼你只需要計算矩形來限制所有的位置,並縮放地圖以包含該區域。

+0

Haaaaaaaaaaaaa兩天在這工作... 30分鐘寫這個問題都浪費了..... aaaa謝謝,我可以問別的東西.. – HernandoZ

+0

問題編輯 - 如何顯示地圖上的所有位置,當它加載。 – HernandoZ

+0

我會更新我的答案,但是最好創建一個單獨的問題,而不是完全改變現有問題的焦點。 – Caleb