在我的应用中,我使用了核心位置& Mapkit框架与mapview。当我们安装应用程序时,默认情况下它会向我显示“想要使用当前位置”的警报,而不进行一次编码。如果我选择“不允许”,地图视图只显示蓝色背景?如果我选择“确定”,那么它可以正常工作。
帮助我!
我的代码如下:
Appdelegate.m
CLLocationManager *locationManager;
CLLocation *userLocation;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
[self createEditableCopyOfDatabaseIfNeeded];
//=========================================Location Manager
if(locationManager == nil)
locationManager =[[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy= kCLLocationAccuracyBest;
self.locationManager.distanceFilter= 5;
[locationManager startUpdatingLocation];
[locationManager startUpdatingHeading];
//==========================================
}
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSLog(@"new location");
self.userLocation=newLocation;
NSLog(@"user llocation %f , %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
[self.locationManager startMonitoringSignificantLocationChanges];
}
Mapview.h
{
IBOutlet MKMapView *map;
CLLocation *currentLocation;
NSString *specificLatitude;
NSString *specificLongitude;
MKCoordinateRegion region;
}
Mapview.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[APPDELEGATE.locationManager startUpdatingLocation];
}
- (void)viewWillAppear:(BOOL)animated
{
self.map.delegate = self;
// Ensure that you can view your own location in the map view.
[self.map setShowsUserLocation:YES];
}
-(void)viewDidAppear:(BOOL)animated{
currentLocation =APPDELEGATE.userLocation;
region.center = self.currentLocation.coordinate;
MKCoordinateSpan span;
span.latitudeDelta = 0.05;
span.longitudeDelta = 0.03;
region.span = span;
[map setRegion:region animated:TRUE];
[self searchPressed];
NSLog(@"Mapview %f %f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude
);
}
答案 0 :(得分:0)
如果您选择“不允许”,请调用此委托。
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
//Do something
}
答案 1 :(得分:0)
我没试过这个,但也许你可以测试一下它是否有效。
对于显示地图视图的视图控制器,请尝试符合UIAlertViewDelegate。
然后在警报视图委托回调方法中,如果有效,你可以做任何你想做的事情:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// you need to type the exact title of the alert popup
// button index starts from 0 (left most button), so if the are 2 buttons
// and "Don't allow" button is on the right, the button == 1 is that button
// the user tapped on
if([alertView.title isEqualToString:@"Type Exact Alert Title Here"] && buttonIndex == 1)
{
// do something
}
}