当前位置: 首页 > news >正文

网站开发网页设计js怎么做网址

网站开发网页设计js,怎么做网址,南京专业网站开发团队,网站建设 在电商的作用Android中获取定位信息的方式有很多种#xff0c;系统自带的LocationManager#xff0c;以及第三方厂商提供的一些定位sdk#xff0c;都能帮助我们获取当前经纬度#xff0c;但第三方厂商一般都需要申请相关的key#xff0c;且调用量高时#xff0c;还会产生资费问题。这…Android中获取定位信息的方式有很多种系统自带的LocationManager以及第三方厂商提供的一些定位sdk都能帮助我们获取当前经纬度但第三方厂商一般都需要申请相关的key且调用量高时还会产生资费问题。这里采用LocationManager FusedLocationProviderClient 的方式进行经纬度的获取以解决普通场景下获取经纬度和经纬度转换地址的功能。 一添加定位权限 !--允许获取精确位置精准定位必选-- uses-permission android:nameandroid.permission.ACCESS_FINE_LOCATION /uses-permission android:nameandroid.permission.ACCESS_COARSE_LOCATION / !--后台获取位置信息若需后台定位则必选-- uses-permission android:nameandroid.permission.ACCESS_BACKGROUND_LOCATION / !--用于申请调用A-GPS模块,卫星定位加速-- uses-permission android:nameandroid.permission.ACCESS_LOCATION_EXTRA_COMMANDS / 二添加依赖库 implementation org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4implementation androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2implementation com.google.android.gms:play-services-location:21.0.1 三使用LocationManager获取当前经纬度 获取经纬度时可根据自己的诉求进行参数自定义如果对经纬度要求不是很精确的可以自行配置Criteria里面的参数。 获取定位前需要先判断相关的服务是否可用获取定位的服务其实有很多种选择因为个人项目对经纬度准确性要求较高为了保证获取的成功率和准确性只使用了GPS和网络定位两种如果在国内还会有基站获取等方式可以自行修改。 import android.Manifest.permission import android.location.* import android.os.Bundle import android.util.Log import androidx.annotation.RequiresPermission import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withTimeout import kotlin.coroutines.resumeobject LocationManagerUtils {val TAG LocationManagerUtils/*** mLocationManager 传入LocationManager对象* minDistance 位置变化最小距离当位置距离变化超过此值时将更新位置信息单位米* timeOut 超时时间如果超时未返回则直接使用默认值*/RequiresPermission(anyOf [permission.ACCESS_COARSE_LOCATION, permission.ACCESS_FINE_LOCATION])suspend fun getCurrentPosition(mLocationManager: LocationManager,timeOut: Long 3000,):Location{var locationListener : LocationListener?nullreturn try {//超时未返回则直接获取失败返回默认值withTimeout(timeOut){suspendCancellableCoroutine {continuation -//获取最佳定位方式如果获取不到则默认采用网络定位。var bestProvider mLocationManager.getBestProvider(createCriteria(),true)if (bestProvider.isNullOrEmpty()||bestProvider passive){bestProvider network}Log.d(TAG, getCurrentPosition:bestProvider:${bestProvider})locationListener object : LocationListener {override fun onLocationChanged(location: Location) {Log.d(TAG, getCurrentPosition:onCompete:${location.latitude},${location.longitude})if (continuation.isActive){continuation.resume(location)mLocationManager.removeUpdates(this)}}override fun onProviderDisabled(provider: String) {}override fun onProviderEnabled(provider: String) {}}//开始定位mLocationManager.requestLocationUpdates(bestProvider,1000,0f,locationListener!!)}}}catch (e:Exception){try {locationListener?.let {mLocationManager.removeUpdates(it)}}catch (e:Exception){Log.d(TAG, getCurrentPosition:removeUpdate:${e.message})}//超时直接返回默认的空对象Log.d(TAG, getCurrentPosition:onError:${e.message})return createDefaultLocation()}}RequiresPermission(anyOf [permission.ACCESS_COARSE_LOCATION, permission.ACCESS_FINE_LOCATION])suspend fun repeatLocation(mLocationManager: LocationManager):Location{return suspendCancellableCoroutine {continuation -//获取最佳定位方式如果获取不到则默认采用网络定位。var bestProvider mLocationManager.getBestProvider(createCriteria(),true)if (bestProvider.isNullOrEmpty()||bestProvider passive){bestProvider network}Log.d(TAG, getCurrentPosition:bestProvider:${bestProvider})val locationListener object : LocationListener {override fun onLocationChanged(location: Location) {Log.d(TAG, getCurrentPosition:onCompete:${location.latitude},${location.longitude})if (continuation.isActive){continuation.resume(location)}mLocationManager.removeUpdates(this)}override fun onProviderDisabled(provider: String) {}override fun onProviderEnabled(provider: String) {}}//开始定位mLocationManager.requestLocationUpdates(bestProvider,1000, 0f, locationListener)}}RequiresPermission(anyOf [permission.ACCESS_COARSE_LOCATION, permission.ACCESS_FINE_LOCATION])fun getLastLocation( mLocationManager: LocationManager): Location {//获取最佳定位方式如果获取不到则默认采用网络定位。var currentProvider mLocationManager.getBestProvider(createCriteria(), true)if (currentProvider.isNullOrEmpty()||currentProvider passive){currentProvider network}return mLocationManager.getLastKnownLocation(currentProvider) ?: createDefaultLocation()}//创建定位默认值fun createDefaultLocation():Location{val location Location(network)location.longitude 0.0location.latitude 0.0return location}private fun createCriteria():Criteria{return Criteria().apply {accuracy Criteria.ACCURACY_FINEisAltitudeRequired falseisBearingRequired falseisCostAllowed truepowerRequirement Criteria.POWER_HIGHisSpeedRequired false}}///定位是否可用fun checkLocationManagerAvailable(mLocationManager: LocationManager):Boolean{return mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)||mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)} } 二使用FusedLocationProviderClient 在获取经纬度时会出现各种异常的场景会导致成功的回调一直无法触发这里使用了协程如果超过指定超时时间未返回则直接默认为获取失败进行下一步的处理。 import android.Manifest import android.app.Activity import android.content.Context import android.content.Context.LOCATION_SERVICE import android.content.Intent import android.location.Geocoder import android.location.Location import android.location.LocationManager import android.provider.Settings import android.util.Log import androidx.annotation.RequiresPermission import com.google.android.gms.location.LocationServices import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withContext import kotlinx.coroutines.withTimeout import java.io.IOException import java.util.* import kotlin.coroutines.resumeobject FusedLocationProviderUtils {val TAG FusedLocationUtilsRequiresPermission(anyOf [android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_FINE_LOCATION])suspend fun checkFusedLocationProviderAvailable(fusedLocationClient: FusedLocationProviderClient):Boolean{return try {withTimeout(1000){suspendCancellableCoroutine { continuation -fusedLocationClient.locationAvailability.addOnFailureListener {Log.d(TAG, locationAvailability:addOnFailureListener:${it.message})if (continuation.isActive){continuation.resume(false)}}.addOnSuccessListener {Log.d(TAG, locationAvailability:addOnSuccessListener:${it.isLocationAvailable})if (continuation.isActive){continuation.resume(it.isLocationAvailable)}}}}}catch (e:Exception){return false}}///获取最后已知的定位信息RequiresPermission(anyOf [android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_FINE_LOCATION])suspend fun getLastLocation(fusedLocationClient: FusedLocationProviderClient):Location{return suspendCancellableCoroutine {continuation -fusedLocationClient.lastLocation.addOnSuccessListener {if (continuation.isActive){Log.d(TAG, current location success:$it)if (it ! null){continuation.resume(it)}else{continuation.resume(createDefaultLocation())}}}.addOnFailureListener {continuation.resume(createDefaultLocation())}}}/*** 获取当前定位需要申请定位权限**/RequiresPermission(anyOf [android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_FINE_LOCATION])suspend fun getCurrentPosition(fusedLocationClient: FusedLocationProviderClient): Location {return suspendCancellableCoroutine {continuation -fusedLocationClient.getCurrentLocation(createLocationRequest(),object : CancellationToken(){override fun onCanceledRequested(p0: OnTokenCanceledListener): CancellationToken {return CancellationTokenSource().token}override fun isCancellationRequested(): Boolean {return false}}).addOnSuccessListener {if (continuation.isActive){Log.d(TAG, current location success:$it)if (it ! null){continuation.resume(it)}else{continuation.resume(createDefaultLocation())}}}.addOnFailureListener {Log.d(TAG, current location fail:$it)if (continuation.isActive){continuation.resume(createDefaultLocation())}}.addOnCanceledListener {Log.d(TAG, current location cancel:)if (continuation.isActive){continuation.resume(createDefaultLocation())}}}}//创建当前LocationRequest对象private fun createLocationRequest():CurrentLocationRequest{return CurrentLocationRequest.Builder().setDurationMillis(1000).setMaxUpdateAgeMillis(5000).setPriority(Priority.PRIORITY_HIGH_ACCURACY).build()}//创建默认值private fun createDefaultLocation():Location{val location Location(network)location.longitude 0.0location.latitude 0.0return location} } 三整合LocationManager和FusedLocationProviderClient 在获取定位时可能会出现GPS定位未开启的情况所以不管是LocationManager或FusedLocationProviderClient都需要判断当前服务是否可用获取定位时如果GPS信号较弱等异常情况下就需要考虑到获取定位超时的情况这里使用了协程如FusedLocationProviderClient超过3秒未获取成功则直接切换到LocationManager进行二次获取这是提升获取经纬度成功的关键。 在实际项目中如果对获取经纬度有较高的考核要求时通过结合LocationManager和FusedLocationProviderClient如果还是获取不到可考虑集成第三方的进行进一步获取可以考虑使用华为的免费融合定位服务因为我们使用过百度地图的sdk每天会出现千万分之五左右的定位错误和定位漂移问题。 import android.Manifest import android.app.Activity import android.content.Context import android.content.Context.LOCATION_SERVICE import android.content.Intent import android.location.Geocoder import android.location.Location import android.location.LocationManager import android.provider.Settings import android.util.Log import androidx.annotation.RequiresPermission import com.google.android.gms.location.LocationServices import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withContext import kotlinx.coroutines.withTimeout import java.io.IOException import java.util.* import kotlin.coroutines.resumeobject LocationHelper {fun getLocationServiceStatus(context: Context):Boolean{return (context.getSystemService(LOCATION_SERVICE) as LocationManager).isProviderEnabled(LocationManager.GPS_PROVIDER)}/*** 打开定位服务设置*/fun openLocationSetting(context: Context):Boolean{return try {val settingsIntent Intent()settingsIntent.action Settings.ACTION_LOCATION_SOURCE_SETTINGSsettingsIntent.addCategory(Intent.CATEGORY_DEFAULT)settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)settingsIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)context.startActivity(settingsIntent)true} catch (ex: java.lang.Exception) {false}}/*** 获取当前定位*/RequiresPermission(anyOf [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])suspend fun getLocation(context: Activity,timeOut: Long 2000):Location{val location getLocationByFusedLocationProviderClient(context)//默认使用FusedLocationProviderClient 如果FusedLocationProviderClient不可用或获取失败则使用LocationManager进行二次获取Log.d(LocationHelper, getLocation:$location)return if (location.latitude 0.0){getLocationByLocationManager(context, timeOut)}else{location}}RequiresPermission(anyOf [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])private suspend fun getLocationByLocationManager(context: Activity,timeOut: Long 2000):Location{Log.d(LocationHelper, getLocationByLocationManager)val locationManager context.getSystemService(LOCATION_SERVICE) as LocationManager//检查LocationManager是否可用return if (LocationManagerUtils.checkLocationManagerAvailable(locationManager)){//使用LocationManager获取当前经纬度val location LocationManagerUtils.getCurrentPosition(locationManager, timeOut)if (location.latitude 0.0){LocationManagerUtils.getLastLocation(locationManager)}else{location}}else{//获取失败则采用默认经纬度LocationManagerUtils.createDefaultLocation()}}RequiresPermission(anyOf [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])private suspend fun getLocationByFusedLocationProviderClient(context: Activity):Location{Log.d(LocationHelper, getLocationByFusedLocationProviderClient)//使用FusedLocationProviderClient进行定位val fusedLocationClient LocationServices.getFusedLocationProviderClient(context)return if (FusedLocationProviderUtils.checkFusedLocationProviderAvailable(fusedLocationClient)){withContext(Dispatchers.IO){//使用FusedLocationProviderClient获取当前经纬度val location FusedLocationProviderUtils.getCurrentPosition(fusedLocationClient)if (location.latitude 0.0){FusedLocationProviderUtils.getLastLocation(fusedLocationClient)}else{location}}}else{LocationManagerUtils.createDefaultLocation()}} } 注因为获取定位是比较耗电的操作在实际使用时可增加缓存机制比如2分钟之内频繁则返回上一次缓存的数据如果超过2分钟则重新获取一次并缓存起来。 四获取当前经纬度信息或经纬度转换地址 1获取当前经纬度 RequiresPermission(anyOf [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])fun getCurrentLocation(activity:Activity){if (activity ! null){val exceptionHandler CoroutineExceptionHandler { _, exception -}viewModelScope.launch(exceptionHandler) {val location LocationHelper.getLocation(activity!!)val map HashMapString,String()map[latitude] ${location.latitude}map[longitude] ${location.longitude}}}} 2经纬度转换地址 /*** param latitude 经度* param longitude 纬度* return 详细位置信息*/suspend fun convertAddress(context: Context, latitude: Double, longitude: Double): String {return try {withTimeout(3000){suspendCancellableCoroutine { continuation -try {val mGeocoder Geocoder(context, Locale.getDefault())val mStringBuilder StringBuilder()if (Geocoder.isPresent()){val mAddresses mGeocoder.getFromLocation(latitude, longitude, 1)if (mAddresses! null mAddresses.size 0) {val address mAddresses[0]Log.d(LocationUtils, convertAddress()---$address)mStringBuilder.append(address.getAddressLine(0)?:).append(,).append(address.adminArea?:address.subAdminArea?:).append(,).append(address.locality?:address.subLocality?:).append(,).append(address.thoroughfare?:address.subThoroughfare?:)}}if (continuation.isActive){continuation.resume(mStringBuilder.toString())}} catch (e: IOException) {Log.d(LocationUtils, convertAddress()--IOException-${e.message})if (continuation.isActive){continuation.resume()}}}}}catch (e:Exception){Log.d(LocationUtils, convertAddress()---timeout)return }} 调用时 fun covertAddress(latitude:double,longitude:double){if (activity ! null){val exceptionHandler CoroutineExceptionHandler { _, exception -}viewModelScope.launch(exceptionHandler) {val hashMap argument as HashMap*, *withContext(Dispatchers.IO){val address LocationHelper.convertAddress(activity!!,${hashMap[latitude]}.toDouble(),${hashMap[longitude]}.toDouble())}}}} 注经纬度转换地址时需要开启一个线程或者协程进行转换不然会阻塞主线程引发异常。
http://www.tj-hxxt.cn/news/217562.html

