Iphone 如何在谷歌地图iOS中找到当前位置?
如何在谷歌地图中显示用户的当前位置Iphone 如何在谷歌地图iOS中找到当前位置?,iphone,ios,google-maps,location,google-maps-sdk-ios,Iphone,Ios,Google Maps,Location,Google Maps Sdk Ios,如何在谷歌地图中显示用户的当前位置 我必须阅读此GPS表吗?要移动到您当前的位置,您必须: self.googleMapsView.myLocationEnabled = YES; 然后,您可以在ViewWillAspect方法中设置键值观察者,然后使用GoogleMaps SDK的位置管理器获取位置更新 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // Implement
我必须阅读此GPS表吗?要移动到您当前的位置,您必须:
self.googleMapsView.myLocationEnabled = YES;
然后,您可以在ViewWillAspect方法中设置键值观察者,然后使用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]];
}
}
不要忘记在视图中取消注册您的观察者,否则将消失
- (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];
使用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];
}
如果希望将当前位置作为默认位置,请在
viewdidload
中调用此方法 很好的解决方案,但是GMSProjection
没有zoom
属性不要忘记在信息列表中设置这些键之一。plist:nslocation当不使用usagedescription或nslocationalways usagedescription时
- (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];
}