2013-05-08 137 views

回答

7

移動到您當前的位置,你必須:

self.googleMapsView.myLocationEnabled = YES; 

然後你可以設置你的viewWillAppear中方法的關鍵值的觀察者,然後你獲得與Google地圖SDK的位置管理您的位置更新。

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    // Implement here to check if already KVO is implemented. 
    ... 
    [self.googleMapsView addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingNew context: nil] 
} 

然後觀察屬性。

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
    if ([keyPath isEqualToString:@"myLocation"] && [object isKindOfClass:[GMSMapView class]]) 
    { 
     [self.googleMapsView animateToCameraPosition:[GMSCameraPosition cameraWithLatitude:self.googleMapsView.myLocation.coordinate.latitude 
                       longitude:self.googleMapsView.myLocation.coordinate.longitude 
                         zoom:self.googleMapsView.projection.zoom]]; 
    } 
} 

不要忘記在viewWillDisappear中註銷您的觀察者。

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
    // Implement here if the view has registered KVO 
    ... 
    [self.googleMapsView removeObserver:self forKeyPath:@"myLocation"]; 
} 

基於Weindl的答案。

+0

不錯的解決方案,但'GMSProjection'沒有'zoom'屬性 – k06a 2014-08-29 15:20:12

+0

不要忘記在你的Info.plist中設置這些鍵中的一個:'NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription' – hash3r 2016-02-03 18:53:34

1
... NSKeyValueObservingOptionNew 

折舊。

使用,按例如:

[self.googleMapsView addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew context:nil]; 
1
//In viewDidLoad 
self.mapView.myLocationEnabled = YES; 

//Whenever you need to check location 
CLLocation *myLocation = self.mapView.myLocation; 
NSLog(@"%f %f",myLocation.coordinate.latitude, myLocation.coordinate.longitude); 
3

的最佳方式,並使用CLLocationManagerDelegate和CLLocationManager讓你的座標,然後只將它們注入GMSMapView中做到這一點最簡單的方法:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.delegate = self; 
    [locationManager startUpdatingLocation]; 

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithTarget:CLLocationCoordinate2DMake(0, 0) zoom: 10]; 
    mapView = [GMSMapView mapWithFrame:self.viewForMap.bounds camera:camera]; 
    mapView.myLocationEnabled = YES; 
    [self.viewForMap addSubview:mapView]; 
} 


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    CLLocation *currentLocation = [locations lastObject]; 
    [mapView animateToLocation:currentLocation.coordinate]; 
} 
0
(void)updateCurrentLocation { 

    CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
    [locationManager startUpdatingLocation]; 
    CLLocation *location = [locationManager location]; 
    CLLocationCoordinate2D currentLocation = [location coordinate]; 
    if(noStartSet){ 
     startPoint = currentLocation; 
     noStartSet = FALSE; 
    } 
    [self.mapView animateToLocation:currentLocation];  
} 

如果您希望將當前位置設置爲默認值,請在您的viewdidload中調用此方法。

相關問題