2013-05-08 88 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中调用此方法。

相关问题