Follow user location on google maps

拥有回忆 提交于 2020-01-01 19:44:48

问题


I have google maps sdk on ios and enabled userlocation = yes. I want to get the user location via GPS when he moves. I could not find any method in document that returns me location updates as the user keeps moving. I want to keep my user in center of the screen by continuously then updating the camera using these location.

Any points on this? There is a method didchangecameraposition which updates when i apply gestures on maps, but not when the gps gets updated.


回答1:


You cannot do it totally with the Google maps SDK, you got to use the CLLocationManger framework to get the location updates.

Initialize your locationManager to register for significant location changes and set up the delegate properly

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, set according to the required value.

[locationManager startUpdatingLocation];

The Location Manger's delegate:

- (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 by using the GMSCameraUpdate object

   GMSCameraUpdate *locationUpdate = [GMSCameraUpdate setTarget:location.coordinate zoom:YOUR_ZOOM_LEVEL];
   [mapView_ animateWithCameraUpdate:locationUpdate];


}


来源:https://stackoverflow.com/questions/22980527/follow-user-location-on-google-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!