被 ObjC 和 MapKit CoreLocation 困住了
我一直在研究ios 的mapkit 和corelocation。我现在只是想显示地图并放大我当前的位置。我可以绘制地图,标明我的位置。我可以获取我当前的位置。我只是无法将位置数据获取到地图方法中。我有点菜鸟,但到目前为止我以为我知道自己在做什么! :) 如有任何帮助,我们将不胜感激。我的代码在这里,它可以工作,但让我在大西洋中部 0 经度和 0 纬度!
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
[locationManager setPurpose:@"What use is a nav app if it doesn't know where you are?!?"];
[locationManager setDelegate:self ];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[locationManager setDistanceFilter:10];
[locationManager startUpdatingLocation];
mapView=[[MKMapView alloc] initWithFrame:self.view.bounds];
[self.view insertSubview:mapView atIndex:0];
mapView.showsUserLocation = TRUE;
mapView.mapType = MKMapTypeStandard;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location = mapView.userLocation.coordinate;
MKCoordinateRegion region;
region.span=span;
region.center=location;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
[self.view insertSubview:mapView atIndex:0];
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D here = newLocation.coordinate;
NSLog(@"%f %f", here.latitude, here.longitude);
[locationManager stopUpdatingLocation];
}
I've been getting into mapkit and corelocation for ios. I'm just trying to display a map and zoom in on my current location for now. I can draw the map, with my location. I can get my current location. I just can't get the location data into the map method. I'm a bit of a noob, but thought I knew what I was doing up until this point! :) Any help is appreciated. My code is here, which sort of works but plonks me in the middle of the Atlantic at 0 long and 0 lat!
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
[locationManager setPurpose:@"What use is a nav app if it doesn't know where you are?!?"];
[locationManager setDelegate:self ];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[locationManager setDistanceFilter:10];
[locationManager startUpdatingLocation];
mapView=[[MKMapView alloc] initWithFrame:self.view.bounds];
[self.view insertSubview:mapView atIndex:0];
mapView.showsUserLocation = TRUE;
mapView.mapType = MKMapTypeStandard;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location = mapView.userLocation.coordinate;
MKCoordinateRegion region;
region.span=span;
region.center=location;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
[self.view insertSubview:mapView atIndex:0];
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D here = newLocation.coordinate;
NSLog(@"%f %f", here.latitude, here.longitude);
[locationManager stopUpdatingLocation];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
showsUserLocation
设置为 YES 后,地图视图userLocation
通常不会立即可用。实现地图视图的委托方法
didUpdateUserLocation
和didFailToLocateUserWithError
。您需要设置地图视图的delegate
属性,否则它们将不会被调用。在didUpdateUserLocation
方法中更改地图的区域或中心,而不是在 viewDidLoad 中。或者,在您已实现的 CLLocationManager 的
didUpdateToLocation
中设置区域。它会被调用吗?还实现其didFailWithError
方法并查看它是否被调用。如果您使用 MKMapView,则只需使用showsUserLocation 或CLLocationManager(而不是两者)即可获取当前位置。但要获得蓝色弹珠,您需要使用showsUserLocation。
另外,为什么你要在mapView上执行两次insertSubview?
The map view
userLocation
will not usually be available right after settingshowsUserLocation
to YES.Implement the map view's delegate methods
didUpdateUserLocation
anddidFailToLocateUserWithError
. You'll need to set the map view'sdelegate
property or they won't get called. Change the map's region or center in thedidUpdateUserLocation
method--not in viewDidLoad.Or, set the region in the CLLocationManager's
didUpdateToLocation
which you've implemented. Does it get called? Also implement itsdidFailWithError
method and see if it gets called.You only need to use either showsUserLocation or the CLLocationManager, not both, to get the current location if you're using MKMapView. But to get the blue marble, you need to use showsUserLocation.
Also, why are you doing insertSubview twice on mapView?