calculate distance between two location by enter latitude and longitude from EditText

Viewed 358

I'm trying to calculate distance between two locations on Google map. I'm inputting latitude and longitude from EditText, but the return value is zero meters. What goes wrong in the code, i.e., how to get the real distance?

Here is a picture of my app:

enter image description here

public class Ma`inActivity extends AppCompatActivity implements OnMapReadyCallback {
private TextView source;
private TextView destination;
private EditText sLatitude1;
private EditText sLongtiude1;
private EditText dLatitude2;
private EditText dLongtiude2;
private Button button;

private GoogleMap mMap;
boolean mapReady = false;
MarkerOptions elsedaway;
MarkerOptions Elrob3;
Location location;



 static final CameraPosition elfayoum = CameraPosition.builder()
        .target(new LatLng(29.309324, 30.842973))
        .zoom(1)
        .bearing(6)
        .tilt(45)
        .build();
double lati1;
double longi1;
double lati2;
double longi2;
TextView textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // find text that display distance
    textView = (TextView) findViewById(R.id.distance);
    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    // find edit text and text view
    source = (TextView) findViewById(R.id.sourc);
    destination = (TextView) findViewById(R.id.destination);
    sLatitude1 = (EditText) findViewById(R.id.lat1);
    sLongtiude1 = (EditText) findViewById(R.id.long1);
    dLatitude2 = (EditText) findViewById(R.id.lat2);
    dLongtiude2 = (EditText) findViewById(R.id.long2);
    // find button
    button = (Button) findViewById(R.id.getDistance);
    // find string from edittext
    String lat1 = sLatitude1.getText().toString();
    // parse string to double
    lati1 = ParseDouble(lat1);
    String lon1 = sLongtiude1.getText().toString();
    longi1 = ParseDouble(lon1);
    String lat2 = dLatitude2.getText().toString();
    lati2 = ParseDouble(lat2);
    String lon2 = dLongtiude2.getText().toString();
    longi2 = ParseDouble(lon2);
    Log.i("**lat", lat2);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            double xy1 = distanceBetween(new LatLng(lati1, longi1), new LatLng(lati1, longi2));
            String distanceis = fmt(xy1) + "meter";
            textView.setText(distanceis);
        }

    });


}

// mehtod to parse double from string

double ParseDouble(String strNumber) {
    if (strNumber != null && strNumber.length() > 0) {
        try {
            return Double.parseDouble(strNumber);
        } catch (Exception e) {
            return -1;   // or some value to mark this field is wrong. or make a function validates field first ...
        }
    } else return 0;
}

// get distance
public static Double distanceBetween(LatLng point1, LatLng point2) {
    if (point1 == null || point2 == null) {
        return null;
    }
    double vw = SphericalUtil.computeDistanceBetween(point1, point2);
    Log.i("distance isby utillib ", String.valueOf(vw));
    return vw;
}

public String fmt(double d) {

    return String.format("%s", d);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mapReady = true;
    mMap = googleMap;
    //    if(elsedaway!=null){
    //    mMap.addMarker(elsedaway);};
   //      mMap.addMarker(Elrob3);

 mMap.addPolyline(new PolylineOptions().geodesic(true)
                    .add(new LatLng(lati1, longi1))
                    .add(new LatLng(lati2, lati2))
            mMap.addCircle(new CircleOptions()
                    .center(new LatLng(29.291540, 30.601884))
                    .radius(500044)
                    .strokeColor(Color.GREEN)
                    .fillColor(Color.argb(54, 99, 255, 0)));
    flyTo(elfayoum);
}
3 Answers
Related