2013-06-12 117 views
9

我目前在我的项目中使用Google Maps API。我正在设置默认的摄像头/缩放到用户的位置。我这样做:Google Maps API:获取当前位置的坐标iOS

@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,它不给当前的位置坐标。我如何正确获取用户的坐标?

+0

这是什么线做什么? CLLocation * myLocation = mapView_.myLocation; 要获取当前位置,我们需要 1.添加CoreLocation框架 2.创建CLLocationManager的对象 CLLocationManager * locationManager = [[CLLocationManager alloc] init]; 3. [locationManager startUpdatingLocation]; CLLocation * location = [locationManager location]; CLLocationCoordinate2D coordinate = [位置坐标]; –

+0

谷歌地图有一个功能,获取用户的位置,但我不知道如何得到它 – Alexyuiop

+0

请参阅:http:// stackoverflow。问题/ 16331767 /如何获取当前位置在谷歌地图 - sdk在iphone中 希望它有帮助 –

回答

10

.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

+0

点击链接查看locationManager是什么.. – DrDev

+1

您需要在''viewDidLoad''自己的实现中调用''[super viewDidLoad]'' –

+0

初始化后,您需要设置委托;否则,将无法正常工作。在Swift中:'locationManager.delegate = self'您需要符合'CLLocationManagerDelegate'委托并实现'func locationManager(manager:CLLocationManager,didUpdateLocations locations:[CLLocation]){...}'。您可能还想停止更新某个位置的位置:'self.locationManager.stopUpdatingLocation()' – oyalhi

6

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

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

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

另一个选择可能是使用上mapView.myLocation志愿,如下所述:创建mapView之前,所以位置将永远是零反正你的示例代码about positioning myself,some problems

顺便问一下,你正在访问mapView.myLocation

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 

希望它能帮助你。

+0

此方法相当长,如果我们使用CLLocation Manager,效果会更好。 –

1

以防万一有人想DrDev的斯威夫特卡伦特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() 
} 
相关问题