如何在iOS中使用Google地图SDK获取当前位置

3

你好,我刚开始学习iOS,在我的项目中,我使用Google SDK来获取用户步行或驾车沿途的路线。

但是我无法获取当前位置,并且在我的Xcode控制台中显示0.0和0.0(纬度和经度值),请有人帮助我获取当前位置。

我的代码如下:

#import "ViewController.h"

@interface ViewController ()
{
    GMSMapView *_mapView;
    NSMutableArray *_coordinates;
    LRouteController *_routeController;
    GMSPolyline *_polyline;
    GMSMarker *_markerStart;
    GMSMarker *_markerFinish;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _mapView.settings.myLocationButton = YES;
    _mapView.myLocationEnabled = YES;
    _mapView.delegate = self;

}

- (void)loadView{

    CLLocation *myLocation = _mapView.myLocation;
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude);
    marker.title = @"Current Location";
    marker.map = _mapView;
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude
                                                            longitude:myLocation.coordinate.longitude
                                                                 zoom:6];
    _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

    self.view = _mapView;

    NSLog(@"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude);
}

#pragma mark - GMSMapViewDelegate

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    _polyline.map = nil;
    _markerStart.map = nil;
    _markerFinish.map = nil;

    [_coordinates addObject:[[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude]];

    if ([_coordinates count] > 1)
    {
        [_routeController getPolylineWithLocations:_coordinates travelMode:TravelModeDriving andCompletitionBlock:^(GMSPolyline *polyline, NSError *error) {
            if (error)
            {
                NSLog(@"%@", error);
            }

            else if (!polyline)
            {
                NSLog(@"No route");
                [_coordinates removeAllObjects];
            }

            else
            {
                _markerStart.position = [[_coordinates objectAtIndex:0] coordinate];
                _markerStart.map = _mapView;

                _markerFinish.position = [[_coordinates lastObject] coordinate];
                _markerFinish.map = _mapView;

                _polyline = polyline;
                _polyline.strokeWidth = 3;
                _polyline.strokeColor = [UIColor blueColor];
                _polyline.map = _mapView;
            }
        }];
    }
}

@end

请查看这个 Stack Overflow 的链接:https://dev59.com/52_Xa4cB1Zd3GeqP1HFq - Om Prakash
请修改我的代码并发布 - user5026700
如果你想获取用户当前的位置,你需要使用CLLocationManager,你可以查看这个SO回答以获取更多详细信息。 - ztan
1个回答

0

获取位置是一个异步过程。因此,您需要“等待”位置更新--在这种情况下通过观察“myLocation”

观察位置

此代码仅在_mapView在屏幕上时才有效。

// Listen to the myLocation property of GMSMapView.
[_mapView addObserver:self
         forKeyPath:@"myLocation"
            options:NSKeyValueObservingOptionNew
            context:NULL];

// Ask for My Location data after the map has already been added to the UI.
dispatch_async(dispatch_get_main_queue(), ^{
    _mapView.myLocationEnabled = YES;
});

获取位置更新:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
  if (!_firstLocationUpdate) {
    // If the first location update has not yet been recieved, then jump to that
    // location.
    _firstLocationUpdate = YES;
    CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
    _mapView.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
                                                     zoom:14];
  }
}

在Google Maps IOS SDK演示中查看MyLocationViewController。 - Daij-Djan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接