相关文章:

  • 上海企业网站制作报价购物平台网站建设
  • 青海住房建设网站沈阳做企业网站的
  • 响应页手机网站源码建设审批网站查询
  • 网站开发的方法wordpress适合环境
  • 如何做视频网站技术指标tom企业邮箱官网
  • 无极在线招工招聘信息willfast优化工具下载
  • 备案 添加网站最专业网站建设哪家好
  • 苏州建站模板平台asp.net网站建设实战 pdf
  • 网站开发与推广就业wordpress pro版
  • 凡科建站怎么收费做一个app需要投资多少钱
  • 帝国系统做网站地图足球网站模板下载
  • 镇江网站建设找思创网站备案无前置审批文件
  • 株洲网站优化哪家强企业英文网站
  • 做网站的公司利润多少呢有关网站建设的知识
  • 贵州建设职业技术学院网站网站首页图片切换
  • 网站建设 淘宝详情南昌网站建设服务
  • 网站无备案号怎么办昨晚广州天河发生事件
  • 佛山新网站制作平台东莞创意网站设计
  • dede后台网站主页网页设计实训结论
  • 国外做设备网站开发一亩地多少钱
  • 做网站程序的步骤无极ip
  • 泊头公司做网站ai绘制logo
  • 跟我一起学做网站WordPress侧边栏跟随
  • 上海网站seoseodian基本的网站开发技术
  • 宜宾网站建设多少钱如何开展网站推广
  • 企业网站设计中应注意产品发布功能优化作品集的个人网站怎么做
  • 做视频网站用什么好贵阳网站建设是什么
  • 重庆网站制作服务怎么制作手机软件
  • 海口网站建设专家评价wordpress有广告插件
  • 网站建设作业做一个简单的网站清迈城市建设网站