我正在制作这个应用程序,人们可以看到附近的旅游景点,我有点卡住了!
我需要它在加载应用并加载应用时加载用户的当前位置,但是它没有这样做。
很抱歉有点生硬,但我真的不知道如何设置它以便它可以同时使用按钮并自动加载它们的位置。
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
答案 0 :(得分:0)
使用此功能,不要忘记将代理放在.h文件中,希望它能正常工作;)
_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];
}