I have a SQLite database where the marks are stored but I am not able to load them on the map. Thank you very much for your time.
Main activity
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
Button btn;
Context context = this;
private GoogleMap mMap;
SQLiteDatabase db=null;
Cursor cursor=null;
LatLng latLng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_maps );
btn = (Button) findViewById ( R.id.btnBuscar );
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager ()
.findFragmentById ( R.id.map );
mapFragment.getMapAsync ( this );
} // FIN METODO ONCREATE
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
CameraUpdate camUpd1 =
CameraUpdateFactory
.newLatLngZoom(new LatLng(40.41, -3.69), 5);
mMap.moveCamera(camUpd1);
mMap.setOnMapLongClickListener ( new GoogleMap.OnMapLongClickListener () {
@Override
public void onMapLongClick(final LatLng latLng) {
// ALERTDIALOG PARA ELEGIR ICONO
final AlertDialog.Builder builder= new AlertDialog.Builder ( MapsActivity.this );
LayoutInflater inflater =(LayoutInflater)getApplicationContext ().getSystemService ( Context.LAYOUT_INFLATER_SERVICE );
View row = inflater.inflate ( R.layout.row_item,null );
ListView listView = (ListView)row.findViewById ( R.id.lvIconosMapa );
listView.setAdapter ( new CustomAdapter ( getApplicationContext () ) );
builder.setView ( row );
// TITULO DEL ALERTDIALOG CUSTOMIZADO
final TextView title = new TextView(MapsActivity.this);
title.setText("Elija una opción");
title.setBackgroundColor(Color.BLUE);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.BLACK);
title.setTextSize(20);
builder.setCustomTitle(title);
builder.setNegativeButton ( "Cancelar", null );
final AlertDialog dialog = builder.create ();
dialog.show ();
// FIN ALERTDIALOG
listView.setOnItemClickListener ( new AdapterView.OnItemClickListener () {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position==0){
mMap.addMarker ( new MarkerOptions ()
.title ( "Titulo" )
.icon ( BitmapDescriptorFactory.fromResource (R.drawable.avion ) )
.anchor ( 0.0f,1.0f )
.snippet ( "Descripcion" )
.position ( latLng ));
Double latitud = latLng.latitude;
Double longitud = latLng.longitude;
Intent intent = new Intent ( getApplicationContext (), MainActivity.class );
intent.putExtra ( "LATITUD", latitud );
intent.putExtra ( "LONGITUD", longitud );
intent.putExtra ( "ICONO", R.drawable.avion );
intent.putExtra ( "FECHA", "HOY" );
startActivity ( intent);
}
else if (position==1){
mMap.addMarker ( new MarkerOptions ()
.icon ( BitmapDescriptorFactory.fromResource (R.drawable.coche ) )
.anchor ( 0.0f,1.0f )
.position ( latLng ));
dialog.cancel ();
}
}
} );
}
} );
mMap.setOnMarkerClickListener ( new GoogleMap.OnMarkerClickListener () {
@Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText ( getApplicationContext (), marker.getId (), Toast.LENGTH_SHORT ).show ();
return true;
}
} );
}
Database
public class BBDD_Helper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "marcas.db";
private final Context myContext;
private BBDD_Helper dbHelper;
private SQLiteDatabase db;
/**
* Definimos constantes con el nombre de las columnas de la tabla
*/
public static final String C_COLUMNA_ID = "_id";
public static final String C_COLUMNA_NOMBRE = "nombre";
public static final String C_COLUMNA_LAT = "lat";
public static final String C_COLUMNA_LON = "lon";
public static final String C_COLUMNA_DESCRIPCION = "descripcion";
public static final String C_COLUMNA_ICONO = "icono";
public static final String C_COLUMNA_FECHA = "fecha";
String slqCreate = "CREATE TABLE tablamarcas (_id INTEGER PRIMARY KEY AUTOINCREMENT," + " nombre TEXT, lat DOUBLE, lon DOUBLE, descripcion TEXT, icono INTEGER, fecha TEXT)";
String sqlUpdate = "ALTER TABLE tablamarcas ADD COLUMN fecha TEXT;";
private String[] columnas = new String[]{ C_COLUMNA_ID, C_COLUMNA_NOMBRE, C_COLUMNA_LAT, C_COLUMNA_LON, C_COLUMNA_DESCRIPCION, C_COLUMNA_ICONO, C_COLUMNA_FECHA} ;
public BBDD_Helper(Context context, String nombre, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
if (db.isReadOnly ()){
db = getWritableDatabase ();
}
db.execSQL(slqCreate);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion){
db.execSQL ( sqlUpdate );
}
}
public BBDD_Helper abrir() throws SQLException
{
dbHelper = new BBDD_Helper (myContext, DATABASE_NAME, null, DATABASE_VERSION);
db = dbHelper.getWritableDatabase();
return this;
}
public void cerrar()
{
dbHelper.close();
}
/**
* Devuelve cursor con todos las columnas de la tabla
*/
public Cursor getCursor() throws SQLException
{
Cursor c = db.query( true, BBDD.BBDD_Estructura.TABLE_NAME, columnas, null, null, null, null, null, null);
return c;
}