【问题标题】:How can I display my current location every 10 seconds on a MapView?如何在 MapView 上每 10 秒显示一次当前位置?
【发布时间】:2017-10-17 10:12:21
【问题描述】:

我想显示一个带有设备位置的 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 密钥,并在应用程序的 build.gradle 中编译:

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

我该怎么做?谢谢

【问题讨论】:

标签: android google-maps android-mapview


【解决方案1】:

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

您需要按照in the docs的说明提出请求

一些相关代码:

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

编辑:要使用最新版本的库,您必须将 Google Repository 添加到您的父项目 build.gradle,以这种方式:

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

不断更新您的 SDK 依赖项会有所帮助,尤其是“Google 存储库”

【讨论】:

  • 我在尝试运行 build.gradle 时遇到问题,如您所说,我已将依赖项更新到 11.2.4,问题是下一个:Error(28, 13) Failed解决:com.google.android.gms:play-servicies... 并且有一个安装存储库和同步项目的链接,但是当我单击 Android Studio 时会冻结。
  • @JavierGalera 请 Javi,试试我更新的答案
  • 现在可以使用了!非常感谢你!给您带来的不便,我很抱歉,我只是学习Android和编程,我不知道该怎么做!谢谢!
猜你喜欢
  • 2019-09-28
  • 2015-10-01
  • 1970-01-01
  • 2011-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多