在MKMapView中滚动我当前的位置

时间:2014-08-14 15:34:46

标签: ios location mkmapview

按照在线教程,我制作了一个MKMapView,但是我遇到了一些问题:

第一个问题:地图显示我当前的位置,但如果我尝试滚动该地图它将自动返回到我的位置,因此,例如,我无法看到附近的街道,因为地图将重新回到我目前的职位。

第二个问题:我想实施一些"捏缩放"就像你点击我的地图一样,它将获得该点的经度和纬度。

我肯定搞砸了我的代码,因为我将两个教程混合在一起...试图将我需要的东西归档到我的#34; app"。 : - )

这是我的.h:

   @interface mappa2 : UIViewController <CLLocationManagerDelegate> {

        MKMapView *mapview;
        CLLocationManager *manager;
        CLGeocoder *geocoder;
        CLPlacemark *placemark;

    } 

@property (nonatomic, retain) IBOutlet MKMapView *mapview;

这是我凌乱的.m

@synthesize mapview;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Procedura per TAP:

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(foundTap:)];

    tapRecognizer.numberOfTapsRequired = 1;
    tapRecognizer.numberOfTouchesRequired = 1;
    [self.mapview addGestureRecognizer:tapRecognizer];     

    [self startMap]; }


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {

    NSLog(@"Errore %@",error);
    NSLog(@"Non ho potuto calcolare la posizione");

}



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

    NSLog(@"Location: %@",newLocation);
    CLLocation *currentLocation = newLocation;


    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {

        if (error == nil && [placemarks count] >0 ) {

            placemark = [placemarks lastObject];


        }

        else NSLog(@"%@",error.debugDescription);

    }];

    MKCoordinateRegion region = { {0.0, 0.0}, {0.0, 0.0} };
    region.center.latitude = currentLocation.coordinate.latitude;
    region.center.longitude = currentLocation.coordinate.longitude;
    region.span.longitudeDelta = 0.001f;
    region.span.latitudeDelta = 0.001f;
    [mapview setRegion: region animated:YES];
    [mapview setScrollEnabled:YES];

}



- (void) startMap {
    manager = [[CLLocationManager alloc] init];
    geocoder = [[CLGeocoder alloc] init];
    manager.delegate = self;
    manager.desiredAccuracy = kCLLocationAccuracyBest;
   [manager startUpdatingLocation];
    mapview.mapType = MKMapTypeStandard;
    mapview.showsUserLocation = YES;

}

所以..我希望我的地图可以指向我当前的位置,当#34;视图确实加载了#34;但是之后我就能够在这一点上移动! ......用手势放大和缩小......这对新手来说太过分了吗?

提前感谢!

1 个答案:

答案 0 :(得分:0)

对于第一个问题,您可能需要通过MKMapView委托更新和重置您的位置。 Mk Mapview keeps resetting on users location?

对于第二个问题,问题可能包含您正在寻找的答案。如果没有,肯定会有答案,加上额外的代码。 Get the coordinates of a point from mkmapview on iphone