0

所以我現在已經用我自己的手持設備,今天我試圖讓谷歌地圖,將在當前用戶的位置在我的應用程序一直在開發這個應用程序的幾個星期Android的位置和谷歌設置

map.setMyLocationEnabled(true); //Locate the current position of the user 

上面的setLocationEnabled(true)通常應該顯示用戶在地圖上的位置,當按下GPS按鈕時,它將放大它。然而,在我添加下面的代碼後,這不再起作用,它不再顯示用戶的位置,我也注意到「Google設置」已安裝到我的設備上,在四處看後是Google的更新,因此它可能不會成爲代碼本身。但我不知道Google設置是否是這個原因。

LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    String provider = service.getBestProvider(criteria, false); 
    Location location = service.getLastKnownLocation(provider); 
    LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude()); 
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15)); 

即使在註釋掉的代碼塊的問題仍然存在。我不知道如何解決這個問題,我希望有人能幫助我。我不確定這是否與代碼本身或只是我的設備有關,即使它在過去幾周工作並且不會給出任何錯誤或警告,它實際上並不會獲得用戶的位置即使在我的地圖上按下GPS按鈕也不會給出任何錯誤或警告。

編輯:我試着用這個應用程序的代碼的老工作版本,它仍然無法獲得用戶位置來在地圖上。

謝謝。

回答

0

爲什麼你不使用:

map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(map.getMyLocation().getLatitude(),map.getMyLocation().getLongitude()), 15));

地圖商店的實際位置,所以你不需要LocationManager類。

+0

嗨我試過你的代碼,但我只是得到一個運行時異常:無法啓動活動。 – user1832478 2013-02-27 13:27:37

+0

你應該檢查你是否有位置:'if(mMap.getMyLocation()!= null)' – 2013-02-27 13:32:09

+0

直到地圖獲得位置,這將需要一點時間。 – 2013-02-27 13:35:55