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).
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
):
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:
Related Topics
Contentview with UItableview Not Scrolling Within UIscrollview
Shared Iad Banner Bannerviewdidloadad Not Being Called
How to Make a Reusable Tableview for Different Screens in the Same Application
iOS 8 Code Working on iPhone 5S But Not iPhone 5
App Crashes After Updating Coredata Model That Is Being Displayed in a UItableview
Check Whether Time Falls Between Two Time iOS
Resize All Subview in Scrollviewdidzoom
Gcm Support for iOS Application When Application in Background or Killed
Apple Doesn't Recognize Imgur's Https
Is There an iOS API for Accessing Call Logs
Can You Listen to Firestore Updates When iOS App Is in The Background
How to Get HTML5 Video Thumbnail Without Using Poster on Safari or iOS
Nsmanagedobject Setter Giving Me [ Myobject Setname:]: Unrecognized Selector Sent to Instance
Get The Data from All Children in Firebase Using Swift