Как показать в Google maps текущую позицию пользователя?Как найти текущее местоположение в Google Maps iOS?
Должен ли я читать эту форму GPS?
Как показать в Google maps текущую позицию пользователя?Как найти текущее местоположение в Google Maps iOS?
Должен ли я читать эту форму GPS?
Для перехода к текущей позиции, вы должны:
self.googleMapsView.myLocationEnabled = YES;
Затем вы можете установить значение ключа наблюдателя в методе viewWillAppear, а затем вы получите обновление вашего местоположения с местоположением Менеджер GoogleMaps 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]];
}
}
Не забудьте отменить регистрацию своего наблюдателя в представленииWillDisappear.
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// Implement here if the view has registered KVO
...
[self.googleMapsView removeObserver:self forKeyPath:@"myLocation"];
}
На основании ответа Weindl.
... NSKeyValueObservingOptionNew
обесценивается.
Использование, как в примере:
[self.googleMapsView addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew context:nil];
//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);
Лучший способ и самый простой способ сделать это с помощью 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];
}
(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
, если вы хотите, чтобы ваше текущее местоположение по умолчанию.
Хорошее решение, но 'GMSProjection' не имеет свойства' zoom' – k06a
Не забудьте указать один из этих ключей в Info.plist: 'NSLocationWhenInUseUsageDescription или NSLocationAlwaysUsageDescription' – hash3r