2017-05-30 183 views
0

代碼位於此處。 它基本上可行,但我迷失了方向,無法弄清楚如何從[geocoder]例程返回「point.title」。 point.title是Mapkit註釋事物的一部分。我可以像已經註釋過的那樣,將一個簡單的字符串加入到point.title中,但是我無法做到的是返回完整的'locatedAT'或任何placemark.subLocality,例如。項目。我無法從例程中獲得位於座標位置的座標位置

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { 

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800); 
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES]; 

// Add an annotation 
MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; 
point.coordinate = userLocation.coordinate; 
//point.title = @"Where am I?"; 
//point.subtitle = @"I'm here!!!"; 
//////////////////////// 
CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
CLLocation *loc = [[CLLocation alloc]initWithLatitude:userLocation.coordinate.latitude longitude:userLocation.coordinate.longitude]; 

[geocoder reverseGeocodeLocation: loc 
       completionHandler:^(NSArray* placemarks, NSError* error){ 
        CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
        if (placemark) { 

         // NSLog(@"placemark %@",placemark); 
         //String to hold address 
         NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "]; 
         // NSLog(@"addressDictionary %@", placemark.addressDictionary); 

        // NSLog(@"placemark %@",placemark.region); 
        // NSLog(@"placemark %@",placemark.country); // Give Country Name 
        // NSLog(@"placemark %@",placemark.locality); // Extract the city name 

        // NSLog(@"location %@",placemark.name); 
        // NSLog(@"location %@",placemark.ocean); 
        // NSLog(@"location %@",placemark.postalCode); 
        // NSLog(@"location %@",placemark.subLocality); 

         //Print the location to console 
        // NSLog(@"I am currently at %@",locatedAt); 

         point.title = locatedAt; 

        } 
        else { 
         NSLog(@"Could not locate"); 
        } 
} 
]; 
/////////////////////////// 

NSLog(@"point.title %@",point.title); 

[self.mapView addAnnotation:point]; 
[self.mapView selectAnnotation:point animated:YES]; } 
+0

請您告訴我您在這裏遇到什麼錯誤或問題? –

+0

你想從反向地理編碼中獲得什麼? –

+0

我想把locateAt字符串放到point.title或point.subtitle變量中。它是在地理編碼器例程中生成的。 – Postmaster

回答

0

我通過簡單地添加/修改以下內容來獲得我的代碼。我看不出它爲什麼會起作用,因爲point.title = locatedAt會覆蓋最後的point.title = @「我在這裏」;那是在大括號之外。因爲如果我取出這兩個point.title和point.sustitle行,它不再打印引腳上的實際地理位置。

     //Print the location to console 
         //NSLog(@"I am currently at %@",locatedAt); 


         point.title = locatedAt; 

        } 
        else { 
         NSLog(@"Could not locate"); 
        } 

    } 
]; 
point.title = @"Where am I"; 
    // NSLog(@"point.title %@",point.title); 
point.subtitle = @"I'm here!!!"; 

[self.mapView addAnnotation:point]; 
[self.mapView selectAnnotation:point animated:YES]; }