【问题标题】:Google Maps API: Getting coordinates of current location iOSGoogle Maps API:获取当前位置 iOS 的坐标
【发布时间】:2013-06-08 02:40:13
【问题描述】:

我目前正在我的项目中使用 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,并且不给出当前位置坐标。如何正确获取用户坐标?

【问题讨论】:

  • 这条线在做什么? CLLocation *myLocation = mapView_.myLocation;要获取当前位置,我们需要 1. 添加 CoreLocation 框架 2. 创建 CLLocationManager 的对象 CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 3. [locationManager startUpdatingLocation]; CLLocation *location = [locationManager 位置]; CLLocationCoordinate2D 坐标 = [位置坐标];
  • 谷歌地图有获取用户位置的功能,但是不知道怎么获取
  • 参考:stackoverflow.com/questions/16331767/…希望对您有所帮助
  • 我看了那个,但它似乎不适合我
  • 当我使用它时出现空白屏幕,我在自定义视图上显示地图并想要获取当前位置的纬度、经度?

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


【解决方案1】:

.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。

【讨论】:

  • 你需要在你自己的viewDidLoad实现中调用[super viewDidLoad];
  • 初始化后,需要设置delegate;否则,将无法正常工作。在 Swift 中:locationManager.delegate = self您需要遵守CLLocationManagerDelegate 委托并实现func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {...}。您可能还想在某个时候停止更新位置:self.locationManager.stopUpdatingLocation()
【解决方案2】:

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

所以你需要等到知道用户的位置后,再更新摄像头位置。

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

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

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

【讨论】:

    【解决方案3】:

    我刚刚下载了适用于 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
    

    希望对你有帮助。

    【讨论】:

    • 这个方法比较长,如果我们用CLLocation Manager会更好。
    【解决方案4】:

    以防万一有人想要 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()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-01
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多