0

I need to show a bus location on the map, I have latitude and longitude saved in my database. To retrieve the Latitude and Longitude i am using AsynTask. But i think there is some problem with my code, app crashes if call AsyncTask in onMapReady. I checked without calling AsyncTask giving manual latitude and longitude, it works fine. Please help me fix the issue.

This is my code:

 @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    GetLocation getLocation = new GetLocation(getApplicationContext());
    getLocation.execute();

}

public class GetLocation extends AsyncTask<String,Void,String> {
    Context context;
    GetLocation(Context ctx){
        context = ctx;
    }

    @Override
    protected String doInBackground(String... params) {

        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String s) {

        String login_url = "https://www.mywebsite.com/android/getlonglatt.php";

        try {
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            String result="";
            while ((line = bufferedReader.readLine())!=null){
                result += line;
            }
            JSONObject jsonObject = new JSONObject(result);
            String latitudedb = (String) jsonObject.get("latitude");
            String longitudedb = (String) jsonObject.get("longitude");
            String busnum = (String) jsonObject.get("busnum");

            Double newlatt = Double.valueOf(latitudedb);
            Double newlong = Double.valueOf(longitudedb);

            LatLng location = new LatLng(newlatt, newlong);

            MarkerOptions options = new MarkerOptions().position(location).title(busnum);
            mMap.addMarker(options);
            mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location,19.2f));

            bufferedReader.close();
            httpURLConnection.disconnect();
        }  catch (JSONException e) {
            e.printStackTrace();
        }  catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected void onProgressUpdate(Void... values) {

    }

}
  • You should get Double numbers for latitude and longitude – MMG Apr 27 '20 at 12:52
  • @MohammadMoeinGolchin Please check, I have 'em already. – Kiran Gogineni Apr 27 '20 at 12:55
  • Try moving the api call to doInBackground() –  Apr 27 '20 at 12:55
  • @LoWaiKiu Yeah, I did that as well, still nope – Kiran Gogineni Apr 27 '20 at 12:56
  • What is the error? – MMG Apr 27 '20 at 12:57
  • App just crashes – Kiran Gogineni Apr 27 '20 at 12:59
  • OK, see the logcat to know what is error – MMG Apr 27 '20 at 13:00
  • There is nothing in logcat, please have a look at console: android.os.StrictMode$LogStackTrace at android.os.StrictMode.readAndHandleBinderCallViolations(StrictMode.java:1727) at android.os.Parcel.readExceptionCode(Parcel.java:1540) at android.os.Parcel.readException(Parcel.java:1509) at com.google.android.gms.internal.maps.zza.zzb(Unknown Source) at com.google.android.gms.maps.internal.zzj.onPause(Unknown Source) at com.google.android.gms.maps.SupportMapFragment$zza.onPause(Unknown Source) – Kiran Gogineni Apr 27 '20 at 13:09
  • Select error in logcat – MMG Apr 27 '20 at 13:09
  • https://stackoverflow.com/questions/60630018/what-is-the-meaning-of-alphabets-in-android-studio-logcat/60630036#60630036 – MMG Apr 27 '20 at 13:10
  • 04-27 18:44:34.228 19425-19481/com.bus.trackbuddy E/Parcel: Reading a NULL string not supported here. 04-27 18:44:34.248 19425-19481/com.bus.trackbuddy E/Parcel: Reading a NULL string not supported here. 04-27 18:44:34.918 19425-19500/com.bus.trackbuddy E/Google Maps Android API: Authorization failure. Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map. 04-27 18:44:34.918 19425-19500/com.bus.trackbuddy E/Google Maps Android API: In the Google Developer Console (https://console.developers.google.com) – Kiran Gogineni Apr 27 '20 at 13:40
  • but, I enabled the API key – Kiran Gogineni Apr 27 '20 at 13:41

0 Answers0