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

时间:2017-10-17 10:12:21

标签: android google-maps android-mapview

我想显示带有设备位置的MapView,并且每隔10秒更新一次该位置。 我找到了很多解决方案而且没有任何工作,即使官方的Google Maps 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密钥,然后在app的build.gradle中进行编译:

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

我该怎么做?感谢

1 个答案:

答案 0 :(得分:0)

您不是在代码中请求用户位置,而是仅显示地图并移动相机。

您需要按照in the docs

所述请求

一些相关代码:

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

编辑:要使用最新版本的库,您必须以这种方式将Google Repository添加到您的paren't project build.gradle中:

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

帮助您不断更新SDK依赖项,尤其是“Google Repository”