Search code examples
iosobjective-cgoogle-maps-sdk-ios

Google Maps iOS SDK, Getting Current Location of user


For my iOS app (building in iOS7),i need to show user's current location when the app load.I am using Google Maps iOS SDK. I am following this Google Map But i can't figure it out. Please help if you go through the path.


Solution

  • It seems Google Maps iOS SDKcannot access to the device position. So you have to retrieve the position by using CLLocationManagerof iOS.

    First, add the CoreLocation.framework to your project :

    • Go in Project Navigator
    • Select your project
    • Click on the tab Build Phases
    • Add the CoreLocation.framework in the Link Binary with Libraries

    Then all you need to do is to follow the basic exemple of Apple documentation.

    • Create a CLLocationManager probably in your ViewDidLoad:

      if (nil == locationManager)
          locationManager = [[CLLocationManager alloc] init];
      
      locationManager.delegate = self;
      //Configure Accuracy depending on your needs, default is kCLLocationAccuracyBest
      locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
      
      // Set a movement threshold for new events.
      locationManager.distanceFilter = 500; // meters
      
      [locationManager startUpdatingLocation];
      

    With the CLLocationManagerDelegate every time the position is updated, you can update the user position on your Google Maps :

    - (void)locationManager:(CLLocationManager *)manager
          didUpdateLocations:(NSArray *)locations {
        // If it's a relatively recent event, turn off updates to save power.
       CLLocation* location = [locations lastObject];
       NSDate* eventDate = location.timestamp;
       NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
       if (abs(howRecent) < 15.0) {
          // Update your marker on your map using location.coordinate.latitude
          //and location.coordinate.longitude); 
       }
    }