获取当前位置(Core Location)

Core Location框架提供了三种用于追踪设备当前位置的服务,Core Location框架从内置的蜂窝,Wi-Fi或者GPS来获取位置

  •  

    The significant-change location 

    service 提供了低耗电的方法来获取当前位置,当前位置改变时会发出通知
  • The standard location service 提供了一种可设置的方法来获取当前位置

  • Region monitoring 监视特定地区的跨越

如果程序必须使用位置服务
在程序的info.plist中添加UIRequiredDeviceCapabilities键,它是一个包含多个字符串的数组,然后添加location-services,gps字符串


1.The Standard Location Service 

[plain]
  1. Listing 1-1  Starting the standard location service  
  2. - (void)startStandardUpdates  
  3. {  
  4.     // 创建location manager  
  5.     if (nil == locationManager)  
  6.         locationManager = [[CLLocationManager alloc] init];  
  7.    
  8.     locationManager.delegate = self; 
[plain]
  1. <span style="font-size:16px;">  // 设置获取位置的精确度,越精确越耗电</span>  
[plain]
  1. <span style="font-size:16px;">    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;  
  2.    
  3.     // 设置距离过滤器,超过次距离就更新一次位置  
  4.     locationManager.distanceFilter = 500;  
  5.    
  6.     [locationManager startUpdatingLocation];  
  7. }</span>  
使用location manager之前一般要检查位置服务是否可用,
[plain]
  1. <span style="font-size:16px;">+ (BOOL)locationServicesEnabled</span>  

当位置信息更新时,会给location manager发送消息

2.Significant-Change Location Service
[plain]
  1. <span style="font-size:16px;">- (void)startSignificantChangeUpdates  
  2. {  
  3.     // Create the location manager if this object does not  
  4.     // already have one.  
  5.     if (nil == locationManager)  
  6.         locationManager = [[CLLocationManager alloc] init];  
  7.    
  8.     locationManager.delegate = self;  
  9.     [locationManager startMonitoringSignificantLocationChanges];  
  10. }</span>  
可以叫醒在后台的程序


3.Region monitoring Service
使用之前调用CLLocationManager的regionMonitoringAvailable and regionMonitoringEnabled

[plain]
  1. <span style="font-size:16px;">- (BOOL)registerRegionWithCircularOverlay:(MyCircle*)overlay andIdentifier:(NSString*)identifier  
  2. {  
  3.    // Do not create regions if support is unavailable or disabled.  
  4.    if ( ![CLLocationManager regionMonitoringAvailable] ||  
  5.         ![CLLocationManager regionMonitoringEnabled] )  
  6.       return NO;  
  7.   
  8.    // If the radius is too large, registration fails automatically,  
  9.    // so clamp the radius to the max value.  
  10.    CLLocationDegrees radius = overlay.radius;  
  11.    if (radius > self.locationManager.maximumRegionMonitoringDistance)  
  12.       radius = self.locationManager.maximumRegionMonitoringDistance;  
  13.   
  14.    // Create the region and start monitoring it.  
  15.    CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:overlay.coordinate  
  16.                         radius:radius identifier:identifier];  
  17.    [self.locationManager startMonitoringForRegion:region  
  18.                     desiredAccuracy:kCLLocationAccuracyHundredMeters];  
  19.   
  20.    [region release];  
  21.    return YES;  
  22. }</span>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用高德地图提供的定位功能来获取当前位置。以下是使用高德地图 SDK 获取当前位置的示例代码(使用 Android 平台): 首先,在项目的 build.gradle 文件中添加以下依赖: ``` implementation 'com.amap.api:location:x.y.z' // 版本号根据实际情况填写 ``` 然后,在需要获取当前位置的 Activity 中,可以按照以下方式来实现: ```java import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.widget.Toast; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationListener; public class MainActivity extends AppCompatActivity implements AMapLocationListener { private static final int REQUEST_CODE_PERMISSION_LOCATION = 100; private AMapLocationClient mLocationClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 检查定位权限是否已经授权 if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // 如果没有授权,则向用户请求定位权限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_PERMISSION_LOCATION); } else { // 如果已经授权,则开始定位 startLocation(); } } @Override protected void onDestroy() { super.onDestroy(); // 在 Activity 销毁时停止定位 if (mLocationClient != null) { mLocationClient.stopLocation(); mLocationClient.onDestroy(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_CODE_PERMISSION_LOCATION) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // 如果用户授权了定位权限,则开始定位 startLocation(); } else { // 如果用户拒绝了定位权限,则弹出 Toast 提示 Toast.makeText(this, "没有定位权限,无法获取当前位置", Toast.LENGTH_SHORT).show(); } } } private void startLocation() { // 创建一个 AMapLocationClient 的实例 mLocationClient = new AMapLocationClient(this); // 配置定位参数 AMapLocationClientOption locationOption = new AMapLocationClientOption(); locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); locationOption.setNeedAddress(true); mLocationClient.setLocationOption(locationOption); // 设置定位监听器 mLocationClient.setLocationListener(this); // 开始定位 mLocationClient.startLocation(); } @Override public void onLocationChanged(AMapLocation aMapLocation) { if (aMapLocation != null) { if (aMapLocation.getErrorCode() == 0) { // 定位成功,获取当前位置的经纬度信息 double latitude = aMapLocation.getLatitude(); double longitude = aMapLocation.getLongitude(); Toast.makeText(this, "当前位置:(" + latitude + ", " + longitude + ")", Toast.LENGTH_SHORT).show(); } else { // 定位失败,弹出 Toast 提示 Toast.makeText(this, "定位失败:" + aMapLocation.getErrorInfo(), Toast.LENGTH_SHORT).show(); } } } } ``` 在上述代码中,首先检查是否已经授权定位权限。如果没有授权,则向用户请求该权限。如果已经授权,则创建一个 AMapLocationClient 对象,并配置定位参数及定位监听器。然后调用 startLocation() 方法开始定位。在定位成功时,通过 AMapLocation 对象获取当前位置的经纬度信息。如果定位失败,则弹出 Toast 提示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值