In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of Android encapsulating Amap positioning tool class Util, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will get something after reading this Android encapsulation Amap positioning tool class Util method article, let's take a look at it.
The first step is to go to the official website to create Gaode Key.
When the * is completed, click submit to get the SHA1 value as follows (obtained through the code) or from the command line, both.
The Android studio code gets the SHA1 value
Call Log.e ("--> print sha1", "${sha1 (this)}") fun sha1 (context: Context): String {try {val info: PackageInfo = context.packageManager.getPackageInfo (context.packageName) PackageManager.GET_SIGNATURES) val cert: ByteArray = info.signatures.get (0). ToByteArray () val md: MessageDigest = MessageDigest.getInstance ("SHA1") val publicKey: ByteArray = md.digest (cert) val hexString = StringBuffer () for (I in publicKey.indices) {val appendString = Integer.toHexString (0xFF and public key [I] .toInt ()) .toUpperCase (Locale.US) if (appendString.length = = 1) hexString.append ("0") hexString.append (appendString) hexString.append (":")} val result = hexString.toString () return result.substring (0 Result.length-1)} catch (e: PackageManager.NameNotFoundException) {e.printStackTrace ()} catch (e: NoSuchAlgorithmException) {e.printStackTrace ()} return ""}
The second step is to integrate SDK through Gradle (convenient):
1. Configure repositories in the build.gradle file of Project and add the address of maven or jcenter warehouse:
Allprojects {repositories {jcenter () / / or mavenCentral ()}}
2. Configure dependencies in the build.gradle file of the main project
/ / locate the configuration parameters of implementation "com.amap.api:location:latest.integration" step 3
Step 1, configure AndroidManifest.xml
Please declare the service component in the application tag, and each app has its own positioning service.
Step 2, declare permissions if you already have permissions in the project, you don't have to add them.
Step 3, set up Gaud Key:
/ / the fourth step of the key applied by the developer is to obtain the location data
Call the following code on the page where you need to obtain longitude and latitude:
Private var amapLocationUtil: AmapLocationUtil? = null fun initLocationOption () {if (null = = amapLocationUtil) {amapLocationUtil = AmapLocationUtil (CommApplication.getApplication ())} amapLocationUtilators. InitLocation () amapLocationUtilators. StartLocation () amapLocationUtilationBackListener {longitude, latitude, location, isSucdess, address-> / Log.e ("-->") "longitude" + longitude + "" + "latitude" + latitude + "" + "isSucdess" + isSucdess + "" + "address" + address) / / Log.e ("-->", location.getProvince () + "+ location.getCity () +" + location.getDistrict ()) / / isSucdess true location failed false failed if (isSucdess) {} else {/ / location failed, retry to locate amapLocationUtilator room.startLocation ()}}
Note: if you are instantiating in the current Activity, don't forget to destroy
Override fun onDestroy () {super.onDestroy () if (amapLocationUtil! = null) {amapLocationUtilability. destroyLocation ()}}
I see that AmapLocationUtil is called in the code. This is a tool class that I encapsulated, which is convenient to call. The code is as follows:
/ * * Created by JasonYin * Description: encapsulate Amap Util * * / class AmapLocationUtil (privateval mContext: Context) {private var locationClient: AMapLocationClient? = null private var locationOption: AMapLocationClientOption? = null private var mOnCallBackListener: onCallBackListener? = null / * * * initialize location * / fun initLocation () {/ / initialize client if (null = = locationClient) {locationClient = AMapLocationClient (mContext) } locationOption = defaultOption / / set location parameter locationClientlisten.setLocationOption (locationOption) / / set locationClientlistener locationClientlistener .setLocationListener (locationListener)} / / optional Set the positioning mode, the optional modes are high precision, device only, network only. The default is high-precision mode / / optional. Setting whether gps is preferred is only valid in high-precision mode. Default off / / optional to set the network request timeout. The default is 30 seconds. Invalid / / optional in device-only mode, set positioning interval. The default is 2 seconds / / optional, which sets whether to return reverse geographic address information. True / / is optional by default, which sets whether to locate at a single time. Default is false / / optional, set whether to wait for wifi refresh, default is false. If set to true, it will automatically change to a single location, do not use / / optional for continuous positioning, and set the protocol requested by the network. HTTP or HTTPS is optional. The default is HTTP / / optional, which sets whether to use the sensor. False / / is optional by default, which sets whether wifi scanning is enabled. The default is true. If it is set to false, it will stop active refresh at the same time. After stopping, it completely depends on the system refresh. There may be errors in positioning position / / optional. Set whether to use cache positioning. Default is true// optional. Set positioning mode. Optional modes are high-precision, device-only, network-only. The default is high-precision mode / / optional. Setting whether gps is preferred is only valid in high-precision mode. Default off / / if the network is available, select high-precision privateval defaultOption: AMapLocationClientOption private get () {val mOption = AMapLocationClientOption () / / if the network is available, select high-precision if (NetworkUtils.isConnected ()) {/ / optional, set the positioning mode, optional modes are high-precision, device-only, network-only. The default is high-precision mode mOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy mOption.isGpsFirst = true / / optional. Setting whether gps is preferred is only valid in high-precision mode. Turn off} else {mOption.locationMode = AMapLocationClientOption.AMapLocationMode.Device_Sensors / / optional by default, and set the positioning mode. The optional modes are high precision, device only, network only. The default is high-precision mode mOption.isGpsFirst = true / / optional. Setting whether gps is preferred is only valid in high-precision mode. Turn off} mOption.httpTimeOut = 30000 / / optional by default to set the network request timeout. The default is 30 seconds. Invalid mOption.interval in device-only mode = 2000 / / optional, set the positioning interval. The default is 2 seconds mOption.isNeedAddress = true / / optional, which sets whether to return inverse geographic address information. The default is true mOption.isOnceLocation = false / / optional, which sets whether to locate at a single time. Default is false mOption.isOnceLocationLatest = false / / optional, set whether to wait for wifi refresh, default is false. If set to true, it will automatically change to a single location, do not use AMapLocationClientOption.setLocationProtocol (AMapLocationClientOption.AMapLocationProtocol.HTTP) / / optional for continuous positioning, and set the protocol requested by the network. HTTP or HTTPS is optional. The default is HTTP mOption.isSensorEnable = true / / optional, which sets whether to use the sensor. The default is false mOption.isWifiScan = true / / optional, which sets whether wifi scanning is enabled. Default is true. If set to false, active refresh will be stopped at the same time. After stopping, it completely depends on system refresh. There may be errors in positioning position mOption.isLocationCacheEnable = true / / optional. Set whether to use cache positioning. The default is true return mOption} var locationListener = AMapLocationListener {location-> val sb = StringBuilder () if (null! = location) {/ / errCode equals 0 indicates successful positioning, and the rest indicates positioning failure For more information, please refer to the official website location error code: if (location.errorCode = = 0) {longitude = location.longitude latitude = location.latitudeval district = location.district locationSuccess (longitude, latitude, true, location, district) / / successfully, stop positioning: if the location is real-time Turn stopLocation () off stopLocation ()} else {/ / location failed / / sb.append ("location failure" + "") / / sb.append ("error Code:" + location.getErrorCode () + "); / / sb.append (" error message: "+ location.getErrorInfo () +"); / / sb.append ("error description:" + location.getLocationDetail () + "") / / Log.e ("--> location failed", sb.toString ()) LocationFarile (false, location)}} else {LocationFarile (false, location)}} private fun LocationFarile (isSucdess: Boolean, location: AMapLocation) {if (mOnCallBackListener! = null) {onCallBackListenerBack. OnCallBack (0.0,0.0, location, false, "")} fun locationSuccess (longitude: Double, latitude: Double, isSucdess: Boolean Location: AMapLocation?, address: String?) {if (mOnCallBackListener! = null) {onCallBack (longitude, latitude, location, true, address)}} fun setOnCallBackListener (listener: onCallBackListener?) {mOnCallBackListener = listener} interface onCallBackListener {fun onCallBack (longitude: Double, latitude: Double, location: AMapLocation?, isSucdess: Boolean Address: String?)} / * start location * / fun startLocation () {locationClientdestroy location.startLocation ()} / * stop location * / fun stopLocation () {locationClientdestroy location. StopLocation ()} / * * destroy location * / fun destroyLocation () { If (null! = locationClient) {/ * if AMapLocationClient is instantiated in the current Activity * be sure to execute the onDestroy of AMapLocationClient in the onDestroy of Activity * / locationClientClient.onDestroy () locationClient = null locationOption = null}} companion object {var longitude = 0.0 var latitude = 0.0}} on "Android encapsulates the method of Util of the Amap positioning tool class". This is the end of the article. Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "Android encapsulating Amap positioning tools Util". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.