iOS Getting My Location Regularly and Setting Button to Get My Location - Google Map Not Working

ios getting my location regularly and setting button to get my location - Google Map not working

Did your - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ get called?

If not, you need to make sure your MapViewController implements the CLLocationDelegate. (Ex: @interface ViewController : UIViewController<CLLocationManagerDelegate> in your MapViewController.h file)

You can try this working example from GitHub, or the code snippet from this Gist.

Also, you should test it in your real device, otherwise you have to simulate a location in your XCode simulator (see image below, but it might not always work).

Sample Image

You have to add NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription to your info.plist file (note: don't rename or change the extension of the info.plist file, the extension has to be .plist):

Sample Image

Sample Image

Cannot get the my location dot for Google Maps iOS SDK

First check out following things are there or not.

    locationManager = [[CLLocationManager alloc]init];
locationManager.delegate= self;
if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];

_mapView.myLocationEnabled = YES;
_mapView.settings.myLocationButton = YES;

If Everything is there and if you are testing on Simulator then try to change the location in Simulator from debug Tab, e.g. change It to Free car run of any of them.

Also, check if you have added Description in PLIST for NSLocationWhenInUseUsageDescription.

Could not get current location using Google Map API (GMSMapView)

The thing is: Getting a location is asynchronous and how you do it is expect it to block till its there... which is not how it works

you have to add a KVO observer inn the map and observer myLocation.

Then when your observe method gets called, you get the myLocation.

(that's how the blue dot works too :))

- (void)viewDidLoad {
//...
[self.mapView addObserver:self forKeyPath:"myLocation" options:0 context:nil];
}
- (void)dealloc {
[_mapView removeObserver:self forKeyPath:@"myLocation"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if([keyPath isEqualToString:@"myLocation"]) {
CLLocation *l = [object myLocation];
//...
}
}

Button on the GMSMapView (Google Map) using Swift

Just create a UIButton and add it to your view after adding the map, as the last thing in your viewDidLoad should be fine.

let button = UIButton(frame: whereYouWant)
self.view.addSubview(button)

Change Location Button Image - Google Maps iOS SDK

I created a whole new button and made it serve as a location button with this code:

@IBAction func myLocationButton(_ sender: UIButton) {
guard let lat = self.mapView.myLocation?.coordinate.latitude, let lng = self.mapView.myLocation?.coordinate.longitude else {
return
}

let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lng, zoom: 16)
self.mapView.animate(to: camera)
}

The final outcome:
The final outcome



Related Topics



Leave a reply



Submit