如何在谷歌地图iOS中找到当前位置?

时间:2013-05-08 19:05:40

标签: iphone ios google-maps location google-maps-sdk-ios

如何在谷歌地图中显示用户的当前位置?

我必须阅读此表格吗?

5 个答案:

答案 0 :(得分:7)

要移至当前位置,您必须:

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回答。

答案 1 :(得分:4)

使用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];
}

答案 2 :(得分:1)

... NSKeyValueObservingOptionNew

已折旧。

使用,例如:

[self.googleMapsView addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew context:nil];

答案 3 :(得分: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);

答案 4 :(得分:0)

AtomicInteger currentRow = new AtomicInteger(); // shared between tasks
final int maxRow = 2000;
final int batchSize = 20;

// Inside every task:
while(true) {
    int row = currentRow.getAndAdd(batchSize);
    if(row >= maxRow) return;
    int from = row+1;
    int to = Math.min(row+batchSize, maxRow);
    // process rows from..to; it's guaranteed that other threads
    // do not process the same rows.
}

如果您希望当前位置为默认位置,请在(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]; } 中调用此方法。