Android實作高德地圖軌跡回放
- 寫在前面
- 準備
- 官方檔案解讀
- 創建應用:
- 地圖api引入:
- 權限添加
- 效果展示
- 程序實作
- 地圖初始化
- 定位
- 顯示標記點
- 點平滑移動
- 添加呼吸點
- 寫在結尾
寫在前面
本篇文章是對近期作業專案中集成高德地圖軌跡回放和單/多點標記功能的一個總結,方便以后使用時翻閱,也方便大家,避免跟我踩一樣的坑
歡迎指正,不喜勿噴,請你善良,謝謝!!!
準備
官方檔案解讀
首先去[官網](https://lbs.amap.com/api/android-sdk/summary)查看開發指南進行專案中需要的內容準備,
創建應用:
左上角頭像位置指標劃過出現如圖所示內容
后點擊應用管理->我的應用,如下圖

上圖中創建新應用后進入如下界面

上圖兩個步驟完成后點擊新建,新建后就會在我的應用中顯示你自己創建的這個應用,我用的是test作為演示,新建完成后如下圖

點擊添加展示圖如下:

將上圖中內容編輯完成后提交,就會在應用串列中將key展示出來,這里我就不做展示了,
以上的內容就是官方檔案中獲取key的內容,相對來說還是比較簡單的,
地圖api引入:
//定位
implementation 'com.amap.api:location:5.1.0'
//搜索
implementation 'com.amap.api:search:7.3.0'
//導航
implementation 'com.amap.api:navi-3dmap:7.6.0_3dmap7.6.0'
權限添加
<!--允許程式打開網路套接字-->
<uses-permission android:name="android.permission.INTERNET" />
<!--允許程式設定內置sd卡的寫權限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--允許程式獲取網路狀態-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--允許程式訪問WiFi網路資訊-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--允許程式讀寫手機狀態和身份-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--用于進行網路定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<!--用于訪問GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!--用于獲取wifi的獲取權限,wifi資訊會用來進行網路定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<!--用于讀取手機當前的狀態-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<!--用于申請呼叫A-GPS模塊-->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<!--獲取運營商資訊,用于支持提供運營商資訊相關的介面-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--用于訪問wifi網路資訊,wifi資訊會用于進行網路定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--這個權限用于獲取wifi的獲取權限,wifi資訊會用來進行網路定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<!--用于訪問網路,網路定位需要上網-->
<uses-permission android:name="android.permission.INTERNET" />
<!--用于讀取手機當前的狀態-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--寫入擴展存盤,向擴展卡寫入資料,用于寫入快取定位資料-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
并且要在AndroidManifest.xml的application中添加如下代碼:
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="創建應用中你自己生成的key" />
<service android:name="com.amap.api.location.APSService" />
到這里準備作業就完成了,我先上個效果圖展示一下,
效果展示

有一個點呼吸的效果是我今天早上才加進去的,所以展示效果上面沒有體現出來呢,不過這個功能也不是本次專案的重點
程序實作
地圖初始化
mMapView.onCreate(savedInstanceState);
if (aMap == null) {
aMap = mMapView.getMap();
myLocationStyle = new MyLocationStyle();
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);
// 設定圓形的邊框顏色
myLocationStyle.strokeColor(Color.argb(50, 30, 150, 180));
// 設定圓形的填充顏色
myLocationStyle.radiusFillColor(Color.argb(50, 30, 150, 180));
// 設定圓形的邊框粗細
myLocationStyle.strokeWidth(1.0f);
//設定顯示定位按鈕 并且可以點擊
UiSettings settings = aMap.getUiSettings();
//設定了定位的監聽
aMap.setLocationSource(this);
// 是否顯示定位按鈕
settings.setMyLocationButtonEnabled(true);
//顯示定位層并且可以觸發定位,默認是flase
aMap.setMyLocationEnabled(true);
// 定位、且將視角移動到地圖中心點,定位點依照設備方向旋轉,并且會跟隨設備移動,
}
定位
//初始化定位
mlocationClient = new AMapLocationClient(getApplicationContext());
//設定定位回呼監聽
mlocationClient.setLocationListener(this);
//初始化定位引數
mLocationOption = new AMapLocationClientOption();
//設定定位模式為Hight_Accuracy高精度模式,Battery_Saving為低功耗模式,Device_Sensors是僅設備模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//設定是否回傳地址資訊(默認回傳地址資訊)
mLocationOption.setNeedAddress(true);
//設定是否只定位一次,默認為false
mLocationOption.setOnceLocation(false);
//設定是否強制重繪WIFI,默認為強制重繪
mLocationOption.setWifiActiveScan(true);
//設定是否允許模擬位置,默認為false,不允許模擬位置
mLocationOption.setMockEnable(false);
//設定定位間隔,單位毫秒,默認為2000ms
mLocationOption.setInterval(2000);
//給定位客戶端物件設定定位引數
mlocationClient.setLocationOption(mLocationOption);
//啟動定位
mlocationClient.startLocation();
/**
* 定位回呼
* /
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (mListener != null && aMapLocation != null) {
if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {
//定位成功回呼資訊,設定相關訊息
aMapLocation.getLocationType();//獲取當前定位結果來源,如網路定位結果,詳見官方定位型別表
aMapLocation.getLatitude();//獲取緯度
aMapLocation.getLongitude();//獲取經度
aMapLocation.getAccuracy();//獲取精度資訊
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(aMapLocation.getTime());
df.format(date);//定位時間
aMapLocation.getAddress();//地址,如果option中設定isNeedAddress為false,則沒有此結果,網路定位結果中會有地址資訊,GPS定位不回傳地址資訊,
aMapLocation.getCountry();//國家資訊
aMapLocation.getProvince();//省資訊
aMapLocation.getCity();//城市資訊
aMapLocation.getDistrict();//城區資訊
aMapLocation.getStreet();//街道資訊
aMapLocation.getStreetNum();//街道門牌號資訊
aMapLocation.getCityCode();//城市編碼
aMapLocation.getAdCode();//地區編碼
// 如果不設定標志位,此時再拖動地圖時,它會不斷將地圖移動到當前的位置
if (isFirstLoc) {
//設定縮放級別
aMap.moveCamera(CameraUpdateFactory.zoomTo(18));
//將地圖移動到定位點
aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude())));
//點擊定位按鈕 能夠將地圖的中心移動到定位點
mListener.onLocationChanged(aMapLocation);
//添加圖釘
// aMap.addMarker(getMarkerOptions(amapLocation));
//獲取定位資訊
StringBuffer buffer = new StringBuffer();
buffer.append(aMapLocation.getCountry() + ""
+ aMapLocation.getProvince() + ""
+ aMapLocation.getCity() + ""
+ aMapLocation.getProvince() + ""
+ aMapLocation.getDistrict() + ""
+ aMapLocation.getStreet() + ""
+ aMapLocation.getStreetNum());
Toast.makeText(getApplicationContext(), buffer.toString(), Toast.LENGTH_LONG).show();
isFirstLoc = false;
// 顯示系統小藍點
mListener.onLocationChanged(aMapLocation);
}
} else {
Log.e("AmapError", "location Error, ErrCode:"
+ aMapLocation.getErrorCode() + ", errInfo:"
+ aMapLocation.getErrorInfo());
Toast.makeText(getApplicationContext(), "定位失敗", Toast.LENGTH_LONG).show();
}
}
}
顯示標記點
ArrayList<BitmapDescriptor> normalGiflist = new ArrayList<>();
normalGiflist.add(BitmapDescriptorFactory.fromResource(R.drawable.icon_normal1));
normalGiflist.add(BitmapDescriptorFactory.fromResource(R.drawable.icon_normal17));
normalGiflist.add(BitmapDescriptorFactory.fromResource(R.drawable.icon_normal38));
for (int i = 0; i < coords.length; i += 2) {
Marker marker = aMap.addMarker(new MarkerOptions().position(new LatLng(coords[i + 1], coords[i])).anchor(0.5f, 0.5f).icons(normalGiflist).period(15));
markerList.add(marker);
}
注意:這里要說一下關于點標記洗掉的問題,洗掉點要將點遍歷后通過remove移除,雖然map.clear()方法也可以使用,但是如果同時有很多中marker在顯示的話就不建議使用了
我就死在了這個上面糾結了好幾天,導致各種bug頻現,唉,說多了全是淚啊🤦?
點平滑移動
public void startMove() {
if (mPolyline == null) {
Toast.makeText(this, "請先設定路線", Toast.LENGTH_SHORT).show();
return;
}
List<LatLng> points = readLatLngs();
// 構建 軌跡的顯示區域
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(points.get(0));
builder.include(points.get(points.size() - 2));
aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 50));
// 實體 MovingPointOverlay 物件
if (smoothMarker == null) {
// 設定 平滑移動的 圖示
marker = aMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));
smoothMarker = new MovingPointOverlay(aMap, marker);
}
// 取軌跡點的第一個點 作為 平滑移動的啟動
LatLng drivePoint = points.get(0);
Pair<Integer, LatLng> pair = SpatialRelationUtil.calShortestDistancePoint(points, drivePoint);
points.set(pair.first, drivePoint);
List<LatLng> subList = points.subList(pair.first, points.size());
// 設定軌跡點
smoothMarker.setPoints(subList);
// 設定平滑移動的總時間 單位 秒
smoothMarker.setTotalDuration(40);
// 設定 自定義的InfoWindow 配接器
aMap.setInfoWindowAdapter(infoWindowAdapter);
// 顯示 infowindow
marker.showInfoWindow();
// 設定移動的監聽事件 回傳 距終點的距離 單位 米
smoothMarker.setMoveListener(new MovingPointOverlay.MoveListener() {
@Override
public void move(final double distance) {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (infoWindowLayout != null && title != null) {
title.setText("距離終點還有: " + (int) distance + "米");
}
}
});
} catch (Throwable e) {
e.printStackTrace();
}
}
});
// 開始移動
smoothMarker.startSmoothMove();
}
添加呼吸點
noLineMaker = aMap.addMarker(new MarkerOptions().position(new LatLng(39.97693511, 116.34892166))
.zIndex(1).anchor(0.5f, 0.5f).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_no_line1)));
//呼吸點中心點
aMap.addMarker(new MarkerOptions().position(new LatLng(39.97693511, 116.34892166)).zIndex(2)
.anchor(0.5f, 0.5f).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_no_line1)));
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(0.5f, 0f);
alphaAnimation.setDuration(2000);
alphaAnimation.setRepeatCount(Animation.INFINITE);
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 3.5f, 1, 3.5f);
scaleAnimation.setDuration(2000);
scaleAnimation.setRepeatCount(Animation.INFINITE);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.setInterpolator(new LinearInterpolator());
noLineMaker.setAnimation(animationSet);
noLineMaker.startAnimation();
到這里所有的內容就都結束了,
寫在結尾
這篇文章本來應該是在前天下午就應該出現的,但是由于不會弄那個gif圖,導致今天才完成了這篇文章,雖然文章寫晚了,但是還是有一個小功能添加的,所以也不虧的哦,按照本文章的話還是可以順利完成這些功能的,我也是找了很多的文章之后才實作這些功能的,中間還是踩了很多坑的,現在回想一下,好像也沒有什么,所以,加油,打工人!我們都是最棒的,
本篇文章相關demo請關注微信公眾號”只會寫bug的咸魚“ 后臺回復”軌跡回放“獲取
創作不易,不喜勿噴,歡迎指正!
最后的最后,我要說一句,周末愉快,打工人
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/199107.html
標籤:其他
