Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.72 KB | None | 0 0
  1. package com.example.tomasz.buklewrunner;
  2.  
  3. import android.graphics.Color;
  4. import android.location.Location;
  5. import android.location.LocationListener;
  6. import android.location.LocationManager;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.TextView;
  11.  
  12. import com.google.android.gms.maps.CameraUpdateFactory;
  13. import com.google.android.gms.maps.GoogleMap;
  14. import com.google.android.gms.maps.MapFragment;
  15. import com.google.android.gms.maps.OnMapReadyCallback;
  16. import com.google.android.gms.maps.model.CircleOptions;
  17. import com.google.android.gms.maps.model.LatLng;
  18. import com.google.android.gms.maps.model.Marker;
  19. import com.google.android.gms.maps.model.MarkerOptions;
  20. import com.google.android.gms.maps.model.Polyline;
  21. import com.google.android.gms.maps.model.PolylineOptions;
  22.  
  23.  
  24. import java.text.DecimalFormat;
  25. import java.util.ArrayList;
  26. import java.util.Random;
  27.  
  28. public class Start extends AppCompatActivity
  29.         implements OnMapReadyCallback, View.OnClickListener, GoogleMap.OnMapClickListener{
  30.  
  31.     GoogleMap googleMap;
  32.     final LatLng ETI = new LatLng(54.371732,18.612349);
  33.     LatLng Pozycja = new LatLng(0.0,0.0);
  34.  
  35.  
  36.     TextView distView;
  37.     private LocationManager lm;
  38.     private LocationListener listener;
  39.     private TextView tv;
  40.     private TextView timer;
  41.     PolylineOptions trasa = new PolylineOptions();
  42.     LatLng[] pozy = new LatLng[9999];
  43.     Marker tu;
  44.  
  45.     double sekundy =0;
  46.     int count =0;
  47.     int min = 0;
  48.     int hour = 0;
  49.     String czas;
  50.  
  51.  
  52.     @Override
  53.     protected void onCreate(Bundle savedInstanceState) {
  54.         super.onCreate(savedInstanceState);
  55.         setContentView(R.layout.activity_start);
  56.         lm = (LocationManager) getSystemService(LOCATION_SERVICE);
  57.         listener = new MyListener();
  58.         tv = (TextView)findViewById(R.id.locTV);
  59.         timer =(TextView)findViewById(R.id.time);
  60.         registerListener();
  61.         MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
  62.         mapFragment.getMapAsync(this);
  63.         findViewById(R.id.changeBtn).setOnClickListener(this);
  64.         //distView = findViewById(R.id.distanceView);
  65.  
  66.         Thread t = new Thread(){
  67.             @Override
  68.             public void run(){
  69.                 while (!isInterrupted()){
  70.                     try {
  71.                         Thread.sleep(1000); //1000ms = 1 sec
  72.                         runOnUiThread(new Runnable() {
  73.                             @Override
  74.                             public void run() {
  75.                                 DecimalFormat dt = new DecimalFormat("##");
  76.  
  77.                                 count++;
  78.                                 if(count==60){
  79.                                     min++;
  80.                                     count=0;
  81.                                     sekundy++;
  82.                                     if(min==60){
  83.                                         hour++;
  84.                                         min=0;
  85.                                     }
  86.                                 }
  87.                                 czas =" "+dt.format(hour)+":"+min+":"+count;
  88.                                 timer.setText(czas);
  89.                             }
  90.                         });
  91.  
  92.                     } catch (InterruptedException e) {
  93.                         e.printStackTrace();
  94.                     }
  95.                 }
  96.             }
  97.         };
  98.         t.start();
  99.     }
  100.  
  101.     Location prevLocation = null;
  102.     int i =0;
  103.  
  104.     public double odleglosc(double szm_obl, double dlm_obl, double sz_obl, double dl_obl) {
  105.         double szm_rad = szm_obl * Math.PI / 180;
  106.         double dlm_rad = dlm_obl * Math.PI / 180;
  107.         double sz_rad = sz_obl * Math.PI / 180;
  108.         double dl_rad = dl_obl * Math.PI / 180;
  109.  
  110.         double delta_sz = sz_rad - szm_rad;
  111.         double delta_dl = dl_rad - dlm_rad;
  112.  
  113.         double R = 6371;
  114.         double a = Math.sin(delta_sz / 2) * Math.sin(delta_sz / 2) +
  115.                 Math.cos(szm_rad) * Math.cos(sz_rad) *
  116.                         Math.sin(delta_dl / 2) * Math.sin(delta_dl / 2);
  117.  
  118.         double c = 2 * Math.asin(Math.sqrt(a));
  119.  
  120.         double wynik = R * c;
  121.         return wynik;
  122.     }
  123.  
  124.     /*public void czas(){
  125.            Thread t = new Thread(){
  126.             @Override
  127.             public void run(){
  128.              while (!isInterrupted()){
  129.                  try {
  130.                      Thread.sleep(1000); //1000ms = 1 sec
  131.                      runOnUiThread(new Runnable() {
  132.                          @Override
  133.                          public void run() {
  134.                              count++;
  135.                             if(count==60){
  136.                                 min++;
  137.                                 count=0;
  138.                                 if(min==60){
  139.                                     hour++;
  140.                                     min=0;
  141.                                 }
  142.                             }
  143.                             czas =" "+hour+":"+min+":"+count;
  144.                              timer.setText(czas);
  145.                          }
  146.                      });
  147.  
  148.                  } catch (InterruptedException e) {
  149.                      e.printStackTrace();
  150.                  }
  151.              }
  152.           }
  153.         };
  154.         t.start();
  155.     }*/
  156.  
  157.     public void przetwarzajLokalizacje(Location location) {
  158.         String info;
  159.         double dlug;
  160.         double szer;
  161.         DecimalFormat df = new DecimalFormat("##.####");
  162.         info = df.format(location.getLatitude())+ " " + df.format(location.getLongitude());
  163.         dlug = location.getLatitude();
  164.         szer = location.getLongitude();
  165.         if(prevLocation!=null){
  166.             //float bearing = prevLocation.bearingTo(location);
  167.             //float distance = prevLocation.distanceTo(location);
  168.             //info += " "+distance+" "+bearing;
  169.             double dystans = Double.parseDouble(df.format(odleglosc(prevLocation.getLatitude(),prevLocation.getLongitude(),location.getLatitude(),location.getLongitude())));
  170.             //info += " "+df.format(odleglosc(prevLocation.getLatitude(),prevLocation.getLongitude(),location.getLatitude(),location.getLongitude()));
  171.             info = ""+dystans;
  172.         }
  173.         // Pozycja = new LatLng(location.getLatitude(),location.getLongitude());
  174.         Pozycja = new LatLng(dlug,szer);
  175.         //tu = googleMap.clear();
  176.         pozy[i] = new LatLng(location.getLatitude(),location.getLongitude());
  177.         trasa.add(pozy[i]);
  178.         trasa.color(Color.BLUE);
  179.         track = googleMap.addPolyline(trasa);
  180.         if (tu != null) {
  181.             tu.remove();}
  182.         tu = googleMap.addMarker(new MarkerOptions().position(Pozycja).title("Moja Lokalizacja"));
  183.         googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Pozycja,15));
  184.         tv.setText(info);
  185.         prevLocation = location;
  186.         if(i==9998){
  187.             i=0;}
  188.         else{
  189.             i=i+1;}
  190.     }
  191.  
  192.     @Override
  193.     public void onRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults){
  194.         registerListener();
  195.     }
  196.     void registerListener() {
  197.         lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
  198.     }
  199.  
  200.     @Override
  201.     public void onMapReady(final GoogleMap googleMap) {
  202.         this.googleMap = googleMap;
  203.         googleMap.addMarker(new MarkerOptions().position(ETI).title("Baza"));
  204.         googleMap.addCircle(new CircleOptions().center(ETI).radius(100));
  205.         googleMap.getUiSettings().setZoomControlsEnabled(true);
  206.         googleMap.setOnMapClickListener(this);
  207.         googleMap.setIndoorEnabled(true);
  208.         googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ETI,15));
  209.         PolylineOptions obw = new PolylineOptions();
  210.         obw.add(ETI);
  211.         obw.add(ETI);
  212.         obw.color(Color.BLUE);
  213.         track = googleMap.addPolyline(obw);
  214.         googleMap.addMarker(new MarkerOptions().position(Pozycja).title("Moja Lokalizacja"));
  215.  
  216.     }
  217.  
  218.  
  219.     private Polyline track;
  220.     @Override
  221.     public void onClick(View view) {
  222.         if(track !=null) track.remove();
  223.         PolylineOptions po = new PolylineOptions();
  224.         Random rand = new Random();
  225.         po.add(ETI);
  226.         for (int i = 0; i < 7; i++){
  227.             double dlat = (rand.nextDouble() - 0.5) / 100;
  228.             double dlon = (rand.nextDouble() - 0.5) / 70;
  229.             po.add(new LatLng(ETI.latitude + dlat , ETI.longitude + dlon));
  230.         }
  231.         po.add(ETI);
  232.         po.color(Color.RED);
  233.         track = googleMap.addPolyline(po);
  234.  
  235.         for (Marker m: all) m.remove();
  236.         all.clear();
  237.         overalDist =0;
  238.         pointId = 1;
  239.     }
  240.  
  241.     int pointId = 1;
  242.     ArrayList<Marker> all = new ArrayList<>();
  243.     double overalDist=0;
  244.     @Override
  245.     public void onMapClick(LatLng latLng) {
  246.         /*Marker marker = googleMap.addMarker(new MarkerOptions().position(latLng)
  247.                 .title("Punkt " + pointId++));
  248.         all.add(marker);    //for future use
  249.         float[] results = new float[3];
  250.         Location.distanceBetween(marker.getPosition().latitude,marker.getPosition().longitude,ETI.latitude,ETI.longitude,results);
  251.         overalDist+=results[0];
  252.         distView.setText(""+overalDist);*/
  253.     }
  254.  
  255.     private class MyListener implements LocationListener {
  256.         @Override
  257.         public void onLocationChanged(Location location) {
  258.             przetwarzajLokalizacje(location);
  259.  
  260.  
  261.         }
  262.         @Override
  263.         public void onStatusChanged(String provider, int status, Bundle extras)
  264.         {
  265.         }
  266.         @Override
  267.         public void onProviderEnabled(String provider) {
  268.         }
  269.         @Override
  270.         public void onProviderDisabled(String provider) {
  271.         }
  272.     }
  273.  
  274.  
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement