【问题标题】:How will i get current location of device from google maps.? [duplicate]我如何从谷歌地图获取设备的当前位置。? [复制]
【发布时间】:2016-09-30 08:07:46
【问题描述】:

我在我的应用程序中使用谷歌地图。我目前在地图上获得默认位置。但我需要获取设备的当前位置并在地图上显示。 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];


 }

【问题讨论】:

  • 让我清楚是否需要在 plist 文件中添加一些内容?

标签: ios objective-c google-maps cllocationmanager


【解决方案1】:

你还没有初始化你的locationManager,你还需要询问用户位置访问权限并设置它的委托。在开始位置更新之前使用以下代码。

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

您使用的方法已弃用。

【讨论】:

  • 好的,所以这段代码也适用于获取位置许可?
  • 我应该把这段代码放在 viewdidload 的什么地方?
  • 是的,在调用“startUpdatingLocation”之前将其添加到viewDidLoad中。设置委托将在“didUpdateToLocation”函数中为您提供位置更新。
【解决方案2】:
@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];
        //...
}

【讨论】:

    【解决方案3】:

    希望对你有帮助..

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

    这很重要

    从---项目、功能选项卡启用后台模式,然后选择位置更新...

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

    【讨论】:

      猜你喜欢
      • 2017-09-27
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多