2016-06-01 55 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]; 
} 

这是很重要

启用从---项目,功能标签背景模式,并选择位置更新...

你应该在设备上而不是模拟器上检查它。

相关问题