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配置文件