Google Maps iOS Sdk, Getting Current Location of User

Google Maps iOS SDK, Getting Current Location of user

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

Google Maps iOS SDK to user's current location on launch Swift SwiftUI

Create a loading view until the location is updated. Then once its updated disable the loading view and show the map and it will begin the camera movement on appearance of the map view.

How to Show my current location on google maps, when I open the ViewController? in Swift?

For Swift 3.x solution, please check this Answer

First all of you have to enter a key in Info.plist file
NSLocationWhenInUseUsageDescription

Sample Image

After adding this key just make a CLLocationManager variable and do the following

@IBOutlet weak var mapView: GMSMapView!
var locationManager = CLLocationManager()

class YourControllerClass: UIViewController,CLLocationManagerDelegate {

//Your map initiation code
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
self.view = mapView
self.mapView?.myLocationEnabled = true

//Location Manager code to fetch current location
self.locationManager.delegate = self
self.locationManager.startUpdatingLocation()
}

//Location Manager delegates
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let location = locations.last

let camera = GMSCameraPosition.cameraWithLatitude((location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!, zoom: 17.0)

self.mapView?.animateToCameraPosition(camera)

//Finally stop updating location otherwise it will come again and again in this delegate
self.locationManager.stopUpdatingLocation()

}

When you run the code you will get a pop up of Allow and Don't Allow for location. Just click on Allow and you will see your current location.

Make sure to do this on a device rather than simulator. If you are using simulator, you have to choose some custom location and then only you will be able to see the blue dot.

Sample Image

Cant get current location (GPS) on google maps ios sdk

Did you add NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription (in your case it's this one) keys in your .plist file. Because if the alert view asking for authorization is not showing this might be the issue.

How to show user location on Google Maps iOS SDK

You can do this with following line of code. This will bring up the google's default blue dot on users current location.

self.mapView.myLocationEnabled = true

How do I get user location using Google Maps Platform SDK for iOS instead of CoreLocation

Both Maps SDK and Places SDK for iOS use ⁠Apple Core Location, which you can also use directly to get location updates without having to use Google's iOS SDKs.

Currently, Google's iOS SDKs do not allow you to modify the location provider.

There's a feature request around being able to set another location provider in Google's issue tracker which I suggest starring to increase visibility and subscribe to future notifications:

https://issuetracker.google.com/issues/116535597

Hope this helps!

How to focus on user location using SwiftUI and GoogleMaps iOS SDK

I thought i could grab mapView.myLocation and use that to manually
focus the map camera on startup but that didnt work. You can see in
the code above i try to print out the location but all i get back is
'User's location is unknown'

It looks like mylocation does not contain any location coordinates. mapView.myLocation might not be what you think it is, perhaps it holds the user's device location and not the coordinate that you're passing to the GMSCameraPosition. According to the documentation

If My Location is enabled, reveals where the user location dot is
being drawn.

So it's not the one you're passing to the camera

Does anyone have an idea of the 'proper' way to have the map camera
focus on a user's current location at startup?

Have you created a locationManager somewhere and dealt with location permissions? If you have, you can catch the user's location inside a function called didUpdateLocations - it takes a couple of seconds or so to retrieve the user location upon startup. Once you get the location, you can do the panning the camera to the location.

How can I find a user's location using Google Maps SDK for Swift?

Have you tried using CLLocationManager() ?

Try the tutorial below, this should show you how to get the user's location. This will take you through asking the user for permission to see their location, down to reverse geocoding the location to display the address they are at, using GMSGeocoder()

[https://www.raywenderlich.com/109888/google-maps-ios-sdk-tutorial][1]

[1]: Ray Wenderlich

Hope that helps



Related Topics



Leave a reply



Submit