一、添加依赖和权限
在项目的build.gradle文件中添加高德地图的依赖:
bash
implementation 'com.amap.api:maps:latest_version'
在AndroidManifest.xml文件中添加必要的权限:
bash
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
二、布局文件
在布局文件中添加一个com.amap.api.maps.MapView用于显示地图:
bash
<com.amap.api.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
三、Activity 代码
bash
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import java.util.ArrayList;
import java.util.List;
public class MapActivity extends AppCompatActivity {
private MapView mapView;
private AMap aMap;
private List<LatLng> latLngList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
if (aMap == null) {
aMap = mapView.getMap();
}
latLngList = new ArrayList<>();
// 假设的经纬度数据,实际中可以从其他地方获取
latLngList.add(new LatLng(39.9087, 116.3975));
latLngList.add(new LatLng(39.9088, 116.3976));
latLngList.add(new LatLng(39.9089, 116.3977));
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
drawPolylineAndSetZoom();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
private void drawPolylineAndSetZoom() {
Polyline polyline = aMap.addPolyline(new PolylineOptions()
.addAll(latLngList)
.width(5)
.color(ContextCompat.getColor(this, R.color.polyline_color)));
LatLngBounds bounds = getBoundsFromLatLngList(latLngList);
if (bounds!= null) {
aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
}
}
private LatLngBounds getBoundsFromLatLngList(List<LatLng> latLngList) {
if (latLngList == null || latLngList.isEmpty()) {
return null;
}
double minLat = Double.MAX_VALUE;
double minLng = Double.MAX_VALUE;
double maxLat = Double.MIN_VALUE;
double maxLng = Double.MIN_VALUE;
for (LatLng latLng : latLngList) {
minLat = Math.min(minLat, latLng.latitude);
minLng = Math.min(minLng, latLng.longitude);
maxLat = Math.max(maxLat, latLng.latitude);
maxLng = Math.max(maxLng, latLng.longitude);
}
return new LatLngBounds.Builder()
.include(new LatLng(minLat, minLng))
.include(new LatLng(maxLat, maxLng))
.build();
}
}
在上述代码中,首先添加了高德地图依赖和必要的权限。在布局文件中添加了MapView,在Activity中初始化地图并添加了一些假设的经纬度数据用于绘制轨迹。drawPolylineAndSetZoom方法用于绘制轨迹并根据轨迹设置地图的缩放倍数,同时定位到轨迹路线。