Google Maps API:获取当前位置iOS的坐标

时间:2013-06-12 04:44:05

标签: ios objective-c google-maps location google-maps-sdk-ios

我目前正在项目中使用Google Maps API。我正在尝试将默认相机/缩放设置为用户位置。我这样做:

@implementation ViewController{

GMSMapView *mapView_;

}
@synthesize currentLatitude,currentLongitude;

- (void)viewDidLoad
{
[super viewDidLoad];
mapView_.settings.myLocationButton = YES;
mapView_.myLocationEnabled = YES;


}
- (void)loadView{

CLLocation *myLocation = mapView_.myLocation;

GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude);
marker.title = @"Current Location";
marker.map = mapView_;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude
                                                        longitude:myLocation.coordinate.longitude
                                                             zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];

self.view = mapView_;
   NSLog(@"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude);

}

然而,它不起作用,因为我做的时候

NSLog(@"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude);

它返回0,0,并且它不提供当前位置坐标。如何正确获取用户的坐标?

4 个答案:

答案 0 :(得分:11)

·H

#import <CoreLocation/CoreLocation.h>
@property(nonatomic,retain) CLLocationManager *locationManager;

的.m

- (NSString *)deviceLocation 
{
NSString *theLocation = [NSString stringWithFormat:@"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
return theLocation;
}

- (void)viewDidLoad
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
    [locationManager startUpdatingLocation];
}

回答here

答案 1 :(得分:6)

当应用程序首次启动时,它可能还不知道您的位置,因为GPS设备通常需要一段时间才能锁定您的位置(如果它刚刚启动),特别是如果这是第一次应用程序已运行,因此用户尚未回答提示,以便让应用程序访问其位置。在刚刚创建了地图视图时,似乎mapView.myLocation总是空的(无或者是坐标0,0)。

因此,您需要等到用户的位置已知,然后更新相机位置。

一种方法可能是使用Puneet建议的how to get current location in google map sdk in iphone处的代码,但请注意,那里的示例代码缺少设置位置管理器的详细信息(比如设置位置管理器的委托),这可能是为什么它不适合你。

另一种选择可能是在mapView.myLocation上使用KVO,如下所述:about positioning myself,some problems

顺便提一下,在您创建mapView.myLocation之前,您在示例代码中访问mapView,因此无论如何,该位置始终为零。

答案 2 :(得分:2)

我刚刚下载了适用于iOS的新版GoogleMap SDK Demo。以下是我从源代码中看到的如何通过KVO实现“当前位置”。

#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif

#import "SDKDemos/Samples/MyLocationViewController.h"

#import <GoogleMaps/GoogleMaps.h>

@implementation MyLocationViewController {
  GMSMapView *mapView_;
  BOOL firstLocationUpdate_;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
                                                          longitude:151.2086
                                                               zoom:12];

  mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  mapView_.settings.compassButton = YES;
  mapView_.settings.myLocationButton = YES;

  // Listen to the myLocation property of GMSMapView.
  [mapView_ addObserver:self
             forKeyPath:@"myLocation"
                options:NSKeyValueObservingOptionNew
                context:NULL];

  self.view = mapView_;

  // Ask for My Location data after the map has already been added to the UI.
  dispatch_async(dispatch_get_main_queue(), ^{
    mapView_.myLocationEnabled = YES;
  });
}

- (void)dealloc {
  [mapView_ removeObserver:self
                forKeyPath:@"myLocation"
                   context:NULL];
}

#pragma mark - KVO updates

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
  if (!firstLocationUpdate_) {
    // If the first location update has not yet been recieved, then jump to that
    // location.
    firstLocationUpdate_ = YES;
    CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
    mapView_.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
                                                     zoom:14];
  }
}

@end

希望它可以帮到你。

答案 3 :(得分:1)

万一有人想要DrDev在Swift 3中的出色答案:

var locationManager: CLLocationManager?

func deviceLocation() -> String {
    let theLocation: String = "latitude: \(locationManager.location.coordinate.latitude) longitude: \(locationManager.location.coordinate.longitude)"
    return theLocation
}

func viewDidLoad() {
    locationManager = CLLocationManager()
    locationManager.distanceFilter = kCLDistanceFilterNone
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
    // 100 m
    locationManager.startUpdatingLocation()
}