gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:09:04 25 4
gpt4 key购买 nike

我创建了一个按钮来显示用户的当前位置,但它无法正常工作。仅当我单击两次时,用户的位置才会出现。我第一次点击时, 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com