高德Location

时间:2024-11-15 21:04:49

1.创建Demo,获取key

打开高德开发平台 → 我的应用 → 创建应用 → 创建新Key

说明:
1.发布版安全码获取:用自己的签名打包成apk安装软件,用SHA1工具查看
2.调试版安全码获取: 直接运行安装软件,再用SHA1工具查看
2.PackageName获取:打开项目的build.gradle的applicationId就是包名

2.SHAI查看地址:https://files.cnblogs.com/files/94xiyang/%E6%9F%A5%E7%9C%8BSHA1.zip

3.Jar包下载和so文件下载地址:https://pan.baidu.com/s/1SABNnVbzgrL5OOIgOulBww

4.权限:

<!-- 使用定位功能所需权限 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 定位需要的服务 -->
<service android:name="com.amap.api.location.APSService" />
<meta-data
android:name="com.amap.api.v2.apikey"
//申请的Key
android:value="f21f6d8c24f1e8f4a1a20960583*****" />

5.Demo:

public class MainActivity extends AppCompatActivity implements AMapLocationListener {
private AMapLocationClient locationClient = null;
private AMapLocationClientOption locationOption = null;
AMapLocation mCurAMapLocation;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv= (TextView) findViewById(R.id.tv);
initAmap();
startLocation();
}
private void initAmap() {
locationClient = new AMapLocationClient(this.getApplicationContext());
locationOption = new AMapLocationClientOption();
// 设置定位模式为高精度模式
locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置定位间隔时间
locationOption.setInterval(2000);
// 设置定位监听
locationClient.setLocationListener(this);
} protected void startLocation() {
// 设置定位参数
locationClient.setLocationOption(locationOption);
// 启动定位
locationClient.startLocation();
} protected void stopLocation() {
if (locationClient.isStarted())
locationClient.stopLocation(); } @Override
public void onLocationChanged(AMapLocation location) {
// TODO Auto-generated method stub
if (location != null && location.getErrorCode() == 0) {
updateDistrictLocation(location);
} else {
updateDistrictLocation(null);
Toast.makeText(MainActivity.this,"获取位置失败",Toast.LENGTH_SHORT).show();
}
stopLocation(); } public void updateDistrictLocation(AMapLocation location) {
mCurAMapLocation = location;
Toast.makeText(MainActivity.this,location.getAddress(),Toast.LENGTH_SHORT).show();
Log.i("打印", "updateDistrictLocation: "+location.getAddress());//详细定位地址
// location.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
// location.getLatitude();//获取纬度
// location.getLongitude();//获取经度
// location.getAccuracy();//获取精度信息
// @SuppressLint("SimpleDateFormat")
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date date = new Date(location.getTime());
// df.format(date);//定位时间
// location.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。
// location.getCountry();//国家信息
// location.getProvince();//省信息
// location.getCity();//城市信息
// location.getDistrict();//城区信息
// location.getStreet();//街道信息
// location.getStreetNum();//街道门牌号信息
// location.getCityCode();//城市编码
// location.getAdCode();//地区编码
}
}