How to add Circle on the map using an ASyncTask?

0

I have an ASyncTask that receives a GoogleMap object, my goal is to get it to take certain data from the server and create a circle at the point defined in onPost () but it does not add the circle and does not give any error. p>

By using the values found in the debugging process and inserting them directly into Activity, it does it right.

My ASyncTask:

public class GeofenceTask extends AsyncTask <Void, Void, Geofence> {

private Context context;
private GoogleMap map;
private Usuario user;
private Geofence geofence;


public GeofenceTask(Context context, GoogleMap map, Usuario user) {

    this.context = context;
    this.map = map;
    this.user = user;

}


@Override
protected Geofence doInBackground(Void... voids) {

    HttpURLConnection http = null;

    try {

        URL url = new URL("http://"+ Servidor.SERVIDOR+"/api/geofences");
        http = (HttpURLConnection) url.openConnection();
        http.setConnectTimeout(10000);
        http.setReadTimeout(10000);
        http.setRequestMethod("GET");
        http.setRequestProperty("Accept","application/json");

        String cred = user.getUsername()+":"+user.getPassword();

        String basicAuth = new String(Base64.encode(cred.getBytes(), Base64.NO_WRAP));
        http.setRequestProperty("Authorization", basicAuth);


        BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream()));

        StringBuilder result = new StringBuilder();


        String s;
        while ((s = reader.readLine()) != null) {


            result.append(s);

        }

        JSONArray array = new JSONArray(result.toString());
        for (int i = 0; i < array.length(); i++) {

            JSONObject obj = array.getJSONObject(i);
            int id = obj.getInt("id");
            String name = obj.getString("name");
            String description = obj.getString("description");
            String area = obj.getString("area");

            geofence = new Geofence(id, name, area, description);

        }

    } catch (MalformedURLException |JSONException e) {

        e.printStackTrace();

    } catch (IOException ioe) {

        ioe.printStackTrace();

    }


    return geofence;
}

@Override
protected void onPostExecute(Geofence geofence) {
    super.onPostExecute(geofence);

    if (geofence != null) {

        String[] circulo = geofence.getArea().split(",");
        String []a  = circulo[0].split("\("); //Onde há abertura de parênteses

        String[] pos = a[1].split("\s"); //Onde há espaços em branco

        final double lat = Double.parseDouble(pos[0]);
        final double lng = Double.parseDouble(pos[1]);


        String[] espaco = circulo[1].split("\s");
        String[] parenteses = espaco[1].split("\)");

        final double radius = Double.parseDouble(parenteses[0]);

        geofence.setRadius(radius);

        Position position = new Position(lat, lng);
        geofence.setPosition(position);

        Log.i("POSITION", String.valueOf(lat+":"+lng)+"-radius:"+radius);


        Toast.makeText(context, geofence.toString(), Toast.LENGTH_LONG).show();

        Circle circle = map.addCircle(new CircleOptions()
                .center(new LatLng(lat, lng))
                .radius(radius)
                .fillColor(Color.BLUE)
                .strokeWidth(2)
                .strokeColor(Color.RED));




        Log.i("Geo", geofence.toString());

    }

}

}

    
asked by anonymous 05.10.2018 / 10:39

0 answers