image.png 目录 LocationManager LocationManager是Android 提供的Location 服务,来获得当前的位置信息和卫星信息。 LocationManager lm; lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 定位服务的提供一般有 =(LocationManager)getSystemService(Context.LOCATION_SERVICE); //获取所有可用的位置提供器 provider =LocationManager.GPS_PROVIDER; Location location=locationManager.getLastKnownLocation(provider (provider, 1000, 1, locationListener); } //LocationListener 用于当位置信息变化时由 locationManager 调用
LocationManager 在Android中可以根据LocationManager来获取设备所在的地理信息 根据需求可以将定位的代码移动到所需的地方或者可以稍加改动获取城市的信息 MainActivity locationManager; private String locationProvider; @Override protected void onCreate(Bundle 引用 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // if (providers.contains(LocationManager.GPS_PROVIDER)) { locationProvider = LocationManager.GPS_PROVIDER ; } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) { locationProvider
10s _locationManager.reGeocodeTimeout = 10; // 带逆地理(返回坐标和地址信息)。 ;,然后用成员变量 _locationManager 进行定位操作 如果有两个(或多个)控制器可能用到定位功能,岂不是每个地方都要写这么多代码? orangeColor]; //这里不用_locationManager,不声明locationManager属性,new一个新的AMapLocationManager AMapLocationManager *locationManager = [AMapLocationManager manager]; [locationManager requestLocationWithReGeocode 属性的问题 使用runtime的关联对象,为分类的locationManager属性赋值(set && get方法) locationManager的创建,也可以使用分类,使用AMapLocationManager
#import "LocationManager.h" @implementation LocationManager CLLocationManager *_locationManager;// _locationManager.delegate = self; #pragma mark - 定位失败 -(void)locationManager:(CLLocationManager *)manager requestWhenInUseAuthorization requestAlwaysAuthorization 最终,我们在LocationManager.m中的代码是: #import "LocationManager.h " @implementation LocationManager CLLocationManager *_locationManager;//定位服务 NSString *_currentCity 中调用了 var locationManager : LocationManager = LocationManager() ...
闭包返回信息:定位Location+反编译地址信息+error git地址:https://github.com/RainManGO/LocationManager 使用代码: LocationManager.shareManager.creatLocationManager "没有错误")") } 封装代码: // // LocationManager.swift // ZYLocationManager // // Created by Nvr on All rights reserved. // import UIKit import CoreLocation class LocationManager: NSObject { ->() //MARK:-属性 ///单例,唯一调用方法 static let shareManager:LocationManager = LocationManager startUpdatingLocation() print("定位开始") } } } extension LocationManager:
对象 locationManager = (LocationManager) getSystemService(serviceName); 第二步 指定LocationManager的定位方法 ,然后调用 LocationManager.getLastKnowLocation()方法获取当前位置 代码如下 // 指定LocationManager的定位方法 String provider = LocationManager.GPS_PROVIDER 首先我们要调用requestLocationUpdates,代码如下 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER , 3000, 0, mLocationListener01); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER = null) { locationManager.removeUpdates(mLocationListener01); locationManager
= (LocationManager) this .getSystemService(Context.LOCATION_SERVICE); LocationProvider provider = locationManager.getProvider(mMockProviderName); if (provider ! = null) { locationManager.addTestProvider( provider.getName() (mMockProviderName, true); //新 locationManager.setTestProviderStatus(mMockProviderName (LocationManager.GPS_PROVIDER); } catch (Exception ex) { // 若未成功addTestProvider
//初始化实例 _locationManager = [[BMKLocationManager alloc] init]; //设置delegate _locationManager.delegate _locationManager.distanceFilter = kCLDistanceFilterNone; //设置预期精度参数 _locationManager.desiredAccuracy = YES; //设置位置获取超时时间 _locationManager.locationTimeout = 10; //设置获取地址信息超时时间 _locationManager.reGeocodeTimeout 代码如下: [self.locationManager startUpdatingLocation]; 如果需要持续定位返回地址信息(需要联网),请设置如下: [self.locationManager self.locationManager) { self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate
{ let locationManager: CLLocationManager = object as! , toObject object: AnyObject) { let locationManager: CLLocationManager = object as! CLLocationManagerDelegate) } else { locationManager.delegate = nil } = CLLocationManager.authorizationStatus() guard let locationManager = locationManager else { () locationManager.startUpdatingLocation() } } 这里的service层就是将之前扩展的LocationManager再次封装。
一、导入框架 import CoreLocation 二、初始化CLLocationManager locationManager = CLLocationManager() locationManager.delegate = self //必须满足始终允许地理位置访问 locationManager.requestAlwaysAuthorization() locationManager.pausesLocationUpdatesAutomatically = false if #available(iOS 9.0, *) { locationManager.allowsBackgroundLocationUpdates = true } else { radius = self.locationManager.maximumRegionMonitoringDistance } (for: region) locationManager.requestState(for: region) } }
= [[AMapLocationManager alloc]init]; // 设置代理对象 self.locationManager.delegate = self; // ; } //开启持续定位 [self.locationManager startUpdatingLocation]; } #pragma mark - 持续定位 - (void)alwaysLoaction{ //初始化定位管理器 self.locationManager = [[AMapLocationManager alloc]init ]; // 设置代理对象 self.locationManager.delegate = self; // 设置反地理编码 self.locationManager.locatingWithReGeocode = YES; //开启持续定位 [self.locationManager startUpdatingLocation]; } #pragma mark - 单次定位 -
() locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.distanceFilter = 1000 locationManager.requestAlwaysAuthorization() locationManager.requestWhenInUseAuthorization () locationManager.delegate = self locationManager.startUpdatingLocation() } () } } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error (“空”, “空”, “空”) } } } func locationManager(_ manager: CLLocationManager,
通过设置distanceFilter可以实现当位置改变超出一定范围时LocationManager才调用相应的代理方法。这样可以达到省电的目的。 locationManager.distanceFilter = 300 //精度 比如为10 就会尽量达到10米以内的精度 locationManager.desiredAccuracy (打开后台定位的开关) //允许后台定位 locationManager.allowsBackgroundLocationUpdates = true locationManager.requestWhenInUseAuthorization () //第二种:能后台定位并且不会出现大蓝条 //locationManager.requestAlwaysAuthorization() } extension ViewController : CLLocationManagerDelegate{ //定位成功 func locationManager(_
import CoreLocation 2 class ViewController:UIViewController,CLLocationManagerDelegate 3 var locationManager = CLLocationManager() 10 locationManager.delegate = self 11 locationManager.desiredAccuracy = kCLLocationAccuracyBest 12 locationManager.distanceFilter = 1000.0 13 14 label = UILabel(frame:CGRect(x:20, y:80, width: () 21 } 22 } 23 func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: 27 break; 28 default: 29 manager.startUpdatingLocation() 30 break; 31 } 32 } 33 func locationManager
= CLLocationManager() guard let locationManager = locationManager else { return } () locationManager.startUpdatingHeading() lastLocation = locationManager.location locationManager.headingFilter = kCLHeadingFilterNone locationManager.pausesLocationUpdatesAutomatically locationManager?. startUpdatingLocation() } func stopUpdatingLocation() { locationManager?.
就像Android为电话管理支持提供了TelephonyManager类、为音频管理支持提供了AudioManager类一样,Android为GPS提供了LocationManager类,通过LocationManager ,如下代码所示: LocationManager lm = getSystemService(Context.LOCATION_SERVICE); 一旦在程序中获得了LocationManager对象之后 ,接下来即可调用LocationManager的方法来获取GPS定位的相关服务和对象。 Ø passive:由LocationManager.PASSIVE_PROVIDER常量表示。 LocationManager类提供了访问定位服务的功能,该类提供了如下常用的方法。
:CLLocationManager = CLLocationManager() let geocoder:CLGeocoder = CLGeocoder(); (3)初始化设置 locationManager.delegate = self //精确到10米,距离过滤器,定义了设备移动后获得位置信息的最小距离 locationManager.desiredAccuracy = kCLLocationAccuracyBest //十米定位一次 locationManager.distanceFilter=10.0; if((UIDevice.currentDevice().systemVersion as NSString ).doubleValue>8.0){ locationManager.requestAlwaysAuthorization() } (4)实现代理的两个方法 func locationManager CLLocation if (location.horizontalAccuracy > 0) { self.locationManager.stopUpdatingLocation
= 10 // 请求定位权限(注意这个方法只有iOS8以上才有,8之前是不用请求权限的) self.locationManager.requestAlwaysAuthorization () } 3.开始定位 func startUpdatingLocation() { // 设置代理 self.locationManager.delegate = self // 开始定位(没有网络,也可以定位) self.locationManager.startUpdatingLocation() // 设置定位30s超时 self.perform ), object: nil) } 5.停止定位 func stopUpdatingLocation(){ // 置空代理 self.locationManager.delegate = nil // 停止定位 self.locationManager.stopUpdatingLocation() } 三、CLLocation对象详解 open var coordinate
= Java.use("android.location.LocationManager") var Location = Java.use("android.location.Location ") LocationManager.getLastLocation.implementation = function(){ console.log("getLastLocation $new(LocationManager.GPS_PROVIDER.value) location.setLatitude(Latitude) location.setLongitude $new(LocationManager.GPS_PROVIDER.value) location.setLatitude(Latitude) location.setLongitude $new() array.add("gps") return array } LocationManager.getProviders.overload
If locationManager:didUpdateLocations: is * implemented, this method will not be called. */ - (void Ifimplemented, updates will * not be delivered to locationManager:didUpdateToLocation:fromLocation : (CLLocationManager *)manager didVisit: (CLVisit*)visit; 1.2 使用示例 1.2.1 locationManager初始化 self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; self.locationManager.distanceFilter = 1000.0f; [self.locationManager startUpdatingLocation