被 ObjC 和 MapKit CoreLocation 困住了

发布于 2024-11-30 17:11:58 字数 1473 浏览 1 评论 0原文

我一直在研究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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走就滚别墨迹 2024-12-07 17:11:58

showsUserLocation 设置为 YES 后,地图视图 userLocation 通常不会立即可用。

实现地图视图的委托方法 didUpdateUserLocationdidFailToLocateUserWithError。您需要设置地图视图的 delegate 属性,否则它们将不会被调用。在 didUpdateUserLocation 方法中更改地图的区域或中心,而不是在 viewDidLoad 中。

或者,在您已实现的 CLLocationManager 的 didUpdateToLocation 中设置区域。它会被调用吗?还实现其 didFailWithError 方法并查看它是否被调用。

如果您使用 MKMapView,则只需使用showsUserLocation 或CLLocationManager(而不是两者)即可获取当前位置。但要获得蓝色弹珠,您需要使用showsUserLocation。

另外,为什么你要在mapView上执行两次insertSubview?

The map view userLocation will not usually be available right after setting showsUserLocation to YES.

Implement the map view's delegate methods didUpdateUserLocation and didFailToLocateUserWithError. You'll need to set the map view's delegate property or they won't get called. Change the map's region or center in the didUpdateUserLocation method--not in viewDidLoad.

Or, set the region in the CLLocationManager's didUpdateToLocation which you've implemented. Does it get called? Also implement its didFailWithError 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文