Not getting users current location in swift 4
The problem is your Info.plist. You need all three keys:
Fix that and the app will spring to life. I pasted your code into a project, set up the Info.plist as shown, and it worked fine.
You can actually omit "Always" starting in iOS 11, but you must have both "When In Use" and "Always and When In Use", even if you are planning to ask only for "Always".
The runtime was in fact telling you this all along in the Xcode console, but you apparently weren't looking at it. It says:
The app's Info.plist must contain both NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription keys with string values explaining to the user how the app uses this data.
How to make google maps api get user's current location to be stored it in the database?
Your code contains several issues that I will not correct here.
Add this at the end of your initMap()
function and it will center the map on your location without changing any other functionality of your first script.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(p) {
var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
// Set the map center on user location
map.setCenter(LatLng);
});
} else {
alert('Geo Location feature is not supported in this browser.');
}
Swift: Get Current User Coordinates and Store them into a Variable
import Foundation
import CoreLocation
import UIKit
public protocol LocalizationHelperDelegate: class {
func didUpdateLocation(_ sender: CLLocation)
}
public class LocalizationHelper: NSObject {
public weak var delegate: LocalizationHelperDelegate?
public static var shared = LocalizationHelper()
private lazy var locationManager: CLLocationManager = {
let locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.desiredAccuracy = kCLLocationAccuracyBest
return locationManager
}()
private var currentLocation: CLLocationCoordinate2D?
public func startUpdatingLocation() {
locationManager.delegate = self
locationManager.startUpdatingLocation()
}
public func stopUpdatingLocation() {
locationManager.stopUpdatingLocation()
}
public func getCurrentLocation() -> CLLocationCoordinate2D? {
return currentLocation
}
public func getLat() -> Double{
return currentLocation?.latitude ?? 0.0
}
public func getLon() -> Double{
return currentLocation?.longitude ?? 0.0
}
}
extension LocalizationHelper: CLLocationManagerDelegate {
public func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]){
guard let location = locations.first else { return }
currentLocation = location.coordinate
print("[Update location at - \(Date())] with - lat: \(currentLocation!.latitude), lng: \(currentLocation!.longitude)")
delegate?.didUpdateLocation(location)
}
}
How to use
LocalizationHelper.shared.Start()
...
let lat = LocalizationHelper.shared.getLat()
let lon = LocalizationHelper.shared.getLon()
...
LocalizationHelper.shared.Stop()
Retrieve user current location using SwiftUI and MapKit
Create instance of CLLocationManager
:
var locManager = CLLocationManager()
locManager.requestWhenInUseAuthorization()
then get the details:
var currentLocation: CLLocation!
if( CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
CLLocationManager.authorizationStatus() == .authorizedAlways){
currentLocation = locManager.location
}
then to get longitude or latitude:
let longitude = currentLocation.coordinate.longitude
let latitude = currentLocation.coordinate.latitude
If you'd like to perform it inside a swiftUI view script, create instance:
@ObservedObject var locationManager = LocationManager()
Get them separately:
var userLatitude: String {
return "\(locationManager.lastLocation?.coordinate.latitude ?? 0)"
var userLongitude: String {
return "\(locationManager.lastLocation?.coordinate.longitude ?? 0)"
Related Topics
Nsfilemanager.Defaultmanager().Fileexistsatpath Returns False Instead of True
How to Check If a String Contains Another String in Objective-C
Periodic iOS Background Location Updates
Swift - Get Device'S Wifi Ip Address
Why Is There Extra Padding At the Top of My Uitableview With Style Uitableviewstylegrouped in Ios7
How to Easily Resize/Optimize an Image Size With Ios
Status Bar and Navigation Bar Appear Over My View'S Bounds in iOS 7
How to Programmatically Get the MAC Address of an Iphone
How to Hide Uinavigationbar 1Px Bottom Line
How to Rotate a Uiimage 90 Degrees
What Is the Ibeacon Bluetooth Profile
Error: 'Flutter/Flutter.H' File Not Found When Flutter Run on Ios
How to Create Dispatch Queue in Swift 3
What's the Difference Between the Atomic and Nonatomic Attributes
Creating a Segue Programmatically