CLLocationManager delegate methods are not getting called(google maps is integrated)
From your code you are working with Swift 3, and in Swift 3 CLLocationManagerDelegate
method's signature is changed like this.
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
}
//func locationManager(_ manager: CLLocationManager, didUpdateTo newLocation: CLLocation,
from oldLocation: CLLocation) is deprecated with below one
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}
Check Apple Documentation on CLLocationManagerDelegate
for more details.
CLLocationManager delegate not called in ios 10 swift 3 simulator]
Did you change the location after starting application? In menu: Debug / Location / ...
Or change the default location (on your last screenshot)
Google Maps: custom LocationSource in iOS
There is a solution. Google Maps API uses the Cocoa framework class CLLocationManager. Everyone how would like to be informed about current GPS location changes must conform to CLLocationManagerDelegate and be a delegate of the CLLocationManager object. The updates are being sent to the delegate object through the
locationManager:didUpdateLocations: delegate method.
This is a theory everyone knows about. So in order to simulate location data we need to find a way to invoke this method passing our own data to it.
Fortunately there is an opportunity to dynamically change the behaviour of any class, via the functions class_addMethod and class_replaceMethod. So now we can change the implementation of a method abc with a method override_abc which can be implemented in a category like so:
let originalSelector = NSSelectorFromString("setDelegate:")
let newSelector = NSSelectorFromString("override_setDelegate:")
let originalMethod = class_getInstanceMethod(CLLocationManager.self, originalSelector)
let newMethod = class_getInstanceMethod(CLLocationManager.self, newSelector)
method_exchangeImplementations(originalMethod, newMethod)
For our case, we need to change the setter for the location managers delegate and add our own setter as an extension:
extension CLLocationManager {
func override_setDelegate(delegate: CLLocationManagerDelegate) {
// save a reference to self
GMLocationManager = self
override_setDelegate(delegate)
}
}
Once we have saved a reference to the Google Maps location manager we can now call locationManager:didUpdateLocations: delegate method using an additional extension method simulateLocation.
extension CLLocationManager {
func simulateLocation(location: CLLocation) {
self.delegate?.locationManager?(self, didUpdateLocations: [location])
}
}
// ...
GMLocationManager?.simulateLocation(fakeLocation)
UIWebView and Google Maps
Step-1
Add these two properties in info.plist
NSLocationAlwaysUsageDescription
and below property
Step-2
on your view controller call delegate method
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
@property (strong, nonatomic) NSString *longitude;
@property (strong, nonatomic) NSString *latitude;
Step-3
on your Implementation File on your `viewDidLoad`
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
}
Step-4
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"There was an error retrieving your location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[errorAlert show];
NSLog(@"Error: %@",error.description);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *crnLoc = [locations lastObject];
latitude= [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.latitude];
longitude = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.longitude];
}
for sample see this and this
Related Topics
How to Analyze Stack Trace Info of a Thread
Cabasicanimation Does Not Animate Correctly When I Update Model Layer
Sending Push Notifications to iOS from Pwa
Ios: Uiview Subclass Init or Initwithframe:
How to Add Constraint Between a View and the Top Layout Guide in a Xib File
How to Turn Flashlight on and Off in Swift
How to Get iOS Appstorereceipturl into Base 64 Encoded String
Uitableviewrowaction Title as Image Icon Instead of Text
How to Extract a Url from a Sentence That Is in a Nsstring
Reconnecting to Disconnected Peers
Save an Object in Nsuserdefaults and Realm
How to Animate Add Uisearchbar on Top of Uinavigationbar
Set Uitableview Content Inset Permanently
Change Color of Png in Buttons - iOS
How to Write Init Methods of a Uiviewcontroller in Swift
How to Add Text Input in Alertview of iOS 8