private Integer getColorWithColor(float speed) {
speed = 80 - speed;
if (speed < 5) {
speed = 0;
}
if (speed > 80) {
speed = 80;
}
float one = (255 + 255) / 80F;
int r = 0;
int g = 0;
int b = 0;
if (speed < 40) {
r = (int) (one * speed);
g = 255;
}
if (speed >= 40) {
// 比例大于50的时候绿色是越来越少的,直到0 变为纯红
g = (int) (255 - ((speed - 40) * one));
r = 255;
}
return Color.argb(255, r, g, b);
}
private Integer getShareColorWithColor(float speed) {
int r;
int g;
int b;
if (speed > 80) {
speed = 80;
}
r = (int) (31 * speed / 80) + 38;
g = 211 - (int) (93 * speed / 80);
b = (int) (11 * speed / 80) + 238;
return Color.argb(255, r, g, b);
}
/**
* 绘制地图数据
*/
private void drawMapData() {
if (rideLineDataModelList == null || rideLineDataModelList.size() <= 0) {
return;
}
// 划线
LatLngBounds.Builder builder = LatLngBounds.builder();
int lineWidth = MyApplication.getApp().dp2px(3);
int shareLineWidth = ConvertUtils.dp2px(6);
PolylineOptions options = new PolylineOptions().width(lineWidth);
PolylineOptions shareOptions = new PolylineOptions().width(shareLineWidth);
List<Integer> integersColor = new ArrayList<>();
List<Integer> shareIntegersColor = new ArrayList<>();
for (int i = 0; i < rideLineDataModelList.size(); i++) {
if (i % 5000 == 0) {
options.useGradient(true);
options.colorValues(integersColor);
aMap.addPolyline(options);
shareOptions.useGradient(true);
shareOptions.colorValues(shareIntegersColor);
aMapSharethird.addPolyline(shareOptions);
aMapSharemb.addPolyline(options);
options = new PolylineOptions().width(lineWidth);
shareOptions = new PolylineOptions().width(shareLineWidth);
integersColor = new ArrayList<>();
shareIntegersColor = new ArrayList<>();
}
options.add(new LatLng(rideLineDataModelList.get(i).getLatitude(), rideLineDataModelList.get(i).getLongitude()));
shareOptions.add(new LatLng(rideLineDataModelList.get(i).getLatitude(), rideLineDataModelList.get(i).getLongitude()));
float currentSpeed = rideLineDataModelList.get(i).getSpeed() * 3.6f;
// 设置速度相对应得点的颜色
integersColor.add(getColorWithColor(currentSpeed));
shareIntegersColor.add(getShareColorWithColor(currentSpeed));
builder.include(new LatLng(rideLineDataModelList.get(i).getLatitude(), rideLineDataModelList.get(i).getLongitude()));
}
options.useGradient(true);
options.colorValues(integersColor);
Polyline aMapPolyline = aMap.addPolyline(options);
aMapPolyline.setZIndex(3);
shareOptions.useGradient(true);
shareOptions.colorValues(shareIntegersColor);
Polyline aMapSharethirdPolyline = aMapSharethird.addPolyline(shareOptions);
aMapSharethirdPolyline.setZIndex(3);
Polyline aMapSharembPolyline = aMapSharemb.addPolyline(options);
aMapSharembPolyline.setZIndex(3);
// 设置显示区域
LatLngBounds latLngBounds = builder.build();
int topbottom = MyApplication.getApp().dp2px(90);
int leftright = MyApplication.getApp().dp2px(70);
aMap.moveCamera(CameraUpdateFactory.newLatLngBoundsRect(latLngBounds, leftright, leftright, topbottom, topbottom));
int topbottom2 = MyApplication.getApp().dp2px(100);
int leftright2 = MyApplication.getApp().dp2px(30);
aMapSharethird.moveCamera(CameraUpdateFactory.newLatLngBoundsRect(latLngBounds, leftright2, leftright2, topbottom2, topbottom2));
int top3 = MyApplication.getApp().dp2px(65);
int bottom3 = MyApplication.getApp().dp2px(130);
int leftright3 = MyApplication.getApp().dp2px(50);
aMapSharemb.moveCamera(CameraUpdateFactory.newLatLngBoundsRect(latLngBounds, leftright3, leftright3, top3, bottom3));
// 当前点Marker
currentPointMarker = aMap.addMarker(new MarkerOptions()
.position(new LatLng(rideLineDataModelList.get(0).getLatitude(), rideLineDataModelList.get(0).getLongitude()))
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.move_icon)));
currentPointMarker.setClickable(false);
// 起点Marker
Marker startMarker = aMap.addMarker(new MarkerOptions()
.position(new LatLng(rideLineDataModelList.get(0).getLatitude(), rideLineDataModelList.get(0).getLongitude()))
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.marker_point_green)));
startMarker.setClickable(false);
Marker startMarker2 = aMapSharethird.addMarker(new MarkerOptions()
.position(new LatLng(rideLineDataModelList.get(0).getLatitude(), rideLineDataModelList.get(0).getLongitude()))
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.marker_point_green)));
startMarker2.setClickable(false);
Marker startMarker3 = aMapSharemb.addMarker(new MarkerOptions()
.position(new LatLng(rideLineDataModelList.get(0).getLatitude(), rideLineDataModelList.get(0).getLongitude()))
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.marker_point_green)));
startMarker3.setClickable(false);
// 终点Marker
Marker endMarker = aMap.addMarker(new MarkerOptions()
.position(new LatLng(rideLineDataModelList.get(rideLineDataModelList.size() - 1).getLatitude(), rideLineDataModelList.get(rideLineDataModelList.size() - 1).getLongitude()))
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.marker_point_red)));
endMarker.setClickable(false);
Marker endMarker2 = aMapSharethird.addMarker(new MarkerOptions()
.position(new LatLng(rideLineDataModelList.get(rideLineDataModelList.size() - 1).getLatitude(), rideLineDataModelList.get(rideLineDataModelList.size() - 1).getLongitude()))
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.marker_point_red)));
endMarker2.setClickable(false);
Marker endMarker3 = aMapSharemb.addMarker(new MarkerOptions()
.position(new LatLng(rideLineDataModelList.get(rideLineDataModelList.size() - 1).getLatitude(), rideLineDataModelList.get(rideLineDataModelList.size() - 1).getLongitude()))
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.marker_point_red)));
endMarker3.setClickable(false);
if (rideLineModel != null) {
// 显示最大速度Marker
if (rideLineModel.getMaxspeedlatitude() != 0 || rideLineModel.getMaxspeedlongitude() != 0) {
Typeface typeface = Typeface.createFromAsset(getAssets(), "FV_Almelo.ttf");
int padding = MyApplication.getApp().dp2px(7);
TextView maxspeedMarkerView = new TextView(getApplicationContext());
maxspeedMarkerView.setTypeface(typeface);
maxspeedMarkerView.setBackgroundResource(R.mipmap.marker_maxspeed);
maxspeedMarkerView.setPadding(padding, 0, padding, padding);
maxspeedMarkerView.setTextColor(getResources().getColor(R.color.M000000));
maxspeedMarkerView.setText(new StringBuilder().append(rideLineModel.getMaxspeed()).append("KM/H"));
TextView maxspeedMarkerView3 = new TextView(getApplicationContext());
maxspeedMarkerView3.setTypeface(typeface);
maxspeedMarkerView3.setBackgroundResource(R.mipmap.marker_maxspeed);
maxspeedMarkerView3.setPadding(padding, 0, padding, padding);
maxspeedMarkerView3.setTextColor(getResources().getColor(R.color.M000000));
maxspeedMarkerView3.setText(new StringBuilder().append(rideLineModel.getMaxspeed()).append("KM/H"));
Marker maxspeedMarker = aMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromView(maxspeedMarkerView))
.anchor(0.5f, 1)
.position(new LatLng(rideLineModel.getMaxspeedlatitude(), rideLineModel.getMaxspeedlongitude())));
maxspeedMarker.setClickable(false);
Marker maxspeedMarker3 = aMapSharemb.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromView(maxspeedMarkerView3))
.anchor(0.5f, 1)
.position(new LatLng(rideLineModel.getMaxspeedlatitude(), rideLineModel.getMaxspeedlongitude())));
maxspeedMarker3.setClickable(false);
}
// 错过的人
if (!TextUtils.isEmpty(rideLineModel.getNearuserstr())) {
try {
missUserModelList = JSON.parseArray(rideLineModel.getNearuserstr(), MissUserModel.class);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
网友评论