Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.00 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.     MapView mMapView;
  3.     MapboxMap mMapboxMap;
  4.  
  5.     private SymbolManager symbolManager;
  6.     private List<Symbol> markers;
  7.     private CarGroup mCarData;
  8.  
  9.     Style mStyle;
  10.     private static final String TAG = "MainActivity";
  11.     final Float textOffset[] = { 0f, -2.5f };
  12.     private Handler handler = new Handler();
  13.  
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         Mapbox.getInstance(this, getString(R.string.token));
  18.         setContentView(R.layout.activity_main);
  19.  
  20.         mMapView = findViewById(R.id.mapView);
  21.         mMapView.onCreate(savedInstanceState);
  22.         mMapView.getMapAsync(mapboxMap -> mapboxMap.setStyle(Style.MAPBOX_STREETS, style -> {
  23.             this.mMapboxMap = mapboxMap;
  24.             this.mStyle = style;
  25.             style.addImage(
  26.                     "ic_marker",
  27.                     Objects.requireNonNull(BitmapUtils.getBitmapFromDrawable(getResources().getDrawable(R.drawable.ic_marker))),
  28.                     true
  29.             );
  30.  
  31.     /*        SymbolLayer symbolLayer = new SymbolLayer("layer-id", "source-id");
  32.             symbolLayer.setProperties(
  33.                     PropertyFactory.iconImage("ic_marker")
  34.             );*/
  35.  
  36.             //Objects.requireNonNull(mapboxMap.getStyle()).addLayer(symbolLayer);
  37.             symbolManager = new SymbolManager(mMapView, mMapboxMap, style, "1");
  38.             symbolManager.setIconRotationAlignment("map");
  39.             markers = new ArrayList<>();
  40.  
  41.             handler.postDelayed(markerUpdateRunnable, 1000);
  42.         }));
  43.     }
  44.  
  45.     private void refreshCarData(int id, String token) {
  46.         NetworkConnector.getInstance()
  47.                 .getGlonassApi()
  48.                 .getCarData(id, token)
  49.                 .enqueue(new Callback<CarGroup>() {
  50.                     @Override
  51.                     public void onResponse(@NonNull Call<CarGroup> call, @NonNull Response<CarGroup> response) {
  52.                         if (response.isSuccessful()) {
  53.                             refreshClusteredGeoJsonSource(response);
  54.                         }
  55.                     }
  56.                     @Override
  57.                     public void onFailure(@NonNull Call<CarGroup> call, @NonNull Throwable t) {
  58.                         refreshCarData(id, token);
  59.                     }
  60.                 });
  61.     }
  62.  
  63.     private void refreshClusteredGeoJsonSource(@NonNull Response<CarGroup> response) {
  64.         mCarData = response.body();
  65.  
  66.         if (markers.size() != mCarData.getSize()) {
  67.             symbolManager.delete(markers);
  68.         }
  69.         if (symbolManager.getAnnotations().size() == 0) {
  70.             createSymbols();
  71.         } else {
  72.             startAnimation();
  73.         }
  74.         showCluster();
  75.     }
  76.  
  77.     private void startAnimation() {
  78.         Symbol symbol;
  79.         if (markers.size() == mCarData.getSize()) {
  80.             for (int i = 0; i < mCarData.getSize(); i++) {
  81.                 symbol = markers.get(i);
  82.                 LatLng location = mCarData.getLatLng(i);
  83.                 float rotation = mCarData.getAngle(i);
  84.                 final LatLng originalPosition = symbol.getLatLng();
  85.                 final float originalRotation = symbol.getIconRotate();
  86.                 final boolean changeLocation = originalPosition.distanceTo(location) > 0;
  87.                 final boolean changeRotation = originalRotation != rotation;
  88.  
  89.                 if (symbolManager == null || symbolManager.getAnnotations().indexOfValue(symbol) < 0) {
  90.                     return;
  91.                 }
  92.  
  93.                 if (!changeLocation && !changeRotation) {
  94.                     continue;
  95.                 }
  96.                 if (changeLocation) {
  97.                     symbol.setLatLng(location);
  98.                 }
  99.                 if (changeRotation) {
  100.                     symbol.setIconRotate(rotation);
  101.                 }
  102.                 symbolManager.update(symbol);
  103.             }
  104.         }
  105.     }
  106.  
  107.  
  108.     private void createSymbols() {
  109.         for (int i = 0; i < mCarData.getSize(); i++) {
  110.             String color = ColorUtils.colorToRgbaString(ContextCompat.getColor(this, R.color.gray));
  111.             if (mCarData.isOnline(i)) {
  112.                 if (mCarData.getDrive(i) == 1) {
  113.                     color = ColorUtils.colorToRgbaString(ContextCompat.getColor(this, R.color.mapboxGreen));
  114.                 } else {
  115.                     color = ColorUtils.colorToRgbaString(ContextCompat.getColor(this, R.color.mapbox_blue));
  116.                 }
  117.             }
  118.  
  119.             createSymbol(mCarData.getLatLng(i), mCarData.getAngle(i), mCarData.getNumber(i), color, "ic_marker");
  120.         }
  121.     }
  122.  
  123.     private void createSymbol(LatLng latLng, float angle, String number, String color, String icon) {
  124.         SymbolOptions symbolOptions = new SymbolOptions()
  125.                 .withLatLng(latLng)
  126.                 .withIconImage(icon)
  127.                 .withIconSize(0.8f)
  128.                 .withTextField(number)
  129.                 .withTextSize(10.0f)
  130.                 .withTextOffset(textOffset)
  131.                 .withTextHaloColor(ColorUtils.colorToRgbaString(Color.WHITE))
  132.                 .withTextHaloWidth(1f)
  133.                 .withIconColor(color)
  134.                 .withIconRotate(angle)
  135.                 .setDraggable(false);
  136.         markers.add(symbolManager.create(symbolOptions));
  137.     }
  138.  
  139.  
  140.     private void showCluster() {
  141.         List<Feature> markerCoordinates = new ArrayList<>();
  142.         for(int i = 0; i < mCarData.getSize(); i++){
  143.             markerCoordinates.add(Feature.fromGeometry(
  144.                     Point.fromLngLat(mCarData.getLon(i), mCarData.getLat(i))));
  145.         }
  146.         GeoJsonSource mSource = (GeoJsonSource) mStyle.getSource("markers");
  147.         if (mSource == null) {
  148.             mStyle.addSource(
  149.                     new GeoJsonSource("markers",
  150.                             FeatureCollection.fromFeatures(markerCoordinates),
  151.                             new GeoJsonOptions()
  152.                                     .withCluster(true)
  153.                                     .withTolerance(0.5f)
  154.                                     .withClusterRadius(40)
  155.                                     .withClusterMaxZoom(18)
  156.                     )
  157.             );
  158.         } else {
  159.             mSource.setGeoJson(FeatureCollection.fromFeatures(markerCoordinates));
  160.         }
  161.  
  162.         int[] layers = new int[] {0, ContextCompat.getColor(this, R.color.mapbox_blue)} ;
  163.  
  164.         for (int i = 0; i < layers.length; i++) {
  165.             //Add clusters' circles
  166.             CircleLayer circles = (CircleLayer) mStyle.getLayer("cluster-" + i);
  167.             if (circles == null) {
  168.                 circles = new CircleLayer("cluster-" + i, "markers");
  169.                 circles.setProperties(
  170.                         circleColor(layers[1]),
  171.                         circleRadius(18f)
  172.                 );
  173.  
  174.                 Expression pointCount = toNumber(get("point_count"));
  175.  
  176.                 // Add a filter to the cluster layer that hides the circles based on "point_count"
  177.                 circles.setFilter(all(has("point_count"), gte(pointCount, literal(layers[0]))));
  178.                 mStyle.addLayer(circles);
  179.             }
  180.         }
  181.  
  182.         //Add the count labels
  183.         SymbolLayer count = (SymbolLayer) mStyle.getLayer("count");
  184.         if (count == null) {
  185.             count = new SymbolLayer("count", "markers");
  186.             count.setProperties(
  187.                     textField(Expression.toString(get("point_count"))),
  188.                     textSize(12f),
  189.                     textColor(Color.WHITE),
  190.                     textIgnorePlacement(true),
  191.                     textAllowOverlap(true)
  192.             );
  193.             mStyle.addLayer(count);
  194.         }
  195.     }
  196. private Runnable markerUpdateRunnable = new Runnable(){
  197.         public void run(){
  198.             if (NetworkManager.isNetWorkAvailable(MainActivity.this)) {
  199.                 refreshCarData(116, "af821e21-caaa-4e09-975b-8df9ea52ef9d");
  200.             } else Toast.makeText(getApplicationContext(), "Отсутствует подключение к интернету", Toast.LENGTH_SHORT).show();
  201.  
  202.             handler.postDelayed(this, 5000);
  203.         }
  204.     };
  205.  
  206.     @Override
  207.     public void onStart() {
  208.         super.onStart();
  209.         mMapView.onStart();
  210.     }
  211.  
  212.     @Override
  213.     public void onResume() {
  214.         super.onResume();
  215.         mMapView.onResume();
  216.     }
  217.  
  218.     @Override
  219.     public void onPause() {
  220.         super.onPause();
  221.         mMapView.onPause();
  222.     }
  223.  
  224.     @Override
  225.     public void onStop() {
  226.         super.onStop();
  227.         mMapView.onStop();
  228.     }
  229.  
  230.     @Override
  231.     public void onLowMemory() {
  232.         super.onLowMemory();
  233.         mMapView.onLowMemory();
  234.     }
  235.  
  236.     @Override
  237.     protected void onDestroy() {
  238.         super.onDestroy();
  239.         mMapView.onDestroy();
  240.     }
  241.  
  242.     @Override
  243.     protected void onSaveInstanceState(Bundle outState) {
  244.         super.onSaveInstanceState(outState);
  245.         mMapView.onSaveInstanceState(outState);
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement