【问题标题】:Stuck with ObjC and MapKit CoreLocation坚持使用 ObjC 和 MapKit CoreLocation
【发布时间】:2011-08-21 23:16:20
【问题描述】:

我一直在研究 ios 的 mapkit 和 corelocation。我现在只是想显示一张地图并放大我当前的位置。我可以用我的位置绘制地图。我可以得到我当前的位置。我只是无法将位置数据放入 map 方法中。我有点菜鸟,但我以为我知道我在做什么直到这一点! :) 任何帮助表示赞赏。我的代码在这里,这有点用,但我在大西洋中部的 0 长和 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];

}

【问题讨论】:

  • 你使用的是模拟器还是真机?
  • 我都试过了。结果完全相同,只有模拟器上的位置显示为 Cupertino。 iPhone上的位置显示伦敦。不知道如何将我当前的位置坐标放入地图中。
  • 对不起,可能只有我一个人,但我没有完全理解这个问题 - 您的位置显示在地图上,但您无法通过哪种地图方法获取位置?它在哪里返回 (0, 0)?
  • 抱歉来晚了。它放大到的位置是赤道穿过子午线的位置。即 0 lat 和 0 long。我想放大当前位置(伦敦)。在 viewDidLoad 中,我设置了一个 CLLocationCoorddinate2D 以从 mapView 中获取 userLocation.coordinate。但它正在返回一个空坐标。

标签: objective-c mapkit core-location


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 谢谢。做到了。我在 didUpdateToLocation 方法中更改了地图中心,它按预期工作。还删除了我的一个 insertSubviews。非常感谢
猜你喜欢
  • 2014-08-22
  • 1970-01-01
  • 2015-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多