1

I want to display a MapView with the device location and every 10 seconds the location is updated. I've found a lot of solutions and no ones works, even the official Google Maps API doesn't works. My code right now is the basic MapActivity:

package org.cuatrovientos.app.pamplonanegra;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

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.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        // 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);
    }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Pamplona, Spain.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Cuatrovientos and move the camera
        LatLng cuatrovientos = new LatLng(42.82452261, -1.6601049900);
        mMap.addMarker(new MarkerOptions().position(cuatrovientos).title("Marker in Cuatrovientos"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(cuatrovientos));
    }

}

I have this permissions:

<!-- Permisions -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.LOCATION" />
<uses-permission android:name="android.permission.PHONE" />
<uses-permission android:name="android.permission.INTERNET" />

Of course I have the Google API Key right in the manifest and this compile in the build.gradle for the app:

compile 'com.google.android.gms:play-services-maps:11.0.4'
compile 'com.google.android.gms:play-services-location:11.0.4'

How can I do that? Thanks

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    First of all, update your dependencies to 11.2.4 – webo80 Oct 17 '17 at 10:13
  • 2
    Possible duplicate of [How can I show current location on a Google Map on Android Marshmallow?](https://stackoverflow.com/questions/34582370/how-can-i-show-current-location-on-a-google-map-on-android-marshmallow) – Ronak Thakkar Oct 17 '17 at 10:14
  • https://stackoverflow.com/questions/17145241/android-location-update-every-minute + https://stackoverflow.com/questions/15905359/how-to-change-the-position-of-a-marker-on-a-android-map-v2 – Yuri Misyac Oct 17 '17 at 10:14

1 Answers1

1

You aren't requesting the user location in your code, you are only showing a map and moving the camera.

You need to request it as described in the docs

Some relevant code:

mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
    })
            .addOnFailureListener(new OnFailureListener() {
                (...)                       
                }
            });

EDIT: For using the last version of the libraries, you must add Google Repository to your paren't project build.gradle, in this way:

allprojects {
  repositories {
    jcenter()
    maven {
        url "https://maven.google.com"
    }
  }
}

It will be of help that you keep update your SDK dependencies, especially the "Google Repository"

webo80
  • 3,365
  • 5
  • 35
  • 52
  • I have a problem when i try to run the build.gradle, I've updated the dependencies to 11.2.4 as you said, the problem is the next one: Error(28, 13) Failed to resolve: com.google.android.gms:play-servicies... And there is a link to install the repositories and sync the project, but when I click Android Studio get freeze. – Javier Galera Oct 18 '17 at 08:04
  • @JavierGalera please Javi, try with my updated answer – webo80 Oct 18 '17 at 08:15
  • It works now! Thanks you very much! Sorry for the inconveniences, I'm just learning Android and programming, I didn't know how to do it! Thanks! – Javier Galera Oct 18 '17 at 08:51