Google Maps iOS SDK, Getting Current Location of user

前端 未结 7 2407
渐次进展
渐次进展 2020-12-01 04:49

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 follo

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 05:13

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

提交回复
热议问题