2011-08-21 81 views
0

我已经进入了ios的mapkit和corelocation。我只是试图显示地图并放大当前位置。我可以用我的位置绘制地图。我可以得到我目前的位置。我无法将位置数据导入地图方法。我有点小事,但认为我知道我在做什么,直到这一点! :)任何帮助表示赞赏。我的代码在这里,哪些作品,但在大西洋中间,0和0 lat!与ObjC和MapKit CoreLocation卡在一起

- (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]; 

} 
+0

您是使用模拟器还是实际的设备? –

+0

我在两者上都试过。完全相同的结果,仅在模拟器上的位置显示为Cupertino。 iPhone上的位置显示伦敦。不知道如何让我的当前位置坐标进入地图。 – Hoppo

+0

对不起,也许这只是我,但我完全不明白这个问题 - 你的位置显示在地图上,但你不能得到地图的方法?它在哪里返回(0,0)? – fbrozovic

回答

1

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

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

或者,在您实施的CLLocationManager的didUpdateToLocation中设置该区域。它会被叫吗?也执行它的didFailWithError方法,看看它是否被调用。

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

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

+0

谢谢。那样做了。我更改了didUpdateToLocation方法中的地图中心,并按预期工作。还删除了我的一个insertSubviews。非常感谢 – Hoppo