How to get current location using google maps sdk in ios

Hi I am new to Ios and in my project I am using Google SDK to get the route when the user is walking or driving on the road.

But I cannot get the current location and show in my console the xcode 0.0 and 0.0 (latitude and longitude values) please help me find someone to get the current location

My code is below: -

#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

      

+3


source to share


1 answer


Getting a location is an asynchronous process. So you need to "wait" for the location to be updated - in this case, watching "myLocation"

observe location

this code only works if _mapView is on screen



// 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;
});

      

get location updates:

- (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];
  }
}

      

0


source







All Articles