坚持使用ObjC和MapKit CoreLocation

时间:2011-08-21 23:16:20

标签: objective-c mapkit core-location

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

}

1 个答案:

答案 0 :(得分:1)

userLocation设置为YES后,通常无法使用地图视图showsUserLocation

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

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

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

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