在 Objective C 中显示 MapKit "Map loading error"

Showing MapKit "Map loading error" in Objective C

我正在使用 objective c 使用 Mapkit,我想在地图上显示 "Error alert"。 当互联网工作时,它工作正常但当互联网不能正常工作时,它会自动显示在日志 "request time out" 上,而无需调用“mapViewDidFailLoadingMap”委托方法。

代码在这里:

- (void)viewDidLoad 
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    mapView.delegate=self;
    mapView.showsUserLocation = YES;
    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];

    //[self activeCLLocation];
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"user latitude==>%f",userLocation.location.coordinate.latitude);
    NSLog(@"user longitude==>%f",userLocation.location.coordinate.longitude);
}


-(void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
    NSLog(@"error map===>%@",error.description);
}

-(void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"error loc===>%@",error.description);
}

显示错误:

2015-05-27 12:51:57.624 EventLocator[1262:160081] Could not determine current country code: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x19d80290 {NSErrorFailingURLStringKey=http://gsp1.apple.com/pep/gcc, NSErrorFailingURLKey=http://gsp1.apple.com/pep/gcc, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x17e877a0 "The request timed out."}

我会在您加载 mapKit 之前测试有效的互联网连接。查看此线程以了解如何执行此操作。

How to check for an active Internet connection on iOS or OSX?

There is no issue in the code. I guess the simulator internally is not able to connect to the internet. Try connecting to some other wifi connection and test internet connection before you load MapView.

检查互联网连接为:

#import <SystemConfiguration/SCNetworkReachability.h>


+(bool)isNetworkAvailable
{
   SCNetworkReachabilityFlags flags;
   SCNetworkReachabilityRef address;
   address = SCNetworkReachabilityCreateWithName(NULL, "www.apple.com" );
   Boolean success = SCNetworkReachabilityGetFlags(address, &flags);
   CFRelease(address);

bool canReach = success
                && !(flags & kSCNetworkReachabilityFlagsConnectionRequired)
                && (flags & kSCNetworkReachabilityFlagsReachable);

   return canReach;
}

您也可以尝试将模拟器重置为

iOS 模拟器 -> 重置内容和设置