Ios 需要单击以显示当前位置

Ios 需要单击以显示当前位置,ios,map,click,mkmapview,Ios,Map,Click,Mkmapview,我创建了一个按钮来显示用户的当前位置,但它不能正常工作。 用户的位置只有在我单击两次后才会显示。我第一次单击时,地图上只显示一个蓝色屏幕 有人能帮忙吗 谢谢 #import "Mapa.h" #import "MapViewAnnotation.h" #import "CoreLocationController.h" @implementation Mapa @synthesize mapView; @synthesize stringTitle; @synthesize minh

我创建了一个按钮来显示用户的当前位置,但它不能正常工作。 用户的位置只有在我单击两次后才会显示。我第一次单击时,地图上只显示一个蓝色屏幕

有人能帮忙吗

谢谢

#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

有多种方法可以让地图视图以用户为中心,您似乎做了其中三种,但都错了

  • showCurrentLocation
    中,您打开地图上的用户位置,然后在一微秒后向其询问位置并尝试将其设置为您的中心,但您没有给它时间

  • viewDidLoad
    中,您打开位置管理器,但尚未实现
    didUpdateToLocation
    ,这是在接收到位置信息时调用的方法

  • mapView.userTrackingMode=YES
    不正确。跟踪模式不是布尔值,将其设置为a,它将执行更有用的操作

  • 下面是如何使用它们的摘要

  • 如果您想显示用户的蓝点,并偶尔让他们回到该位置的中心,请打开showsUserLocation,并在按下按钮时从地图中获取用户的位置

  • 如果只想在按下按钮时转到用户的位置,而不需要在其余时间显示其位置,请启动位置管理器并实现缺少的委托方法。每次接收到有效的位置数据时,将其保存在某个位置,当按下按钮时,您可以调用该数据并将其设置为中心

  • 如果您需要标准地图按钮,该按钮将循环显示用户、跟踪用户和转向他们面对的方式,然后添加一个
    mkusertrackingbarbuttoneim
    ,并将该按钮链接到您的地图。然后,它会根据用户按下的按钮来移动和旋转视图,如果用户滚动离开视图,它会自动关闭


  • 你的
    locationManager:didUpdateLocation:fromLocation:
    (ios5及以下版本)或
    locationManager:didUpdateLocations:
    -你似乎没有在你发布的代码中的任何地方使用locationManager更新,据我所知,它也不存在。非常感谢。现在我修改了代码,按照第二个选项执行。非常感谢你的帮助!酷。因为这个答案解决了问题,你需要“接受”它,但是点击旁边的勾选/勾选,这样其他人就可以很容易地看到它是正确的。