Resulta que estoy desarrollando una aplicacion que utiliza un mapa. Para realizarlo lo genere desde android studio con la opcion new-> Google -> Google Maps Activity.
Esto genera una activity
con un fragment
que es donde se ecuentra el mapa. Yo quiero poner botones en esa activity
y que al precionarlos se ejecuten metodos definidos en MapsActivity.java
. Para poner los botones toque el xml
, pero el problema esta a la hora del metodo onClick
, ya que no aparecen las funciones definidas en la clase (tiene un metodo de prueba que muestra un mensaje nomas). Y si defino en el xml
el metodo que ejecutara cuando haga onclick
me detiene la aplicacion.
Layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="uap.geocolportaje.geocoportaje.MapsActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.375"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="uap.geocolportaje.geocoportaje.MapsActivity"
android:layout_below="@+id/btnGuardar" />
<Button
android:id="@+id/btnGuardar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnUbicacion"
android:elevation="3dp"
android:text="Guardar Ubicacion"
android:visibility="visible"
android:onClick="Guardar" />
<Button
android:id="@+id/btnUbicacion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:text="Mi Posicion"
android:visibility="visible" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
Activity:
package uap.geocolportaje.geocoportaje;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
/*TODO
*/
private GoogleMap mMap;
private Marker marcador;
double lat = 0.0;
double lng = 0.0;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.activity_maps, null);
public void Guardar(){
try {
Toast.makeText(this, "Guardar.", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "Error.", Toast.LENGTH_SHORT).show();
}
}
@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);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
miUbicacion();
}
public void agregarMarcador(double lat, double lng) {
LatLng coordenadas = new LatLng(lat, lng);
CameraUpdate miUbicacion = CameraUpdateFactory.newLatLngZoom(coordenadas, 16);
if (marcador != null)marcador.remove();
marcador = mMap.addMarker(new MarkerOptions()
.position(coordenadas)
.title("Ubicacion Actual")
//.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)
);
mMap.animateCamera(miUbicacion);
}
private void actualizarUbicacion(Location location) {
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
agregarMarcador(lat, lng);
}
}
private void miUbicacion() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
actualizarUbicacion(location);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER,15000,0,locListener);
}
LocationListener locListener= new LocationListener() {
@Override
public void onLocationChanged(Location location) {
actualizarUbicacion(location);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
private void GuardarPunto(){
Conexion con= new Conexion(this,"BD",null,1);
SQLiteDatabase db=con.getWritableDatabase();
//TODO
}
}