2013-06-12 115 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() 
} 
相關問題