0

我创建了一个按钮来显示用户的当前位置,但它无法正常工作。仅当我单击两次时才会显示用户的位置。第一次点击时,地图只显示蓝屏。

任何人都可以帮忙吗?

谢谢!!

#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
4

1 回答 1

1

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

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

  2. viewDidLoad您打开位置管理器但您尚未实现didUpdateToLocation接收位置信息时调用的方法。

  3. mapView.userTrackingMode=YES不好。跟踪模式不是布尔值,将其设置为有效值,它会做一些更有用的事情

以下是如何使用它们的摘要

  1. 如果您想显示用户的蓝点并偶尔让他们以该位置为中心,请打开 showUserLocation 并在按下按钮时从地图中获取用户的位置。

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

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

于 2013-02-05T00:13:21.403 回答