I have 2 "Start" and "Stop" buttons, google maps api v2 map.
I would like to make it so that when the button is pressed, the start point is assigned, and the stop point at Stop . And considered between them the distance, speed.
tell me how to start and end?
I tried to do, but the application takes off force closed when I press the start button.
here is my code:
package com.example.tuantv.slidingtabsdemo2; import android.app.Dialog; import android.content.Context; import android.graphics.Color; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.AsyncTask; import android.os.Bundle; import android.os.SystemClock; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.Chronometer; import android.widget.TextView; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.UiSettings; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.android.gms.maps.model.CameraPosition; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.model.Polyline; import com.google.android.gms.maps.model.PolylineOptions; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; /** * Created by Admin on 6/26/2015. */ public class Tab1 extends Fragment implements LocationListener, View.OnClickListener{ private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters private static final long MINIMUM_TIME_BETWEEN_UPDATES = 30000; GoogleMap googleMap; SupportMapFragment mapFragment; private Button buttonStart; private Button buttonStop; private Button buttonReset; private Chronometer chronometerRun; private double latStart; private double lonStart; private double latStop; private double lonStop; private LatLng latLngStart; private LatLng latLngStop; private TextView textViewDistance; Location location; long time = 0; @Nullable public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tab_1, container, false); textViewDistance = (TextView)view.findViewById(R.id.textViewDistance); buttonStart = (Button) view.findViewById(R.id.buttonStart); buttonStop = (Button) view.findViewById(R.id.buttonStop); buttonReset = (Button) view.findViewById(R.id.buttonReset); chronometerRun = (Chronometer) view.findViewById(R.id.chronometerRunning); buttonStart.setOnClickListener(this); buttonStop.setOnClickListener(this); buttonReset.setOnClickListener(this); // double distance = CalculationByDistance(latLngStart,latLngStop); initMap(); return view; } public void initMap(){ if (googleMap == null) { mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); } googleMap = mapFragment.getMap(); googleMap.setMyLocationEnabled(true); // Getting LocationManager object from System Service LOCATION_SERVICE LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); // Creating a criteria object to retrieve provider Criteria criteria = new Criteria(); // Getting the name of the best provider String provider = locationManager.getBestProvider(criteria, true); // Getting Current Location Location location = locationManager.getLastKnownLocation(provider); if(location!=null){ onLocationChanged(location); } locationManager.requestLocationUpdates(provider, 20000, 0, this); googleMap.getUiSettings().setMyLocationButtonEnabled(true); } public double CalculationByDistance(LatLng StartP, LatLng EndP) { int Radius = 6371;// radius of earth in Km double lat1 = StartP.latitude; double lat2 = EndP.latitude; double lon1 = StartP.longitude; double lon2 = EndP.longitude; double dLat = Math.toRadians(lat2 - lat1); double dLon = Math.toRadians(lon2 - lon1); double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2); double c = 2 * Math.asin(Math.sqrt(a)); double valueResult = Radius * c; double km = valueResult / 1; DecimalFormat newFormat = new DecimalFormat("####"); int kmInDec = Integer.valueOf(newFormat.format(km)); double meter = valueResult % 1000; int meterInDec = Integer.valueOf(newFormat.format(meter)); Log.i("Radius Value", "" + valueResult + " KM " + kmInDec + " Meter " + meterInDec); return Radius * c; } @Override public void onClick(View v) { switch(v.getId()) { case R.id.buttonStart: chronometerRun.setBase(SystemClock.elapsedRealtime() - time); chronometerRun.start(); latStart = location.getLatitude(); // Getting longitude of the current location lonStart = location.getLongitude(); // Creating a LatLng object for the current location latLngStart = new LatLng(latStart, lonStart); break; case R.id.buttonStop: time = SystemClock.elapsedRealtime() - chronometerRun.getBase(); chronometerRun.stop(); break; case R.id.buttonReset: time = 0; chronometerRun.setBase(SystemClock.elapsedRealtime()); chronometerRun.stop(); latStop = location.getLatitude(); // Getting longitude of the current location lonStop = location.getLongitude(); // Creating a LatLng object for the current location latLngStop = new LatLng(latStop, lonStop); break; } double distance = CalculationByDistance(latLngStart,latLngStop); } @Override public void onLocationChanged(Location location) { // Getting latitude of the current location double latitude = location.getLatitude(); // Getting longitude of the current location double longitude = location.getLongitude(); // Creating a LatLng object for the current location LatLng latLng = new LatLng(latitude, longitude); // Showing the current location in Google Map googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); // Zoom in the Google Map googleMap.animateCamera(CameraUpdateFactory.zoomTo(13)); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }