在当前用户位置iphone mkmapview放置引脚

时间:2012-07-24 04:16:46

标签: ios mkmapview coordinates

好的,这是我尝试使用CLLoactionManager

- (void)viewDidLoad {

    [super viewDidLoad];
    mapView=[[MKMapView alloc] initWithFrame:self.view.frame];
    //mapView.showsUserLocation=TRUE;
    mapView.delegate=self;
    [self.view insertSubview:mapView atIndex:0];


    CLLocationManager *locationManager=[[CLLocationManager alloc] init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;

    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    mStoreLocationButton.hidden=FALSE;
    location=newLocation.coordinate;
    //One location is obtained.. just zoom to that location

    MKCoordinateRegion region;
    region.center=location;
    MKCoordinateSpan span;
    span.latitudeDelta=0.01;
    span.longitudeDelta=0.01;
    region.span=span;

    [mapView setRegion:region animated:TRUE];    
}

我的问题是[locationManager startUpdatingLocation];似乎没有触发下一个方法。我错过了什么?我已尝试在第二种方法中设置断点,但它们从未捕获过。显然它没有被使用。

2 个答案:

答案 0 :(得分:3)

您应该考虑使用CLLocationManager来获取当前位置以使用正确的坐标为MKMapView提供信息。

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
MKMapView *map = [[MKMapView alloc] init];

[locationManager startUpdatingLocation];

CLLocationCoordinate2D _coordinate = locationManager.location.coordinate;
MKCoordinateRegion extentsRegion = MKCoordinateRegionMakeWithDistance(_coordinate, 800, 800); 

[map setRegion:extentsRegion animated:YES];

答案 1 :(得分:0)

This看起来是个好地方。要放下引脚,您需要知道坐标。使用- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation可以帮助您获取用户的位置,以便您可以创建MKAnnotationMKPinAnnotationViews。一旦开始,它就非常直接。