PaddyAreaResult.java 3.03 KB
package com.example.eketha.PaddyArea;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.eketha.FertilizerGuidence;
import com.example.eketha.R;

public class PaddyAreaResult extends AppCompatActivity {

    Button btLocation,nxt1,navigateFertiGuidence;
    TextView area,d1,d2;
    private String distance1,lon4,lat4,lon3,lat3;
    private int distance;
    private double lol,loll,lol1,loll1;
    private String fertilizerType;

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

        distance1 = getIntent().getStringExtra("distance1");
        lat3 = getIntent().getStringExtra("lat3");
        lon3 = getIntent().getStringExtra("lon3");
        lat4 = getIntent().getStringExtra("lat4");
        lon4 = getIntent().getStringExtra("lon4");
        fertilizerType = getIntent().getStringExtra("fertilizerType");
        navigateFertiGuidence = findViewById(R.id.navigateFertiGuidence);

        area = findViewById(R.id.area);
//        d1 = findViewById(R.id.d1);
//        d2 = findViewById(R.id.d2);

//        d1.setText(distance1);

        int x = Integer.valueOf(distance1);

        lol = Double.valueOf(lat3);
        loll = Double.valueOf(lon3);
        lol1 = Double.valueOf(lat4);
        loll1 = Double.valueOf(lon4);

        distance = calculateDistanceInKilometer(lol,loll,lol1,loll1);
        //    distance = calculateDistanceInKilometer(6.713408110319169,79.91602709961393,6.713184955531217,79.91592511089544);

        int a = distance * x;
        int hect = a / 10000;

//        d2.setText(String.valueOf(distance));

        area.setText(String.valueOf(a) + "m");

        navigateFertiGuidence.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PaddyAreaResult.this, FertilizerGuidence.class);
                intent.putExtra("fertilizerType",fertilizerType);
                intent.putExtra("areaResult",String.valueOf(hect));
                startActivity(intent);
            }
        });
    }

    public final static double AVERAGE_RADIUS_OF_EARTH_KM = 6371;
    public int calculateDistanceInKilometer(double userLat, double userLng,
                                            double venueLat, double venueLng) {

        double latDistance = Math.toRadians(userLat - venueLat);
        double lngDistance = Math.toRadians(userLng - venueLng);

        double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
                + Math.cos(Math.toRadians(userLat)) * Math.cos(Math.toRadians(venueLat))
                * Math.sin(lngDistance / 2) * Math.sin(lngDistance / 2);

        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        double r = (AVERAGE_RADIUS_OF_EARTH_KM * c) * 1000;

        int x = (int) r * 1;

        return x;

    }
}