我创建了一个按钮来显示用户的当前位置,但它无法正常工作。仅当我单击两次时才会显示用户的位置。第一次点击时,地图只显示蓝屏。
任何人都可以帮忙吗?
谢谢!!
#import "Mapa.h"
#import "MapViewAnnotation.h"
#import "CoreLocationController.h"
@implementation Mapa
@synthesize mapView;
@synthesize stringTitle;
@synthesize minhaL;
@synthesize myAnnotation;
@synthesize stringLat;
@synthesize stringLong;
@synthesize locationManager;
- (IBAction)showCurrentLocation {
mapView.userTrackingMode=YES;
mapView.showsUserLocation = YES;
self.mapView.centerCoordinate = mapView.userLocation.coordinate;
/*
MKCoordinateRegion Region = MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 1000,
1000);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:Region];
Region.center.latitude = mapView.userLocation.coordinate.latitude;
Region.center.longitude = mapView.userLocation.coordinate.longitude;
Region.span.longitudeDelta = 0.01f;
Region.span.latitudeDelta = 0.01f;
[mapView setRegion:adjustedRegion animated:YES];
*/
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBar1.png"] forBarMetrics:UIBarMetricsDefault];
CLLocationCoordinate2D location;
NSString *latitudeA = [NSString stringWithString:stringLat];
double valorLatitude = [latitudeA doubleValue];
NSString *longitudeA = [NSString stringWithString:stringLong];
double valorLongitude = [longitudeA doubleValue];
location.latitude = valorLatitude;
location.longitude = valorLongitude;
// Add the annotation to our map view
myAnnotation = [[MapViewAnnotation alloc] initWithTitle:stringTitle andCoordinate:location];
[self.mapView addAnnotation:myAnnotation];
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.02;
span.longitudeDelta=0.02;
region.span=span;
[mapView setRegion:region animated:YES];
[mapView regionThatFits:region];
[mapView setCenterCoordinate:myAnnotation.coordinate animated:YES];
}
- (void)locationManager: (CLLocationManager *)manager didFailWithError: (NSError *)error
{
[manager stopUpdatingLocation];
NSLog(@"error%@",error);
switch([error code])
{
case kCLErrorNetwork: // general, network-related error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"please check your network connection or that you are not in airplane mode" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
break;
case kCLErrorDenied:{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"user has denied to use current Location " delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
break;
default:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"unknown network error" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
break;
}
}
// Received memory warning
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
// If the view unloads, release the map view
- (void)viewDidUnload {
}
- (void)dealloc {
[stringLong release];
[stringLat release];
[myAnnotation release];
[minhaL release];
[stringTitle release];
[mapView release];
[super dealloc];
}
@end