문제

I'm making this app where people can see what tourist attractions are near them and I'm a bit stuck!

I need it to load the user's current location when the app is loaded and zoom to it, however it's not doing so.

Sorry to be a bit blunt but I really don't know how to set it up so that it will work with both the button and load their location automatically.

https://pbs.twimg.com/media/BcRSTk0CAAEV47Q.jpg

ViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface ViewController : UIViewController{

    MKMapView *mapview;

}

@property (weak, nonatomic) IBOutlet MKMapView *myMapView;
-(IBAction)getlocation;

@end

ViewController.m

#import "ViewController.h"
#import "Annotation.h"

@interface ViewController ()

@end

//Thorpe Park Coordinates
#define THORPE_LATITUDE 51.40395;
#define THORPE_LONGITUDE -0.51433;

//London Eye Coordinates
#define LONDONEYE_LATITUDE 51.50340;
#define LONDONEYE_LONGITUDE -0.11952;

//The New London Dungeons
#define LONDONDUGNEONS_LATITUDE 51.50231;
#define LONDONDUGNEONS_LONGITUDE -0.11965;


//Span
#define THE_SPAN 0.50f;



@implementation ViewController
@synthesize myMapView;

-(IBAction)getlocation {
    mapview.showsUserLocation = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Create Region
    MKCoordinateRegion myRegion;

    //Center
    CLLocationCoordinate2D center;
    center.latitude = LONDONEYE_LATITUDE;
    center.longitude = LONDONEYE_LONGITUDE;

    //Span
    MKCoordinateSpan span;
    span.latitudeDelta = THE_SPAN;
    span.longitudeDelta = THE_SPAN;

    myRegion.center = center;
    myRegion.span = span;



    //Set our mapView
    [myMapView setRegion:myRegion animated:YES];


    //Annotation's
    NSMutableArray * locations = [ [NSMutableArray alloc] init];
    CLLocationCoordinate2D location;
    Annotation *myAnn;

    //London Eye
    myAnn = [[Annotation alloc] init];
    location.latitude = LONDONEYE_LATITUDE;
    location.longitude = LONDONEYE_LONGITUDE;
    myAnn.coordinate = location;
    myAnn.title = @"The London Eye";
    myAnn.subtitle = @"";
    [locations addObject:myAnn];

    //The New London Dungeons
    myAnn = [[Annotation alloc] init];
    location.latitude = LONDONDUGNEONS_LATITUDE;
    location.longitude = LONDONDUGNEONS_LONGITUDE;
    myAnn.coordinate = location;
    myAnn.title = @"The London Dungeons";
    myAnn.subtitle = @"";
    [locations addObject:myAnn];

    //Thorpe Park
    myAnn = [[Annotation alloc] init];
    location.latitude = THORPE_LATITUDE;
    location.longitude = THORPE_LONGITUDE;
    myAnn.coordinate = location;
    myAnn.title = @"Thorpe Park";
    myAnn.subtitle = @"";
    [locations addObject:myAnn];

    [self.myMapView addAnnotations:locations];



    /*
    //1. Create a coordinate to be used with pin
    CLLocationCoordinate2D ThorpeLocation;
    ThorpeLocation.latitude = THORPE_LATITUDE;
    ThorpeLocation.longitude = THORPE_LONGITUDE;

    Annotation * myAnnotation = [Annotation alloc];
    myAnnotation.coordinate = ThorpeLocation;
    myAnnotation.title = @"Thorpe Park";
    myAnnotation.subtitle = @"Theme Park";

    [self.myMapView addAnnotation:myAnnotation];
    */

}

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

@end
도움이 되었습니까?

해결책

Use this, and don't forget to put the delegate on your .h file, hope it works ;)

    _mapView.userTrackingMode = YES;

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

    MKCoordinateRegion region;
    MKCoordinateSpan span;

    span.latitudeDelta = 0.00001;
    span.longitudeDelta = 0.00001;

    CLLocationCoordinate2D location = _mapView.userLocation.coordinate;

    region.span = span;
    region.center = location;

    [_mapView setRegion:region animated:TRUE];
    [_mapView regionThatFits:region];

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top