entre Desarrolladores

Recibe ayuda de expertos

Registrate y pregunta

Es gratis y fácil

Recibe respuestas

Respuestas, votos y comentarios

Vota y selecciona respuestas

Recibe puntos, vota y da la solución

Pregunta

1voto

public class Root_Map extends AppCompatActivity implements OnMapReadyCallback {
    ArrayList<LatLng> poos;

    /*public Root_Map(ArrayList poos) {
        this.poos = poos;
    }*/
    Huerta huerta;
    private boolean s = false;
    private GoogleMap mMap;
    private DatabaseReference database;
    private FirebaseDatabase db;
    FrameLayout fram_map;
    PolygonOptions rectOptions, rectOptio;
    ArrayList lon = new ArrayList();
    ArrayList lat = new ArrayList();
    ArrayList val = new ArrayList();
    private ArrayAdapter<String> adapterCat, adapterpol;
    Polygon polygon, pa;
    Button dib, elim, guar, btn_editarpol;
    int i = 0;
    HashMap<String, ArrayList> nombremap = new HashMap<String, ArrayList>();
    private Spinner hue;
    private AdapterMapsHuerta adapter;
    private RecyclerView recyclerpo;
    Boolean Is_MAP_Moveable = false;
    private Poligonoo poligo1;
    PopupWindow popupWindow;

    public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
    protected LocationManager locationManager;
    List<Poligonoo> poligonos = new ArrayList<>();
    List<PolygonOptions> pol = new ArrayList<>();
    List<Polygon> polos = new ArrayList<>();
    List<ArrayList> lala = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.root__map);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        // Bordes del polígo

        fram_map = findViewById(R.id.fram_map);
        hue = findViewById(R.id.sp);
        FloatingActionButton fab = findViewById(R.id.fab);
        dib = findViewById(R.id.btn_dibujar_map);
        elim = findViewById(R.id.btn_eliminar_map);
        guar = findViewById(R.id.btn_Guardar_map);
        elim.setVisibility(View.INVISIBLE);
        guar.setVisibility(View.INVISIBLE);
        hue.setVisibility(View.INVISIBLE);
        final List<String> huertas = new ArrayList<>();

        database = FirebaseDatabase.getInstance().getReference("Huertas");
        database.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                huertas.removeAll(huertas);
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    for (DataSnapshot ss : snapshot.getChildren()) {
                        Huerta huerta = ss.getValue(Huerta.class);
                        huertas.add(huerta.getNombrePredio());
                    }
                }
                adapterCat = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.select_dialog_item, huertas);
                hue.setAdapter(adapterCat);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
        db = FirebaseDatabase.getInstance();
        db.getReference("Poligono").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                poligonos.removeAll(poligonos);
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    //for (DataSnapshot ss : snapshot.getChildren()) {
                    //for (DataSnapshot ds : ss.getChildren()) {
                    ArrayList producto = snapshot.getValue(ArrayList.class);
                    lala.add(producto);
                    Toast.makeText(Root_Map.this, "producto: " + producto, Toast.LENGTH_SHORT).show();
                    // }
                    //  }
                    // Polygon poll=snapshot.getValue(Polygon.class);
                    //polos.add(poll);
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hue.setVisibility(View.VISIBLE);

                // Intent intent = new Intent(getBaseContext(), NuevaHuertaMapa.class);
                //startActivity(intent);
            }
        });
        guar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Guardarpol((String) hue.getSelectedItem());
                dib.setVisibility(View.INVISIBLE);
            }
        });

    }

    @Override
    public void onMapReady(final GoogleMap googleMap) {

        mMap = googleMap;
        checkLocationPermission();

        LocationServices.getFusedLocationProviderClient(getApplicationContext()).getLastLocation()
                .addOnSuccessListener(new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location location) {

                        if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION)
                                != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(),
                                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            return;
                        }
                        mMap.setMyLocationEnabled(true);
                        LatLng dgs = new LatLng(location.getLatitude(), location.getLongitude());

                        mMap.addMarker(new MarkerOptions().position(dgs).title("Tu ubicacion actual :)"));
                        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(dgs, 19));
                    }
                });
        dib.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dib.setVisibility(View.INVISIBLE);
                guar.setVisibility(View.VISIBLE);
                mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                    @Override
                    public void onMapClick(LatLng latLng) {
                        Toast.makeText(Root_Map.this, "Un solo clic", Toast.LENGTH_SHORT).show();
                        val.add(latLng);
                        lat.add(latLng.latitude);
                        lon.add(latLng.longitude);
                        Draw_Map();
                    }

                });
            }
        });
        mMap.setOnPolygonClickListener(new GoogleMap.OnPolygonClickListener() {
            @Override
            public void onPolygonClick(Polygon polygon) {

            }

        });
        //Toast.makeText(this, "este es el valor de poos: " +this.poligonos.get(0).getPoo(), Toast.LENGTH_SHORT).show();
        //addPolygon(poligonos);

    }

    public PolygonOptions addPolygon(ArrayList<Poligonoo> arg) {

        PolygonOptions polygonOptions;

        for (int i = 0; i <= arg.size(); i++) {
            Poligonoo a = arg.get(i);
            polygonOptions = new PolygonOptions();
            // polygonOptions.addAll(a.getPoo().getPoints()).strokeColor(Color.RED).strokeWidth(2);
            polygonOptions.fillColor(Color.parseColor("#51000000"));
            return polygonOptions;
        }
        return null;
    }

    public void Guardarpol(String nombre) {
        DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("Poligono");
        Poligonoo poloo;
        //    for (int i = 0; i < val.size(); i++) {
        poloo = new Poligonoo((ArrayList) polygon.getPoints());
        Toast.makeText(this, "" + polygon.getPoints(), Toast.LENGTH_SHORT).show();

        mDatabase.child(nombre).setValue(poloo).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {

                if (task.isSuccessful()) {
                    Toast.makeText(getApplicationContext(), "se guardo el poligono", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), task.getException().getMessage(), Toast.LENGTH_LONG).show();
                }

            }
        });
        //}

    }

    public void Draw_Map() {
        rectOptions = new PolygonOptions();
        rectOptions.addAll(val);
        rectOptions.strokeColor(Color.BLACK);
        rectOptions.strokeWidth(7);
        rectOptions.fillColor(0x8000FFFF);
        polygon = mMap.addPolygon(rectOptions);
    }

    public boolean checkLocationPermission() {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)) {

                new AlertDialog.Builder(this)
                        .setTitle("Titulo")
                        .setMessage("Botón")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                ActivityCompat.requestPermissions(Root_Map.this,
                                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                        MY_PERMISSIONS_REQUEST_LOCATION);
                            }
                        })
                        .create()
                        .show();

            } else {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSIONS_REQUEST_LOCATION);
            }
            return false;
        } else {
            return true;
        }
    }

    public boolean con() {
        return s;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_LOCATION: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    if (ContextCompat.checkSelfPermission(this,
                            Manifest.permission.ACCESS_FINE_LOCATION)
                            == PackageManager.PERMISSION_GRANTED) {

                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, (LocationListener) this);
                    }

                } else {
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }

        }
    }
}

Por favor, accede o regístrate para responder a esta pregunta.

Otras Preguntas y Respuestas


...

Bienvenido a entre Desarrolladores, donde puedes realizar preguntas y recibir respuestas de otros miembros de la comunidad.

Conecta