9

我目前正在我的项目中使用 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

4 回答 4

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

在这里回答。

于 2013-06-12T13:46:06.047 回答
6

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

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

一种方法可能是使用Puneet 建议的如何在 iphone 中的 google map sdk 中获取当前位置的代码,但请注意,那里的示例代码缺少设置位置管理器的详细信息(例如设置位置管理器的委托),这可能就是为什么它不适合你。

另一种选择可能是在 上使用 KVO mapView.myLocation,如下所述:关于定位自己,一些问题

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

于 2013-06-12T07:31:17.563 回答
2

我刚刚下载了适用于 iOS 的新 GoogleMap SDK 演示。这是我从源代码中看到的如何通过 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

希望它可以帮助你。

于 2015-08-31T13:41:14.550 回答
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()
}
于 2017-05-18T06:32:11.270 回答