He seguido un motón de vídeos y preguntas de este foro para implementarlo, pero por algún motivo que desconozco no me ubica en el mapa, ni tan siquiera aparece el MyLocationButton
.
Mapa
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
Context context = this;
private GoogleMap mMap;
SQLiteDatabase db = null;
Double latitud = 0.0;
Double longitud;
String titulo, descripcion, fecha;
Float dimension1 = 0.5f;
Float dimension2 = 0.5f;
Float opacidad = 1f;
int icono;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_maps );
// 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(final GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings ().setZoomControlsEnabled ( true );
mMap.getUiSettings ().setMyLocationButtonEnabled ( true );
final BBDD_Helper bbdd_helper = new BBDD_Helper ( context, BBDD_Helper.DATABASE_NAME, null, BBDD_Helper.DATABASE_VERSION );
cargarPuntos ();
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 );
final 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("Elige una opción");
title.setBackgroundColor(Color.BLUE);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
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.marihuana ) )
.anchor ( 0.5f,0.5f )
.snippet ( "Descripcion" )
.position ( latLng ));
Double latitud = latLng.latitude;
Double longitud = latLng.longitude;
fecha = String.valueOf ( Math.random () );
Intent intent = new Intent ( getApplicationContext (), DatosMarcas.class );
intent.putExtra ( "LATITUD", latitud );
intent.putExtra ( "LONGITUD", longitud );
intent.putExtra ( "ICONO", R.drawable.marihuana );
intent.putExtra ( "FECHA", fecha);
startActivity ( intent);
}
else if (position==1){
mMap.addMarker ( new MarkerOptions ()
.icon ( BitmapDescriptorFactory.fromResource (R.drawable.interes ) )
.title ( "Titulo" )
.anchor ( 0.0f,1.0f )
.snippet ( "Descripcion" )
.position ( latLng ));
Double latitud = latLng.latitude;
Double longitud = latLng.longitude;
fecha = String.valueOf ( Math.random () );
Intent intent = new Intent ( getApplicationContext (), DatosMarcas.class );
intent.putExtra ( "LATITUD", latitud );
intent.putExtra ( "LONGITUD", longitud );
intent.putExtra ( "ICONO", R.drawable.interes );
intent.putExtra ( "FECHA", fecha);
startActivity ( intent);
dialog.cancel ();
}
else if (position==2){
mMap.addMarker ( new MarkerOptions ()
.icon ( BitmapDescriptorFactory.fromResource (R.drawable.viogen_autor ) )
.title ( "Titulo" )
.anchor ( 0.0f,1.0f )
.snippet ( "Descripcion" )
.position ( latLng ));
Double latitud = latLng.latitude;
Double longitud = latLng.longitude;
fecha = String.valueOf ( Math.random () );
Intent intent = new Intent ( getApplicationContext (), DatosMarcas.class );
intent.putExtra ( "LATITUD", latitud );
intent.putExtra ( "LONGITUD", longitud );
intent.putExtra ( "ICONO", R.drawable.viogen_autor );
intent.putExtra ( "FECHA", fecha);
startActivity ( intent);
dialog.cancel ();
}
else if (position==3){
mMap.addMarker ( new MarkerOptions ()
.icon ( BitmapDescriptorFactory.fromResource (R.drawable.viogen ) )
.title ( "Titulo" )
.anchor ( 0.0f,1.0f )
.snippet ( "Descripcion" )
.position ( latLng ));
Double latitud = latLng.latitude;
Double longitud = latLng.longitude;
fecha = String.valueOf ( Math.random () );
Intent intent = new Intent ( getApplicationContext (), DatosMarcas.class );
intent.putExtra ( "LATITUD", latitud );
intent.putExtra ( "LONGITUD", longitud );
intent.putExtra ( "ICONO", R.drawable.viogen );
intent.putExtra ( "FECHA", fecha);
startActivity ( intent);
dialog.cancel ();
}
}
} );
}
} );
mMap.setOnMarkerClickListener ( new GoogleMap.OnMarkerClickListener () {
@Override
public boolean onMarkerClick(final Marker marker) {
final AlertDialog.Builder builder= new AlertDialog.Builder ( MapsActivity.this );
LayoutInflater inflater =(LayoutInflater)getApplicationContext ().getSystemService ( Context.LAYOUT_INFLATER_SERVICE );
// TITULO DEL ALERTDIALOG CUSTOMIZADO
final TextView title = new TextView(MapsActivity.this);
title.setText(marker.getTitle ());
title.setBackgroundColor(Color.BLUE);
title.setPadding(10, 10, 10, 10);
// title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
latitud = marker.getPosition ().latitude;
builder.setMessage ( marker.getSnippet () );
builder.setCustomTitle(title);
builder.setPositiveButton ( "Eliminar marca", new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog, int which) {
SQLiteDatabase db = bbdd_helper.getWritableDatabase ();
String marcaBorrar = marker.getTitle ();
String [] argumento = {marcaBorrar};
String seleccion = BBDD_Helper.C_COLUMNA_NOMBRE + " LIKE ?";
db.delete ( BBDD.BBDD_Estructura.TABLE_NAME, seleccion, argumento );
Toast.makeText ( MapsActivity.this,"La marca ha sido borrada correctamente",Toast.LENGTH_LONG ).show ();
Cursor c = db.query ( BBDD.BBDD_Estructura.TABLE_NAME,
null,null,null,null,null,null);
int tamaño = c.getCount ();
googleMap.clear ();
cargarPuntos ();
}
} );
builder.setNegativeButton ( "Cerrar", null );
final AlertDialog dialog = builder.create ();
dialog.show ();
// FIN ALERTDIALOG
// marker.showInfoWindow ();
return true;
}
} );
}
private void setMarker(LatLng posicion, String titulo, String descripcion, float opacidad, float dimesion1, float dimension2, int icono){
mMap.addMarker ( new MarkerOptions ()
.position ( posicion )
.title ( titulo )
.snippet ( descripcion )
.alpha ( opacidad )
.anchor ( dimesion1,dimension2 )
.icon ( BitmapDescriptorFactory.fromResource ( icono )));
}
private void cargarPuntos (){
BBDD_Helper bbdd_helper = new BBDD_Helper ( context, BBDD_Helper.DATABASE_NAME, null, BBDD_Helper.DATABASE_VERSION);
db = bbdd_helper.getReadableDatabase ();
Cursor c = db.query(
BBDD.BBDD_Estructura.TABLE_NAME, // Nombre de la tabla
null, // Lista de Columnas a consultar
null, // Columnas para la cláusula WHERE
null, // Valores a comparar con las columnas del WHERE
null, // Agrupar con GROUP BY
null, // Condición HAVING para GROUP BY
null // Cláusula ORDER BY
);
if (c.moveToFirst()) {
//Recorremos el cursor hasta que no haya más registros
do {
latitud= c.getDouble (2);
longitud = c.getDouble(3);
titulo = c.getString ( 1 );
descripcion = c.getString ( 4 );
icono = c.getInt ( 5 );
setMarker ( new LatLng (latitud,longitud ), titulo,descripcion, opacidad,dimension1,dimension2, icono );
} while(c.moveToNext());
}
db.close ();
if (latitud!=0.0){
CameraUpdate camUpd1 =
CameraUpdateFactory
.newLatLngZoom(new LatLng(latitud,longitud), 15);
mMap.moveCamera(camUpd1);}}
}
Permisos Manifest
android.permission.READ_EXTERNAL_STORAGE
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.INTERNET
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_WIFI_STATE
com.google.android.providers.gsf.permission.READ_GSERVICES
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION