ios - 需要点击才能显示当前位置

标签 ios dictionary click mkmapview

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

有人可以帮忙吗?

谢谢!!

#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

最佳答案

有多种方法可以使 map View 以用户为中心,您似乎做了其中三种,但都错了。

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

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

  3. mapView.userTrackingMode=YES 很糟糕。跟踪模式不是 bool 值,将其设置为 valid value它会做一些更有用的事情

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

  1. 如果您想显示用户的蓝点并偶尔让他们重新回到该位置的中心,请启用showUserLocation,然后在按下按钮时从 map 中获取用户的位置。

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

  3. 如果您希望标准 map 按钮能够循环显示用户、跟踪用户以及转向他们面对的方向,那么添加一个 MKUserTrackingBarButtonItem 并将该按钮链接到您的 map 。然后,它负责移动 View 并根据用户按下的内容旋转 View ,如果用户滚动离开它,它会自行关闭。

关于ios - 需要点击才能显示当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14694000/

相关文章:

ios - 如何从 swift 类到 Objective C 类访问变量

JavaScript 单击图像按钮

javascript - 如何在 jQuery 中重新计算单击时的变量?

java - Java 中二维数组的替代方案

Python:迭代两个以列表为值的字典

python - 如何避免两次写入 request.GET.get() 以打印它?

javascript - 错误选择器上的 jQuery 单击函数

ios - UILabel swift 的圆角

ios - 不同 View Controller 的固定导航栏

ios - 从 UIImage 检索数据时图像尺寸会增加