【发布时间】: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