lifewithelliott
lifewithelliott

Reputation: 1432

swift - How to have google map view load smoothly upon application run

When the view loads on initial launch(tap of icon/run program), the google Map View starts in the united kingdom then teleports to my current location in a very abrupt manner. Nowhere in my code do I have any UK Coordinates that I would think would cause it to load there then teleport to my location.

How can I solve this issue and make the launch process more elegant?

let locationManager = CLLocationManager()
let googleMapView: GMSMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startMonitoringSignificantLocationChanges()

    }

}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.googleMapView = GMSMapView(frame: self.mapViewContainer.frame)
    self.view.addSubview(self.googleMapView)
    self.googleMapView.myLocationEnabled = true
    self.googleMapView.settings.compassButton = true

}

Beginning of Launch

Abruptly shifts to current location

Upvotes: 2

Views: 2836

Answers (2)

Mantas Laurinavičius
Mantas Laurinavičius

Reputation: 951

You have to initialize your map with camera (and location init). Example:

var map: GMSMapView!

init() {
map = GMSMapView(frame: view.frame, camera: GMSCameraPosition.camera(withTarget: getMyCoordinate(), zoom: mySavedZoom()))
}

Upvotes: 0

abielita
abielita

Reputation: 13469

You may try using Map Objects. By default, no location data is shown on the map. You may enable the blue "My Location" dot and compass direction by setting myLocationEnabled on GMSMapView.

mapView.myLocationEnabled = true

By enabling this feature will also provide the user's current location through the myLocation property.

if let mylocation = mapView.myLocation {
  print("User's location: \(mylocation)")
} else {
  print("User's location is unknown")
}

This property may not be immediately available - for example, if the user is prompted by iOS to allow access to this data. It will be nil in this case.

Upvotes: 0

Related Questions