2016-12-14 43 views
-1

我是Objective-C/IOS的​​新手,我正在学习如何在地图上实现用户的当前位置。按照说明,但当我按下按钮来查看我的当前位置,没有任何反应。任何人都可以给我一个提示吗?我使用的是最新版本的Xcode,我不知道,如果一个教程是一样的...代码:IOS/Objective-c:关于MKMapView的问题

ViewController.m:

#import "ViewController.h" 
#import "Pin.h" 


@interface ViewController() 

@end 

@implementation ViewController; 

@synthesize mapview; 

- (void)viewDidLoad { 
[super viewDidLoad]; 
MKCoordinateRegion region={{0.0,0.0},{0.0,0.0}}; 
region.center.latitude=38.711995; 
region.center.longitude=-9.144932; 
region.span.longitudeDelta=0.01f; 
region.span.latitudeDelta=0.01f; 
[mapview setRegion:region animated:YES]; 

Pin*vega =[[Pin alloc] init]; 
[email protected]"Capela"; 
[email protected]"Bar"; 
vega.coordinate= region.center; 
[mapview addAnnotation:vega]; 

} 


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

-(IBAction)SetMap:(id)sender{ 

switch (((UISegmentedControl *) sender).selectedSegmentIndex) { 
    case 0: 
     mapview.mapType=MKMapTypeStandard; 
     break; 

    case 1: 
     mapview.mapType=MKMapTypeSatellite; 
     break; 

    case 2: 
     mapview.mapType=MKMapTypeHybrid; 
     break; 


    default: 
     break; 
    } 


} 


-(IBAction)GetLocation:(id)sender{ 
    mapview.showsUserLocation=YES; 

} 


(IBAction)Directions:(id)sender{ 
    NSString * [email protected]"http://maps.apple.com/maps?daddr=38.711995,  -9.144932"; 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; 

} 

@end 

VieController.h:

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

@interface ViewController : UIViewController{ 

MKMapView * mapview; 

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

-(IBAction)SetMap:(id)sender; 

-(IBAction)GetLocation:(id)sender; 

-(IBAction)Directions:(id)sender; 

@end 

而且在连接检查一切(按钮)设置..

+0

你必须检查的位置权限 –

回答

0

你必须检查/获取位置权限:

它添加到您的plist文件

<key>NSLocationWhenInUseUsageDescription</key> 
<string></string> 

和代码添加到您的VC:

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) 
{ 
    [self.locationManager requestWhenInUseAuthorization]; 
}