0

para empezar tengo un Splash Activity que en 2,5 segundos automáticamente carga el MainActivity donde tengo un fragment para visualizar el mapa. El mapa se muestra, pero no la ubicación que le tengo puesta en el código. Como puedo hacer que aparezca esa ubicación con esas coordenadas automaticamente? Os dejo parte del código.

Muchas gracias y perdón por la ignorancia.

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.

    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

    if(status == ConnectionResult.SUCCESS) {

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(MapsActivity.this);
    }else{
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, (Activity)getApplicationContext(),10);
        dialog.show();
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

    UiSettings uiSettings = mMap.getUiSettings();
    uiSettings.setZoomControlsEnabled(true);

    // Add a marker in Sydney and move the camera
    LatLng burgerbanc = new LatLng(43.304640, -2.016711);
    mMap.addMarker(new MarkerOptions().position(burgerbanc).title("BurgerBanc Sociedad Limitada"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(burgerbanc));
    float zoomLevel=15;

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(burgerbanc,zoomLevel));
}

}

//activity_main.xml

<ImageView
    android:id="@+id/imageView4"
    android:layout_width="408dp"
    android:layout_height="0dp"
    android:layout_marginBottom="342dp"
    android:layout_marginTop="9dp"
    app:layout_constraintBottom_toTopOf="@+id/btnPedido"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/logo" android:contentDescription="@string/todo" />

<TextView
    android:id="@+id/textView7"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_marginBottom="14dp"
    android:layout_marginStart="52dp"
    android:text="@string/email"
    android:textSize="20sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintStart_toStartOf="@+id/imageView4" />

<TextView
    android:id="@+id/textView8"
    android:layout_width="wrap_content"
    android:layout_height="31dp"
    android:text="@string/direccionn"
    android:textSize="20sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/map"
    app:layout_constraintStart_toStartOf="@+id/textView" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_marginBottom="14dp"
    android:text="@string/tfono"
    android:textSize="20sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/textView8"
    app:layout_constraintStart_toStartOf="@+id/textView7" />

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="0dp"
    android:layout_height="211dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="15dp"
    android:layout_marginStart="15dp"
    app:layout_constraintBottom_toTopOf="@+id/btnPedido"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:context="com.example.urtzi.cebancburger.MapsActivity" />


<Button
    android:id="@+id/btnPedido"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="2dp"
    android:layout_marginEnd="42dp"
    android:layout_marginStart="42dp"
    android:text="@string/haz_ya_tu_pedido"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView4" />

//AndroidMainifest.xml

<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity" />
    <activity android:name=".DatosActivity" />
    <activity android:name=".BurgerActivity" />
    <activity
        android:name=".SplashActivity"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps" />
</application>

4
  • no te da ninguna excepción? Simplemente no te muestra el marcador no? O el marcador te lo muestra pero no te centra el mapa en el marcador? Commented el 15 dic. 2017 a las 8:57
  • Has probado de dejar solo un mMap.moveCamera ?
    – Codelaby
    Commented el 15 dic. 2017 a las 9:36
  • No me muestra ni el marcador, ni el zoom, ni se centra en las coordenadas que le he puesto.
    – Urtx
    Commented el 15 dic. 2017 a las 10:13
  • Simplemente muestra el mapa.
    – Urtx
    Commented el 15 dic. 2017 a las 10:13

1 respuesta 1

-1

Falta agregar el método CameraPosition en onMapReady:

mMap.setMyLocationEnabled(true);
CameraPosition cameraPosition = new CameraPosition.Builder().target(burgerbanc).zoom(15).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

Puedes usar animateCamera o moveCamera.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.