博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
高德定位
阅读量:6262 次
发布时间:2019-06-22

本文共 6119 字,大约阅读时间需要 20 分钟。

import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.CompoundButton; 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; import com.amap.api.maps.AMap; import com.amap.api.maps.LocationSource; import com.amap.api.maps.MapView; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Locale; public class MainActivity extends AppCompatActivity implements LocationSource,AMapLocationListener,         CompoundButton.OnCheckedChangeListener {
private MapView mMapView; private AMap aMap; private AMapLocationClient mlocationClient; private AMapLocationClientOption mLocationOption; private OnLocationChangedListener mListener; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mMapView = (MapView) findViewById(R.id.map); mMapView.onCreate(savedInstanceState); //设置监听 aMap = mMapView.getMap(); aMap.setLocationSource(this); aMap.getUiSettings().setMyLocationButtonEnabled(true);//设置默认按钮是否显示 aMap.setMyLocationEnabled(true); aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);//设置定位的模式为定位模式 } @Override public void onLocationChanged(AMapLocation aMapLocation) {
Log.i("TAG","onLocationChanged"+1); if (mListener != null && aMapLocation != null) {
if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {
Log.i("TAG", "定位成功"); // Toast.makeText(MainActivity.this, "定位成功", Toast.LENGTH_LONG).show(); mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点 } else {
String errText = "定位失败," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo(); Toast.makeText(MainActivity.this, errText, Toast.LENGTH_LONG).show(); //Log.e("AmapErr", errText); } } } @Override public void activate(OnLocationChangedListener onLocationChangedListener) {
Log.i("TAG","activate"+2); mListener = onLocationChangedListener; if (mlocationClient == null) {
mlocationClient = new AMapLocationClient(this); mLocationOption = new AMapLocationClientOption(); //设置定位监听 mlocationClient.setLocationListener(this); //设置为高精度定位模式 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置定位参数 mlocationClient.setLocationOption(mLocationOption); // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗, // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求 // 在定位结束后,在合适的生命周期调用onDestroy()方法 // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除 mlocationClient.startLocation(); } } @Override public void deactivate() {
Log.i("TAG","deactivate"+3); mListener = null; if (mlocationClient != null) {
mlocationClient.stopLocation(); mlocationClient.onDestroy(); } mlocationClient = null; } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("TAG","onCheckedChanged"+4); aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE); } public String sHA1(Context context) {
Log.i("TAG","sHA1"+5); try {
PackageInfo info = context.getPackageManager().getPackageInfo( context.getPackageName(), PackageManager.GET_SIGNATURES); byte[] cert = info.signatures[0].toByteArray(); MessageDigest md = MessageDigest.getInstance("SHA1"); byte[] publicKey = md.digest(cert); StringBuffer hexString = new StringBuffer(); for (int i = 0; i < publicKey.length; i++) {
String appendString = Integer.toHexString(0xFF & publicKey[i]) .toUpperCase(Locale.US); if (appendString.length() == 1) hexString.append("0"); hexString.append(appendString); hexString.append(":"); } return hexString.toString(); } catch (PackageManager.NameNotFoundException e) {
e.printStackTrace(); } catch (NoSuchAlgorithmException e) {
e.printStackTrace(); } return null; } @Override protected void onPause() {
super.onPause(); Log.i("TAG","onPause"+6); mMapView.onPause(); deactivate(); } @Override protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); Log.i("TAG","onSaveInstanceState"+7); mMapView.onSaveInstanceState(outState); } @Override protected void onResume() {
super.onResume(); Log.i("TAG","onResume"+8); mMapView.onResume(); } @Override protected void onDestroy() {
super.onDestroy(); Log.i("TAG","onDestroy"+9); mMapView.onDestroy(); mMapView.onDestroy(); if (null != mlocationClient) {
mlocationClient.onDestroy(); } } } //mainfest文件 还需要导入地图的3djar 和定位的Jar key需要自己申请 应用的包名和项目的包名需一致
//xml配置文件
 

转载于:https://www.cnblogs.com/l123/p/5675477.html

你可能感兴趣的文章
更改MySQL数据库的编码为utf8mb4
查看>>
android自动化测试--appium运行的坑问题及解决方法
查看>>
mysql Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’
查看>>
TeamCity : .NET Core 插件
查看>>
Python 爬虫知识点 - XPath
查看>>
由数量众多照片拼贴而成的马赛克图片
查看>>
如何在linux Shell脚本里面把一个数组传递到awk内部进行处理
查看>>
共模电感的原理以及使用情况
查看>>
GridLookUpEdit多列模糊查询最简单方式 z
查看>>
memcache与Redis
查看>>
Python27中Json对中文的处理
查看>>
结构,是指事物自身各种要素之间的相互关联和相互作用的方式
查看>>
andoid电阻触摸移植
查看>>
备忘录模式
查看>>
U盘安装CentOS 7卡住在 mounting configuration file system
查看>>
《Java设计模式》之装饰模式
查看>>
android开发使用SQLite之写日记
查看>>
[生活] 日常英语学习笔记-NEVER HAVE I EVER游戏
查看>>
uva103 - Stacking Boxes(DAG)
查看>>
ACdream 1127(Base Station-树状数组-2个约束条件)
查看>>