我正在尝试在我的iOS应用中获取用户位置.我首先在我的项目中包含了重置框架.然后在按钮上单击我调用核心位置api,如下所示.当我尝试在设备中安装它时,核心位置永远不会询问用户权限.当我尝试在按钮单击时获取位置时,我将获得kCLAuthorizationStatusNotDetermined作为authorisationStatus.请帮帮我.我不知道发生了什么.
- (IBAction)fetchLessAccurateLocation:(id)sender {
[self.txtLocation setText:@""];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
locationManager.distanceFilter = 1000;
if ([self shouldFetchUserLocation]) {
[locationManager startUpdatingLocation];
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的shouldFetchUserLocation方法:
-(BOOL)shouldFetchUserLocation{
BOOL shouldFetchLocation= NO;
if ([CLLocationManager locationServicesEnabled]) {
switch ([CLLocationManager authorizationStatus]) {
case kCLAuthorizationStatusAuthorized:
shouldFetchLocation= YES;
break;
case kCLAuthorizationStatusDenied:
{
UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"App level settings has been denied" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
alert= nil;
}
break;
case kCLAuthorizationStatusNotDetermined:
{
UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The user is yet …Run Code Online (Sandbox Code Playgroud) 我试图将ObjC中的旧应用程序转换为Swift作为练习练习并且遇到了一些问题.我在旧应用程序中使用它的方式,它是建立CLLocation Manager然后我会使用:
manager = [[CLLocationManager alloc]init];
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
[manager startUpdatingLocation]
Run Code Online (Sandbox Code Playgroud)
会自动调用:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
}
Run Code Online (Sandbox Code Playgroud)
从那里我可以提取我需要的所有信息.但是在swift中,没有自动完成这种方法,我无法弄清楚如何重现它.文档说明了这一点
startUpdatingLocation()
Run Code Online (Sandbox Code Playgroud)
仍将由代表调用,但它没有发生.
这是我到目前为止:
import UIKit
import corelocation
class ViewController: UIViewController,CLLocationManagerDelegate{
@IBOutlet var gpsResult : UILabel
var manager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.startUpdatingLocation()
}
func locationManager(manager:CLLocationManager, didUpdateLocations locations:AnyObject[]) {
println("locations = \(locations)")
gpsResult.text = "success" …Run Code Online (Sandbox Code Playgroud) 我无法弄清楚如何让地图从viewDidLoad放大用户位置.我尝试设置一个区域,但这不起作用.这是我的代码,任何提示?
@IBOutlet弱var mapView:MKMapView!var MapViewLocationManager:CLLocationManager!= CLLocationManager()var currentLocation:PFGeoPoint!= PFGeoPoint()
override func viewDidLoad() {
super.viewDidLoad()
self.mapView.showsUserLocation = true
mapView.delegate = self
MapViewLocationManager.delegate = self
mapView.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
我已经查找了这个问题的答案但是没有在Swift中找到答案或者确实有效.谢谢!!
我正在使用swift4.2并且Xcode 10我正在尝试使iOS应用程序使用位置服务,但它给了我一个例外:Fatal error: Unexpectedly found nil while unwrapping an Optional value
所以我试图检查位置是否返回 nil 所以我复制我的代码并打印位置并返回null,我Xcode从产品>方案>编辑方案>默认位置和调试区域中的检查位置模拟位置,并模拟到位置我选择任何一个知道问题所在吗?
import CoreLocation
class LocationVC: UIViewController,CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var currentlocation:CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startMonitoringSignificantLocationChanges()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
authorizelocationstates()
}
func authorizelocationstates(){
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
currentlocation = locationManager.location
print(currentlocation)
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试将当前用户位置上传到Parse但是我无法检索当前用户位置.我在这里按照教程并直接从那里复制了所有必要的代码但是我收到错误:当前位置错误:操作无法完成.尚未启用Places API for iOS.有关如何启用适用于iOS的Google Places API的信息,请参阅开发人员指南(https://developers.google.com/places/ios/start).
下面是我的以下viewdidload以及上传功能.
@IBOutlet weak var viewMap: GMSMapView!
var placesClient: GMSPlacesClient?
override func viewDidLoad() {
super.viewDidLoad()
placesClient = GMSPlacesClient()
let testObject = PFObject(className: "TestObject")
testObject["foo"] = "bar"
testObject.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
print("Object has been saved.")
}
let camera = GMSCameraPosition.cameraWithLatitude(33.600727, longitude: -117.900840, zoom: 16.9)
viewMap.camera = camera
viewMap.myLocationEnabled = true
viewMap.settings.myLocationButton = true
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(33.600727, -117.900840)
marker.title = "Newport Beach"
marker.snippet = "California"
marker.map …Run Code Online (Sandbox Code Playgroud) 我试图获取用户的位置很长,但我做了以下工作:
导入的核心位置
import CoreLocation
Run Code Online (Sandbox Code Playgroud)
添加核心位置代表
class ViewController: UIViewController, CLLocationManagerDelegate, AVCaptureMetadataOutputObjectsDelegate, DTDeviceDelegate {
Run Code Online (Sandbox Code Playgroud)
定义了这个变量:
var locationManager: CLLocationManager!
Run Code Online (Sandbox Code Playgroud)
然后添加此方法:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0]
let long = userLocation.coordinate.longitude;
let lat = userLocation.coordinate.latitude;
print(long, lat)
//Do What ever you want with it
}
Run Code Online (Sandbox Code Playgroud)
但该位置未得到打印,我的方法甚至没有得到它。
我将要使用“核心位置”的项目添加到我的列表中,并且该应用在我第一次运行时要求我使用位置服务。但是现在位置正在打印中。...我在做什么错?
我正在使用 Swift 构建一个 iOS 来共享位置。有谁知道如何使用 Geofire 与 Swift 来存储一个模拟器中的位置,然后在另一个模拟器中显示存储的位置?
多谢。