1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = findViewById(R.id.map);
//必须
mapView.onCreate(savedInstanceState);
AMap aMap = mapView.getMap();
List<LatLng> latLngList = getLatLngList();
//移动相机到中心点,第二个参数是相机缩放倍数
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLngList.get(0), 6));
//画点
for (LatLng latLng : latLngList) {
aMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))//图标,可以使用resId或者bitmap
//.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_background))
//.icon(BitmapDescriptorFactory.fromBitmap(null))
.position(latLng)//经纬坐标位置
.title("Title")//标题
.snippet("Snippet"))//子标题
;
}
//画线
aMap.addPolyline(new PolylineOptions()
.addAll(latLngList)//添加一个LatLng列表
.color(Color.RED)//线条的颜色
.width(2f)//线条的宽度
.setDottedLine(false)//点线?虚线?
);
//画圆
aMap.addCircle(new CircleOptions()
//.fillColor(Color.TRANSPARENT)//圆圈的颜色,内部颜色
.strokeColor(Color.RED)//圆圈的颜色
.strokeWidth(5f)//圆圈的宽度
.radius(100000d)//半径大小
.center(latLngList.get(0))//中心点坐标
);
//自定义View的Marker标记
LatLng unknownLatLng = new LatLng(25.026211, 124.189077);
TextView textView = new TextView(this);
textView.setTextColor(Color.BLACK);
textView.setText("不可知之地");
aMap.addMarker(new MarkerOptions().
position(unknownLatLng)
.icon(BitmapDescriptorFactory.fromView(textView))
);
//带自定义文本的Marker标记
LatLng customizeLatLng = new LatLng(26.026211, 128.189077);
Bitmap bitmap = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE).getBitmap();
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(25f);
paint.setTypeface(Typeface.DEFAULT_BOLD);
canvas.drawText("1", bitmap.getWidth() / 2, bitmap.getHeight() / 2, paint);
aMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.position(customizeLatLng));
//画长方形
aMap.addPolygon(new PolygonOptions()
.add(new LatLng(25.745506, 123.459086))
.add(new LatLng(23.745506, 123.459086))
.add(new LatLng(23.745506, 121.459086))
.add(new LatLng(25.745506, 121.459086))
.fillColor(Color.TRANSPARENT)
.strokeColor(Color.YELLOW)
.strokeWidth(2f));
//绘制热区
HeatmapTileProvider provider = new HeatmapTileProvider.Builder()
.data(latLngList)
.build();
aMap.addTileOverlay(new TileOverlayOptions()
.tileProvider(provider));
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
private List<LatLng> getLatLngList() {
List<LatLng> latLngList = new ArrayList<>();
/*
先北纬后东经
中心点:钓鱼岛:25.592319,123.463979
台湾省台北市:24.996343,121.519399
台湾省高雄市:22.563626,120.29991
厦门:24.447478,118.058705
福州:26.086712,119.289174
温州:27.917085,120.607534
赤尾屿:25.839774,125.089955
不可知之地:25.026211,124.189077
*/
latLngList.add(new LatLng(25.592319, 123.463979));
latLngList.add(new LatLng(24.996343, 121.519399));
latLngList.add(new LatLng(22.563626, 120.29991));
latLngList.add(new LatLng(24.447478, 118.058705));
latLngList.add(new LatLng(26.086712, 119.289174));
latLngList.add(new LatLng(27.917085, 120.607534));
latLngList.add(new LatLng(25.839774, 125.089955));
return latLngList;
}
|