我想用設備位置顯示MapView,並且每10秒鐘更新一次位置。 我發現了很多解決方案,沒有人可以使用,即使官方的Google Maps API也無法正常工作。 我的代碼,現在是基本MapActivity:如何在MapView上每10秒顯示一次當前位置?
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" />
當然我也有谷歌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'
我該怎麼做?謝謝
首先,更新您的依賴關係到11.2.4 – webo80
可能的重複[如何顯示Android Marshmallow Google Map上的當前位置?](https://stackoverflow.com/questions/34582370/how-can -i-show-current-location-on-a-google-map-on-android-marshmallow) –
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 –