IOS/ Objective-c: MKMapView 问题
IOS/ Objective-c: issue on MKMapView
我是 objective-c/IOS 的新人,我正在学习有关在地图上实现用户当前位置的教程。按照说明进行操作,但是当我按下按钮查看我的当前位置时,没有任何反应。有人可以给我提示吗?我使用的是 Xcode 的最新版本,我不确定教程中的版本是否相同...代码:
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];
vega.title=@"Capela";
vega.subtitle=@"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 * urlString=@"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
并且在 Connections Inspector 中设置了所有内容(按钮)..
您必须 check/get 位置许可:
将其添加到您的 plist 文件
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
并将该代码添加到您的 VC:
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}
我是 objective-c/IOS 的新人,我正在学习有关在地图上实现用户当前位置的教程。按照说明进行操作,但是当我按下按钮查看我的当前位置时,没有任何反应。有人可以给我提示吗?我使用的是 Xcode 的最新版本,我不确定教程中的版本是否相同...代码:
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];
vega.title=@"Capela";
vega.subtitle=@"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 * urlString=@"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
并且在 Connections Inspector 中设置了所有内容(按钮)..
您必须 check/get 位置许可:
将其添加到您的 plist 文件
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
并将该代码添加到您的 VC:
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}