0

I am trying to fetch latitude and longitude from mysql database using AsyncTask and show it on Map.I have created a class which gets the location from database here is the method which returns the location from database

 public Location addEmployee() {


    class AddEmployee extends AsyncTask<Void, Void, String> {

        //ProgressDialog loading;

       /* @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(MainActivity.this, "Retreiving...", "Wait...", false, false);
        }*/

        @Override
        protected void onPostExecute(String success) {
           // super.onPostExecute(success);
            //loading.dismiss();

            try {
                parseJSON(success);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }


        @Override
        protected String doInBackground(Void... v) {
            HashMap<String, String> params = new HashMap<>();
            params.put("busid", busid);

            RequestHandler rh = new RequestHandler();
            String res = rh.sendPostRequest("https://tkajbaje.000webhostapp.com/php/busid.php", params);
            return res;
        }
        public void parseJSON(final String success) throws JSONException {
             JSONObject jsonObject;

            jsonObject = new JSONObject(success);
            if (jsonObject.has("Latitude")) {
                location.setLatitude(Double.valueOf(jsonObject.getString("Latitude")));

            }
            if (jsonObject.has("Longitude")) {
                location.setLongitude(Double.valueOf(jsonObject.getString("Longitude")));



            }

        }
    }
    AddEmployee ae = new AddEmployee();
    ae.execute();
    return(location);

}

I have another activity which gets the location from the above function and shows it on map.here is the code

public void onMapReady(GoogleMap googleMap) {
    // Add a marker in Sydney, Australia,
    // and move the map's camera to the same location.
    Bundle extras = getIntent().getExtras();
    String busid=String.valueOf(1);
    Location location;
   MainActivity activity=new MainActivity(busid);
    location=activity.addEmployee();


   double Latitude=location.getLatitude();
    Toast.makeText(MapsMarkerActivity.this,String.valueOf(Latitude), Toast.LENGTH_LONG).show();



    double Longitude=location.getLongitude();

    LatLng sydney = new LatLng(Latitude, Longitude);
    googleMap.addMarker(new MarkerOptions().position(sydney)
            .title("Bus location"));
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

But the latitude and longitude always happen to be 0.0.

Tanmay Kajbaje
  • 61
  • 1
  • 3
  • 9

2 Answers2

1

Cuz, it starts the AsyncTask and return the location object(whatever the state of that object is). it will not wait for AsyncTask to complete the execution.

Solution:- You can create an interface in that class and execute in onPostExecute() implement the Interface in activity or use it Anonymously with Class object. Now, whenever it callbacks you can do rest of the work.

SRB Bans
  • 3,096
  • 1
  • 10
  • 21
1

I don't see where the location is defined, however it looks like you are mixing asynchronous and synchronous code.

AddEmployee ae = new AddEmployee();
ae.execute();
return(location);

In this part, you invoke an asynchronous task which will assign location a new value if the task is successful, however the keyword here is asynchronous, here location will return before Async Task is completed. You can create a Listener and call it in onPostExecute.

MainActivity activity=new MainActivity(busid);
location=activity.addEmployee();

Here, I strongly recommend making a new class and putting addEmployee there (and other logic that is not specific to the Activity). Creating a new activity with new keyword is not a good approach.

merterpam
  • 765
  • 6
  • 14