GitHub地址:https://github.com/LoveYourselfAndMe/GaoDeMapView
下面是全部代碼復(fù)制粘貼即可
packagecom.cheyifu.businessapp.ui;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Color;
importandroid.os.Bundle;
importandroid.os.SystemClock;
importandroid.support.v7.app.AppCompatActivity;
importandroid.util.Log;
importandroid.view.animation.CycleInterpolator;
importandroid.view.animation.Interpolator;
importandroid.view.animation.LinearInterpolator;
importcom.amap.api.location.AMapLocation;
importcom.amap.api.location.AMapLocationClient;
importcom.amap.api.location.AMapLocationClientOption;
importcom.amap.api.location.AMapLocationListener;
importcom.amap.api.maps.AMap;
importcom.amap.api.maps.CameraUpdateFactory;
importcom.amap.api.maps.LocationSource;
importcom.amap.api.maps.MapView;
importcom.amap.api.maps.UiSettings;
importcom.amap.api.maps.model.BitmapDescriptorFactory;
importcom.amap.api.maps.model.Circle;
importcom.amap.api.maps.model.CircleOptions;
importcom.amap.api.maps.model.LatLng;
importcom.amap.api.maps.model.Marker;
importcom.amap.api.maps.model.MarkerOptions;
importcom.cheyifu.businessapp.R;
importcom.cheyifu.businessapp.utils.SensorEventHelper;
importjava.util.Timer;
importjava.util.TimerTask;
public classMapActivityextendsAppCompatActivityimplementsLocationSource,
AMapLocationListener,AMap.OnMapLoadedListener,AMap.OnMapClickListener {
privateMapViewmapView;
privateAMapaMap;
privateUiSettingsmUiSettings;
privateOnLocationChangedListenermListener;
privateAMapLocationClientmlocationClient;
privateAMapLocationClientOptionmLocationOption;
//? ? private static final int STROKE_COLOR = Color.argb(180, 3, 145, 255);
//? ? private static final int FILL_COLOR = Color.argb(10, 0, 0, 180);
private booleanmFirstFix=false;
privateMarkermLocMarker;
privateSensorEventHelpermSensorHelper;
privateCirclemCircle;
privateCirclec;
privatecircleTaskmTimerTask;
private longstart;
privateTimermTimer=newTimer();
private finalInterpolatorinterpolator=newCycleInterpolator(1);
private finalInterpolatorinterpolator1=newLinearInterpolator();
privateCircleac;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mapView= (MapView) findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
initMapView();
}
private voidinitMapView() {
if(aMap==null) {
aMap=mapView.getMap();
mUiSettings=aMap.getUiSettings();
}
setUpMap();
mSensorHelper=newSensorEventHelper(this);
if(mSensorHelper!=null) {
mSensorHelper.registerSensorListener();
}
}
/**
* 設(shè)置一些amap的屬性
*/
private voidsetUpMap() {
aMap.setOnMapLoadedListener(this);
aMap.setOnMapClickListener(this);
aMap.setLocationSource(this);// 設(shè)置定位監(jiān)聽
//設(shè)置比例尺
mUiSettings.setScaleControlsEnabled(true);
//設(shè)置指南針
mUiSettings.setCompassEnabled(true);
//定位按鈕
mUiSettings.setMyLocationButtonEnabled(true);
aMap.setMyLocationEnabled(true);
// 設(shè)置定位的類型為定位模式 癌椿,可以由定位扶供、跟隨或地圖根據(jù)面向方向旋轉(zhuǎn)幾種
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
}
@Override
protected voidonDestroy() {
super.onDestroy();
if(mLocMarker!=null) {
mLocMarker.destroy();
}
mapView.onDestroy();
if(null!=mlocationClient) {
mlocationClient.onDestroy();
}
if(mTimerTask!=null) {
mTimerTask.cancel();
mTimerTask=null;
}
try{
mTimer.cancel();
}catch(Throwable e) {
e.printStackTrace();
}
}
@Override
protected voidonResume() {
super.onResume();
mapView.onResume();
if(mSensorHelper!=null) {
mSensorHelper.registerSensorListener();
}
}
@Override
protected voidonPause() {
super.onPause();
if(mSensorHelper!=null) {
mSensorHelper.unRegisterSensorListener();
mSensorHelper.setCurrentMarker(null);
mSensorHelper=null;
}
mapView.onPause();
deactivate();
}
/**
* 停止定位
*/
@Override
public voiddeactivate() {
mListener=null;
if(mlocationClient!=null) {
mlocationClient.stopLocation();
mlocationClient.onDestroy();
}
mlocationClient=null;
}
@Override
protected voidonSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
public voidonLocationChanged(AMapLocation aMapLocation) {
if(mListener!=null&& aMapLocation !=null) {
if(mTimerTask!=null) {
mTimerTask.cancel();
mTimerTask=null;
}
if(aMapLocation !=null&& aMapLocation.getErrorCode() ==0) {
LatLng mylocation =newLatLng(aMapLocation.getLatitude(),
aMapLocation.getLongitude());
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mylocation,18));
addLocationMarker(aMapLocation);
mSensorHelper.setCurrentMarker(mLocMarker);//定位圖標旋轉(zhuǎn)
}else{
String errText ="定位失敗,"+ aMapLocation.getErrorCode() +": "
+ aMapLocation.getErrorInfo();
Log.e("AmapErr",errText);
}
}
}
private voidaddLocationMarker(AMapLocation aMapLocation) {
LatLng mylocation =newLatLng(aMapLocation.getLatitude(),aMapLocation.getLongitude());
floataccuracy = aMapLocation.getAccuracy();
if(mLocMarker==null) {
addMarker(mylocation);
ac=aMap.addCircle(newCircleOptions().center(mylocation)
.fillColor(Color.argb(100,255,218,185)).radius(accuracy)
.strokeColor(Color.argb(255,255,228,185)).strokeWidth(5));
c=aMap.addCircle(newCircleOptions().center(mylocation)
.fillColor(Color.argb(70,255,218,185))
.radius(accuracy).strokeColor(Color.argb(255,255,228,185))
.strokeWidth(0));
}else{
mLocMarker.setPosition(mylocation);
ac.setCenter(mylocation);
ac.setRadius(accuracy);
c.setCenter(mylocation);
c.setRadius(accuracy);
}
Scalecircle(c);
}
/**
* 開始定位原茅。
*/
private voidstartlocation() {
if(mlocationClient==null) {
mlocationClient=newAMapLocationClient(this);
mLocationOption=newAMapLocationClientOption();
//設(shè)置定位監(jiān)聽
mlocationClient.setLocationListener(this);
//設(shè)置為高精度定位模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//設(shè)置定位時間
//是指定位間隔
mLocationOption.setInterval(10000);
//設(shè)置定位參數(shù)
mlocationClient.setLocationOption(mLocationOption);
// 此方法為每隔固定時間會發(fā)起一次定位請求贾惦,為了減少電量消耗或網(wǎng)絡(luò)流量消耗滴某,
// 注意設(shè)置合適的定位時間的間隔(最小間隔支持為2000ms)辫诅,并且在合適時間調(diào)用stopLocation()方法來取消定位請求
// 在定位結(jié)束后催式,在合適的生命周期調(diào)用onDestroy()方法
// 在單次定位情況下临庇,定位無論成功與否,都無需調(diào)用stopLocation()方法移除請求麸塞,定位sdk內(nèi)部會移除
mlocationClient.startLocation();
}else{
mlocationClient.startLocation();
}
}
@Override
public voidactivate(OnLocationChangedListener listener) {
mListener= listener;
startlocation();
}
private voidaddMarker(LatLng latlng) {
if(mLocMarker!=null) {
return;
}
MarkerOptions options =newMarkerOptions();
options.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(this.getResources(),
R.mipmap.navi_map_gps_locked)));
options.anchor(0.5f,0.5f);
options.position(latlng);
mLocMarker=aMap.addMarker(options);
//? ? ? ? mLocMarker.setTitle(LOCATION_MARKER_FLAG);
}
@Override
public voidonMapClick(LatLng latLng) {
}
@Override
public voidonMapLoaded() {
}
public voidScalecircle(finalCircle circle) {
start= SystemClock.uptimeMillis();
mTimerTask=newcircleTask(circle,1000);
mTimer.schedule(mTimerTask,0,30);
}
private classcircleTaskextendsTimerTask {
private doubler;
privateCirclecircle;
private longduration=1000;
publiccircleTask(Circle circle, longrate) {
this.circle= circle;
this.r= circle.getRadius();
if(rate >0) {
this.duration= rate;
}
}
@Override
public voidrun() {
try{
longelapsed = SystemClock.uptimeMillis() -start;
floatinput = (float) elapsed /duration;
//? ? ? ? ? ? ? ? 外圈循環(huán)縮放
//? ? ? ? ? ? ? ? float t = interpolator.getInterpolation((float)(input-0.25));//return (float)(Math.sin(2 * mCycles * Math.PI * input))
//? ? ? ? ? ? ? ? double r1 = (t + 2) * r;
//? ? ? ? ? ? ? ? 外圈放大后消失
floatt =interpolator1.getInterpolation(input);
doubler1 = (t +1) *r;
circle.setRadius(r1);
if(input >2) {
start= SystemClock.uptimeMillis();
}
}catch(Throwable e) {
e.printStackTrace();
}
}
}
}