I have successfully created a google map. I can check mylocation, and I wrote code that can add a point in the google map (touch listener) and draw a line between the starting location and the point. and now I want to check the distance. I donβt know how I can googled this one example, but this code does not work correctly? I always have one distance this is my code, if anyone knows the solution, please help me thanks
public class GPS extends Activity implements OnMyLocationChangeListener, OnMapClickListener { final int RQS_GooglePlayServices = 1; private GoogleMap myMap; Circle myCircle; TextView tvLocInfo, GPSLocation; LatLng latLng; boolean markerClicked; ArrayList<LatLng> markerPoints; double Startlatitude, Startlongitude, Endlatitude, Endlongitude; public Button gpssize; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.gps); markerPoints = new ArrayList<LatLng>(); gpssize = (Button) findViewById(R.id.gpssize); tvLocInfo = (TextView) findViewById(R.id.GpsTxt); GPSLocation = (TextView) findViewById(R.id.GPSLocation); FragmentManager myFragmentManager = getFragmentManager(); MapFragment myMapFragment = (MapFragment) myFragmentManager .findFragmentById(R.id.GpsMap); myMap = myMapFragment.getMap(); myMap.getUiSettings().setZoomControlsEnabled(true); myMap.getUiSettings().setCompassEnabled(true); myMap.getUiSettings().setMyLocationButtonEnabled(true); myMap.setMyLocationEnabled(true); myMap.setOnMyLocationChangeListener(this); gpssize.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { double distance; Location locationA = new Location(""); locationA.setLatitude(Startlatitude); locationA.setLongitude(Startlongitude); Location locationB = new Location(""); locationB.setLatitude(Endlatitude); locationB.setLongitude(Endlongitude); distance = locationA.distanceTo(locationB) / 1000; Toast.makeText(getApplicationContext(), "" + distance, Toast.LENGTH_LONG).show(); } }); myMap.setOnMapClickListener(this); myMap.setTrafficEnabled(true); markerClicked = false; } @Override protected void onResume() { super.onResume(); int resultCode = GooglePlayServicesUtil .isGooglePlayServicesAvailable(getApplicationContext()); if (resultCode == ConnectionResult.SUCCESS) { Toast.makeText(getApplicationContext(), "isGooglePlayServicesAvailable SUCCESS", Toast.LENGTH_LONG) .show(); } else { GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices); } } @Override public void onMyLocationChange(Location location) { Startlatitude = location.getLatitude(); Startlongitude = location.getLongitude(); latLng = new LatLng(Startlatitude, Startlongitude); myMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); GPSLocation.setText(Startlatitude + " " + Startlongitude);
}
source share