簡體   English   中英

如何在谷歌地圖iOS中找到當前位置?

[英]How to find the current location in 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]];
    }
}

不要忘記在viewWillDisappear中取消注冊觀察者。

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

基於Weindl的回答。

使用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];
}
... 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);
(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調用此方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM