2017-10-17 293 views
-1

我想用設備位置顯示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' 

我該怎麼做?謝謝

+1

首先,更新您的依賴關係到11.2.4 – webo80

+2

可能的重複[如何顯示Android Marshmallow Google Map上的當前位置?](https://stackoverflow.com/questions/34582370/how-can -i-show-current-location-on-a-google-map-on-android-marshmallow) –

+0

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 –

回答

0

你沒有在你的代碼中請求用戶位置,你只是顯示地圖並移動攝像頭。

你需要請求其描述in the docs

一些相關代碼:

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

編輯:對於使用庫的最後一個版本,你必須添加谷歌儲存庫,以您的paren't項目構建.gradle,以這樣的方式

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

這將是您保持更新SDK依賴的幫助,特別是「谷歌庫」

+0

我有一個問題,當我嘗試運行build.gradle,我'正如你所說,問題是下一個: 錯誤(28,13)未能解決:com.google.android.gms:play-servicies ... 一個安裝存儲庫的鏈接d同步項目,但是當我點擊Android Studio時凍結。 –

+0

@JavierGalera請Javi,嘗試我的更新回答 – webo80

+0

它現在的作品!非常感謝你!對不便之處,我只是學習Android和編程,我不知道該怎麼做!謝謝! –

相關問題