作者:UUUUUUUUUU8 | 来源:互联网 | 2023-09-15 17:37
首先需要创建一个高德开发者账号。(不会的百度下)
然后我们需要在控制台创建应用
Sha1获取可以参照:这里
packagename为你创建程序的名字,在Manifest里可以看见
然后点击创建就行了。
创建之后就可以获取到Key
接下来我们需要去下载包文件了
点击这里下载
为了省事,直接选择一键下载。快而且方便
下载完成之后,进行压缩:
将jar这个文件拷贝到项目的 app->libs里
这里一定要注意:
点击app->build.gradle
在 dependencies里加入 implementation fileTree(includes: [’*.jar’],dir:‘libs’)
还有就是package
就是如果你名字改变了,一定要将高德api的package改掉
接下来配置Manifest.xml
<uses-permission android:name&#61;"android.permission.ACCESS_COARSE_LOCATION">uses-permission><uses-permission android:name&#61;"android.permission.ACCESS_FINE_LOCATION">uses-permission><uses-permission android:name&#61;"android.permission.ACCESS_NETWORK_STATE">uses-permission><uses-permission android:name&#61;"android.permission.ACCESS_WIFI_STATE">uses-permission><uses-permission android:name&#61;"android.permission.CHANGE_WIFI_STATE">uses-permission><uses-permission android:name&#61;"android.permission.INTERNET">uses-permission><uses-permission android:name&#61;"android.permission.READ_PHONE_STATE">uses-permission><uses-permission android:name&#61;"android.permission.WRITE_EXTERNAL_STORAGE">uses-permission><uses-permission android:name&#61;"android.permission.ACCESS_LOCATION_EXTRA_COMMANDS">uses-permission><uses-permission android:name&#61;"android.permission.FOREGROUND_SERVICE"/><uses-permission android:name&#61;"android.permission.ACCESS_BACKGROUND_LOCATION"/>
还需要在application中加入apikey和server
<meta-data android:name&#61;"com.amap.api.v2.apikey" android:value&#61;"92aa621ff8c377821aa7b6e412e7a706">
<service android:name&#61;"com.amap.api.location.APSService">service>
完整代码&#xff1a;
<manifest xmlns:android&#61;"http://schemas.android.com/apk/res/android"package&#61;"com.example.maptest"><uses-permission android:name&#61;"android.permission.ACCESS_COARSE_LOCATION">uses-permission><uses-permission android:name&#61;"android.permission.ACCESS_FINE_LOCATION">uses-permission><uses-permission android:name&#61;"android.permission.ACCESS_NETWORK_STATE">uses-permission><uses-permission android:name&#61;"android.permission.ACCESS_WIFI_STATE">uses-permission><uses-permission android:name&#61;"android.permission.CHANGE_WIFI_STATE">uses-permission><uses-permission android:name&#61;"android.permission.INTERNET">uses-permission><uses-permission android:name&#61;"android.permission.READ_PHONE_STATE">uses-permission><uses-permission android:name&#61;"android.permission.WRITE_EXTERNAL_STORAGE">uses-permission><uses-permission android:name&#61;"android.permission.ACCESS_LOCATION_EXTRA_COMMANDS">uses-permission><uses-permission android:name&#61;"android.permission.FOREGROUND_SERVICE"/><uses-permission android:name&#61;"android.permission.ACCESS_BACKGROUND_LOCATION"/><applicationandroid:allowBackup&#61;"true"android:icon&#61;"&#64;mipmap/ic_launcher"android:label&#61;"&#64;string/app_name"android:roundIcon&#61;"&#64;mipmap/ic_launcher_round"android:supportsRtl&#61;"true"android:theme&#61;"&#64;style/Theme.MapTest"><meta-data android:name&#61;"com.amap.api.v2.apikey" android:value&#61;"92aa621ff8c377821aa7b6e412e7a706">meta-data><activityandroid:name&#61;".MainActivity"android:exported&#61;"true"><intent-filter><action android:name&#61;"android.intent.action.MAIN" /><category android:name&#61;"android.intent.category.LAUNCHER" />intent-filter>activity><service android:name&#61;"com.amap.api.location.APSService">service>application>manifest>
activitymain.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"xmlns:app&#61;"http://schemas.android.com/apk/res-auto"xmlns:tools&#61;"http://schemas.android.com/tools"android:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent"tools:context&#61;".MainActivity"><LinearLayoutandroid:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent"android:orientation&#61;"vertical"><TextViewandroid:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:id&#61;"&#64;&#43;id/position_text" />LinearLayout>androidx.constraintlayout.widget.ConstraintLayout>
MainActivity:
package com.example.maptest;import androidx.appcompat.app.AppCompatActivity;import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;public class MainActivity extends AppCompatActivity {public AMapLocationClient mLocationClient&#61;null;public AMapLocationListener mapLocationListener&#61;new AMapLocationListener() {&#64;Overridepublic void onLocationChanged(AMapLocation aMapLocation) {if(aMapLocation!&#61;null){if(aMapLocation.getErrorCode()&#61;&#61;0){double latitude&#61;aMapLocation.getLatitude();double Longitude&#61;aMapLocation.getLongitude();String province&#61;aMapLocation.getProvince();String city&#61;aMapLocation.getCity();String district&#61;aMapLocation.getDistrict();String streetNumber&#61;aMapLocation.getStreetNum();String text&#61;"经度: "&#43;Longitude&#43;"\n"&#43;"纬度: "&#43;latitude&#43;"\n"&#43;"详细位置: "&#43;province&#43;city&#43;district&#43;streetNumber;position.setText(text);}else{Log.e("AmapError","location Error, ErrCode:"&#43; aMapLocation.getErrorCode() &#43; ", errInfo:"&#43; aMapLocation.getErrorInfo());position.setText("定位失败");}}}};public AMapLocationClientOption mLocationOption&#61;null;private TextView position;&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);position&#61;findViewById(R.id.position_text);AMapLocationClient.updatePrivacyShow(getApplicationContext(),true,true);AMapLocationClient.updatePrivacyAgree(getApplicationContext(),true);try {mLocationClient&#61;new AMapLocationClient(getApplicationContext());} catch (Exception e) {e.printStackTrace();}mLocationClient.setLocationListener(mapLocationListener);mLocationOption&#61;new AMapLocationClientOption();mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);if(null!&#61;mLocationClient){mLocationClient.setLocationOption(mLocationOption);mLocationClient.stopLocation();mLocationClient.startLocation();}}public void onDestroy(){super.onDestroy();mLocationClient.onDestroy();}public static String sHA1(Context context){try {PackageInfo info &#61; context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);byte[] cert &#61; info.signatures[0].toByteArray();MessageDigest md &#61; MessageDigest.getInstance("SHA1");byte[] publicKey &#61; md.digest(cert);StringBuffer hexString &#61; new StringBuffer();for (int i &#61; 0; i < publicKey.length; i&#43;&#43;) {String appendString &#61; Integer.toHexString(0xFF & publicKey[i]).toUpperCase(Locale.US);if (appendString.length() &#61;&#61; 1)hexString.append("0");hexString.append(appendString);hexString.append(":");}String result &#61; hexString.toString();return result.substring(0, result.length()-1);} catch (PackageManager.NameNotFoundException e) {e.printStackTrace();} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return null;}
}
这里注意一下。如果你显示不了定位的话&#xff0c;直接调用sHA1这个方法进行获取自己的SHA1值。然后拷贝到控制台的Sha1里。
最后截图&#xff1a;
如果有什么不知道的。可以评论区留言。看到会回复