2
A
回答
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的答案。
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
中調用此方法。
相關問題
- 1. 如何在Android中查找當前位置的谷歌地圖?
- 2. 谷歌地圖sdk如何獲得當前位置在xamarin ios
- 3. 在谷歌地圖中查找位置
- 4. 谷歌地圖當前位置繪圖
- 5. ios - 谷歌地圖在webview中不加載當前位置
- 6. 谷歌地圖V2找到,如果當前位置位於圓
- 7. iOS谷歌地圖SDK - 谷歌地圖不更新到當前位置
- 8. 谷歌地圖上的當前位置
- 9. 谷歌地圖當前位置Swift
- 10. 獲取谷歌地圖當前位置
- 11. 谷歌地圖 - 當前位置
- 12. 如何重定向在谷歌地圖中的當前位置
- 13. 谷歌地圖找到當前位置和附近的地方
- 14. 如何與Android中的當前位置共享谷歌地圖?
- 15. 如何居中當前位置? (谷歌地圖)
- 16. 谷歌地圖放大到IOS當前位置
- 17. 谷歌地圖iOS SDK,獲取當前位置用戶
- 18. 谷歌地圖iOS SDK,獲取當前位置用戶
- 19. 谷歌地圖API如何設置當前位置標記
- 20. 谷歌地圖我得到當前的位置當我改變位置標記不會在谷歌地圖
- 21. 在谷歌地圖中查找位置地址
- 22. 如何將當前位置添加到動態谷歌地圖?
- 23. 如何獲得谷歌地圖的網絡當前位置
- 24. 如何獲得當前位置的谷歌地圖片段?
- 25. 如何獲取當前位置的谷歌地圖?
- 26. 谷歌地圖中心位於當前位置
- 27. 我想在谷歌地圖中打開我當前的位置
- 28. 在谷歌地圖(JavaScript)中顯示當前用戶的位置
- 29. 谷歌地圖在距離計算器中的當前位置
- 30. 使用我的當前位置將谷歌地圖實施到谷歌地圖
不錯的解決方案,但'GMSProjection'沒有'zoom'屬性 – k06a 2014-08-29 15:20:12
不要忘記在你的Info.plist中設置這些鍵中的一個:'NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription' – hash3r 2016-02-03 18:53:34