如何在 MapView 上每 10 秒显示一次我的当前位置?

How can I display my current location every 10 seconds on a MapView?

我想显示一个带有设备位置的 MapView,并且位置每 10 秒更新一次。 我找到了很多解决方案,但没有一个有效,甚至官方 Google 地图 API 也无效。 我现在的代码是基本的 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));
    }

}

我有这个权限:

<!-- 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" />

当然,我在清单中有 Google API 密钥,并且在 build.gradle 中为应用程序编译:

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

我该怎么做?谢谢

您并没有在代码中请求用户位置,您只是在显示地图和移动相机。

您需要按照描述申请in the docs

一些相关代码:

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

编辑:要使用最新版本的库,您必须将 Google 存储库添加到您的父项目 build.gradle,这样:

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

不断更新您的 SDK 依赖项将对您有所帮助,尤其是 "Google Repository"