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,它将执行更有用的操作mkusertrackingbarbuttoneim
,并将该按钮链接到您的地图。然后,它会根据用户按下的按钮来移动和旋转视图,如果用户滚动离开视图,它会自动关闭你的
locationManager:didUpdateLocation:fromLocation:
(ios5及以下版本)或locationManager:didUpdateLocations:
-你似乎没有在你发布的代码中的任何地方使用locationManager更新,据我所知,它也不存在。非常感谢。现在我修改了代码,按照第二个选项执行。非常感谢你的帮助!酷。因为这个答案解决了问题,你需要“接受”它,但是点击旁边的勾选/勾选,这样其他人就可以很容易地看到它是正确的。