根據經緯度反向地理編譯出地址信息(如果報錯:Error Domain=kCLErrorDomain Code=8 "(null)")


注意:Error Domain=kCLErrorDomain Code=8 "(null)" 如果出現這個錯誤  一定是 經緯度有問題   一定是 經緯度有問題 一定是 經緯度有問題

- (void)reverseGeocoder{
    //創建地理編碼對象
    CLGeocoder *geocoder=[[CLGeocoder alloc]init];
    //經度
    NSString *longitude = @"116.4582890000";
    //緯度
    NSString *latitude = @"39.9127500000";
    //創建位置
    CLLocation *location=[[CLLocation alloc]initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
    //根據經緯度反向地理編譯出地址信息
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *array, NSError *error){
        if (array.count > 0){
            CLPlacemark *placemark = [array objectAtIndex:0];
            //獲取城市
            NSString *city = placemark.locality;
            if (!city) {
                //四大直轄市的城市信息無法通過locality獲得,只能通過獲取省份的方法來獲得(如果city為空,則可知為直轄市)
                city = placemark.administrativeArea;
            }
            NSLog(@"city = %@", city);//沈陽市
            NSLog(@"--%@",placemark.name);//黃河大道21號
            NSLog(@"++++%@",placemark.subLocality); //渾南區
            NSLog(@"country == %@",placemark.country);//中國
            NSLog(@"administrativeArea == %@",placemark.administrativeArea); //遼寧省
            NSString *addressStr = [NSString stringWithFormat:@"%@ %@ %@ %@",placemark.administrativeArea,city,placemark.subLocality,placemark.name];
            NSLog(@"administrativeArea == %@",addressStr);
        }
        else if (error == nil && [array count] == 0)
        {
            NSLog(@"No results were returned.");
        }
        else if (error != nil)
        {
            NSLog(@"An error occurred = %@", error);
        }
    }];
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM