2014-04-16 130 views
11

我想製作一個程序來計算一些位置到我當前位置的距離,但我的googleMap.getMyLocation();沒有正常工作。googleMap.getMyLocation();無法獲取當前位置

googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
googleMap.setMyLocationEnabled(true); 
googleMap.getUiSettings().setCompassEnabled(false); 
mylocation = googleMap.getMyLocation(); 
direction = new GMapV2Direction(); 

LatLng from, to; 
from = new LatLng(-7.26071409, 112.80674726); 
for(int i=0; i<lat.length; i++){  
    to = new LatLng(lat[i], lon[i]); 
    doc = direksi.getDocument(from, to, GMapV2Direction.MODE_DRIVING); 
    distance[i] = (double)direction.getDistanceValue(doc)/1000; 
} 

我保存了lat []和lon []中某些地方的經度和緯度。 LatLng'from'是mylocation,'to'是我的目的地。 當我改變

from = new LatLng(-7.26071409, 112.80674726); 
to 
from = new LatLng(mylocation.getLatitude(), mylocation.getLongitude()); 

我想要做的計算,而無需打開谷歌地圖的問題出現。當我觸摸地圖按鈕作爲彈出窗口時,谷歌地圖將出現。因此計算會發生,而無需打開Goog​​leMap的

請幫我

+0

getMyLocation(),[此方法已棄用](https://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html)。改用LocationClient。 – AlexDG

+0

你在使用google map v2嗎? –

+0

@Alex我不知道如何使用LocationClient。你能詳細說明嗎? – user3506223

回答

21

嘗試......我希望這將是對您有所幫助

LocationManager locationManager = (LocationManager) 
     getSystemService(Context.LOCATION_SERVICE); 
Criteria criteria = new Criteria(); 

Location location = locationManager.getLastKnownLocation(locationManager 
     .getBestProvider(criteria, false)); 
double latitude = location.getLatitude(); 
double longitude = location.getLongitude(); 
+0

是的工作... tq ... – user3506223

6

這是我對你的代碼,希望這有助於你...編輯它,如果你認爲,它不是正確的sintaxis ...

這工作對於Android 4.x和5.x和6.x,我不測試這在android 7

//I use this like global var to edit or get informacion about google map 
GoogleMap gMap; 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    gMap = googleMap; 

if (ActivityCompat.checkSelfPermission(Activity_Map.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(Activity_Map.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
         ActivityCompat.requestPermissions(Activity_Map.this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1); 
        }else{ 
         if(!gMap.isMyLocationEnabled()) 
          gMap.setMyLocationEnabled(true); 

         LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
         Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

         if (myLocation == null) { 
          Criteria criteria = new Criteria(); 
          criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
          String provider = lm.getBestProvider(criteria, true); 
          myLocation = lm.getLastKnownLocation(provider); 
         } 

         if(myLocation!=null){ 
          LatLng userLocation = new LatLng(myLocation.getLatitude(), myLocation.getLongitude()); 
          gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 14), 1500, null); 
         } 
        } 

} 
+0

謝謝you.this爲我工作,這是一個很好的做法。 –