2014-11-21 59 views
0

我想在MapView上顯示我當前的位置。我向故事板添加了一個按鈕。MapView不會加載當前用戶位置,也不會居中/放大它

第一個問題:當前位置沒有顯示在我的屏幕上。

第二個問題:如果我點擊按鈕,MapView不會縮放到位置。

我ViewController.m: (按鈕方法名稱是:getLocation

#import "ViewController.h" 
@import CoreLocation; 


@interface ViewController() <CLLocationManagerDelegate> 
@property (strong, nonatomic) CLLocationManager *locationManager; 

@end 

@implementation ViewController 
@synthesize mapview = _mapview; 

- (void)viewDidLoad { 

    [super viewDidLoad]; 


    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.delegate = self; 

    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 
     [self.locationManager requestWhenInUseAuthorization]; 
    } 
    mapview.showsUserLocation = YES; 
    [self.locationManager startUpdatingLocation]; 

} 

- (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; 
    } 
} 


// The sender here are a Button on my Storyboard, who should show the current device Location if i click on it. 
-(IBAction)getLocation:(id)sender 
{ 
    mapview.showsUserLocation = YES; 
    //NSLog(@"Koordinaten des Geräts: %@", mapview.userLocation.description); 
    //[mapview setCenterCoordinate:mapview.userLocation.coordinate animated:YES]; 
} 
@end 
+0

你在iOS的8測試它? – 2014-11-21 13:39:28

+0

你是否實現了CLLocationManagerDelegate方法? – 2014-11-21 13:43:16

+0

是的,我還在info.plist中設置了密鑰 – Linus 2014-11-21 13:44:59

回答

0

編譯馬克 - CLLocationManagerDelegate方法

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    CLLocation *newLocation = locations.lastObject; 
} 
- (void)locationManager: (CLLocationManager *)manager didFailWithError: (NSError *)error 
{ 
} 

而對於變焦試試這個

@property (weak, nonatomic) IBOutlet MKMapView *mapAllLocations; 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

mapAllLocations.ZoomEnabled = true; 
mapAllLocations.ScrollEnabled = true; 
CLLocationCoordinate2D zoomLocation; 

zoomLocation.latitude =newLocation.latitude; 
zoomLocation.longitude=newLocation.longitude; 

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation,1000.0, 1000.0); 
[self.mapAllLocations setRegion:viewRegion animated:YES]; 
+0

和我的語法在哪裏我應該添加這個塊? – Linus 2014-11-21 14:52:14

+0

謝謝你,你的語法對我有用。 – Linus 2014-11-21 19:28:13

+0

非常歡迎:) – 2014-11-25 10:47:19

0

你可能在你的Info.plist文件丟失NSLocationWhenInUseUsageDescription,它需要的iOS 8日起,您可以選擇將其值清空。過去,人們可以選擇包含'NSLocationUsageDescription'鍵,這是一個向用戶解釋計劃使用位置服務的應用程序的字符串。現在它已被分成兩個單獨的密鑰(NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription),現在是強制性的;如果您調用requestWhenInUseAuthorization或requestAlwaysAuthorization而沒有使用相應的鍵,則提示將不會顯示給用戶。

請記住,如果你指定requestAlwaysAuthorization關鍵,但您的應用程序並不總是使用定位,最好在後臺,那麼你就可以從應用程序商店提交過程中蘋果遭到拒絕。

+0

是的,我設置了按鍵,我也在警報視圖中看到了我自己的描述。 – Linus 2014-11-21 13:46:41