|
//
// TourLocationManager.swift
// PaiAi
//
// Created by zhengjianfei on 2016/12/23.
// Copyright © 2016年 FFIB. All rights reserved.
//
import UIKit
import CoreLocation
let minInterval = 60.0
let maxInterval = 60.0
let minGather_at = 60.0
let maxGather_at = 900.0
class TourLocationManager: CLLocationManager, CLLocationManagerDelegate {
let bgTask = BGTask.shareTask()
var interval = 0.0
var gathered_at = 0.0
var isCollect = false
var currentCoordinate = CLLocationCoordinate2D()
static let instance = TourLocationManager()
override init() {
super.init()
self.delegate = self
self.desiredAccuracy = kCLLocationAccuracyBest
self.distanceFilter = kCLDistanceFilterNone
self.requestWhenInUseAuthorization()
self.pausesLocationUpdatesAutomatically = false
}
class var sharedManager: TourLocationManager {
return instance
}
func startLocation() {
if CLLocationManager.locationServicesEnabled() {
let status = CLLocationManager.authorizationStatus()
if status == .denied || status == .notDetermined {
printLog("authorizationStatus failed")
} else {
printLog("authorizationStatus authorized")
startUpdatingLocation()
perform(#selector(stopLocation), with: nil, afterDelay: gathered_at)
}
} else {
printLog("location servicses false")
}
}
@objc func stopLocation() {
printLog("stopping locating")
stopUpdatingLocation()
NotificationCenter.default.post(name: NSNotification.Name("startPoll"), object: nil)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
printLog("update location")
let userLocation = locations[0]
let marsCoordinate = userLocation.transformationWorldCoordinateToMars()
let geo = CLGeocoder()
currentCoordinate = marsCoordinate
geo.reverseGeocodeLocation(userLocation) { (placemarks, _) in
NotificationCenter.default.post(name: Notification.Name("location"), object: nil, userInfo: ["position": placemarks?.first?.name ?? ""])
}
submitUserLocationData(lat: marsCoordinate.latitude, long: marsCoordinate.longitude)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
printLog(error)
}
var latitude: Double = 39.9266863406
var longtitude: Double = 116.285584532
func submitUserLocationData(lat: Double, long: Double) {
let params = ["user_id": SharedUserInfo.userId, "lon": long, "lat": lat] as [String : AnyObject]
let request = StatusNetworkRequest(param: params, path: .submitLocation)
NetworkApi.share.post(request: request) { _ in }
}
}
|