2017-05-30 170 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]; }