2016-06-01 52 views
0

我在谷歌地圖工作在我的應用程序。我現在在地圖上獲得默認位置。但我需要獲取設備的當前位置並在地圖上顯示它。 stackoverlfow上有很多解決方案,但不知何故,它不適用於我的情況。如果我在默認視圖中添加地圖,這些解決方案可以工作。查看我的一點點代碼。我將如何從谷歌地圖獲取設備的當前位置?

.h文件中

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 
@import GoogleMaps; 

@interface ViewController : UIViewController<CLLocationManagerDelegate, GMSMapViewDelegate> 

@property (weak, nonatomic) IBOutlet UIView *maponScreem; 
@property (nonatomic, retain) CLLocationManager *locationManager; 
@end 

.m文件

self.locationManager.delegate = self; 
self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.distanceFilter = kCLDistanceFilterNone; 
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m 
[self.locationManager startUpdatingLocation]; 

latitude = [NSString stringWithFormat:@"%f",self.locationManager.location.coordinate.latitude]; 
longtitude = [NSString stringWithFormat:@"%f",self.locationManager.location.coordinate.longitude]; 

NSLog(@"%@", latitude); 
NSLog(@"%@", longtitude); 


GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[latitude floatValue] 
                 longitude:[longtitude floatValue] 
                  zoom:12]; 
mapView_ = [GMSMapView mapWithFrame:self.maponScreem.bounds camera:camera]; 
mapView_.delegate = self; 
mapView_.myLocationEnabled = YES; 
[self.maponScreem addSubview: self->mapView_]; 

// Creates a marker in the center of the map. 
GMSMarker *marker = [[GMSMarker alloc] init]; 
marker.position = CLLocationCoordinate2DMake([latitude intValue], [longtitude intValue]); 
marker.title = @"Current Location"; 
marker.map = mapView_; 

我得到0.000000的態度和經度。

編輯:
我找到解決辦法,看看這究竟是怎麼工作的。感謝大家的回答並支持我。

- (void)viewDidLoad { 
[super viewDidLoad]; 



if (self.locationManager == nil) 
{ 
    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.delegate = self; 
} 
else 
{ 
    nil; 
} 

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

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[self.locationManager startUpdatingLocation]; 


GMSCameraPosition *camera = [GMSCameraPosition cameraWithTarget:CLLocationCoordinate2DMake(0, 0) zoom: 16]; 
mapView_ = [GMSMapView mapWithFrame:self.maponScreem.bounds camera:camera]; 
mapView_.myLocationEnabled = YES; 
[self.maponScreem addSubview: self->mapView_]; 

} 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
    { 
CLLocation *location = [locations lastObject]; 
NSString *lasti = [NSString stringWithFormat:@"%f", location.coordinate.latitude]; 
NSString *longi = [NSString stringWithFormat:@"%f", location.coordinate.longitude]; 
    // NSLog(@"%@", lat); 
// NSLog(@"%@", longi); 
    [mapView_ animateToLocation:location.coordinate]; 


} 
+0

讓我清楚如果我需要添加一些東西在plist文件? –

回答

1

您尚未初始化的LocationManager,你也需要詢問用戶權限的位置訪問和起始位置更新之前設置其delegate.Use下面的代碼。

self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.delegate = self; 
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 
     [self.locationManager requestWhenInUseAuthorization]; //gives alert for location access 
    } 

它會給你的用戶位置。

希望它能幫助:)

編輯 您必須使用以下方法來接收位置更新

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateLocations:(NSArray<CLLocation *> *)locations 

你用的是過時的方法。

+0

好吧,所以這段代碼也適用於獲取位置權限? –

+0

和我應該把這段代碼放在viewdidload中? –

+0

是在調用「startUpdatingLocation」之前將它添加到viewDidLoad中.Setting委託將爲您提供位置更新,位於「didUpdateToLocation」函數中。 – sanman

0
@property (nonatomic, retain) IBOutlet GMSMapView *googleMapView; 
@property (nonatomic, retain) CLLocationManager *locationManager; 

- (void)showCurrentLocation { 
    _googleMapView.myLocationEnabled = YES; 
    [self.locationManager startUpdatingLocation]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
      GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:newLocation.coordinate.latitude 
                    longitude:newLocation.coordinate.longitude 
                     zoom:17.0]; 
      [_googleMapView animateToCameraPosition:camera]; 
     //... 
} 
0

我希望這將幫助你..

- (IBAction)btnSetDistance:(id)sender { 

locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
locationManager.pausesLocationUpdatesAutomatically = NO; 

locationManager.distanceFilter =//as per your requirment; 

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

這是很重要

啓用從---項目,功能標籤背景模式,並選擇位置更新...

你應該在設備上而不是模擬器上檢查它。

相關問題