2012-08-30 43 views
1

在我的應用程序中,我使用地圖視圖可以找到我的GPS位置。Android:谷歌地圖,我的gps位置不顯示,如果GPS啓動後啓動應用程序

所有的工作終於如果我打開GPS並等待GPS修復,啓動應用程序之前。 如果我在啓動應用程序後打開gps,則會出現問題,因爲在地圖中不顯示位置。

這是對我的MapActivity我的代碼:

private MapView mapView; 
private List<Overlay> mapOverlays; 
private MapController mapController; 
private LocationManager locationManager; 
private MyLocationOverlay myLocationOverlay; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_map_sensor); 

    // Configure the Map 
    mapView = (MapView) findViewById(R.id.mapview); 

    mapView.setBuiltInZoomControls(true); 
    mapView.setSatellite(false); 
    mapController = mapView.getController(); 


    GeoPoint point = new GeoPoint(47.6945683 *1E6 ,11.5765886 *1E6); 
    mapController.animateTo(point); 

    mapController.setZoom(9); // Zoom 1 is world view 

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
      50, new GeoUpdateHandler()); 
    myLocationOverlay = new MyLocationOverlay(this, mapView); 
    mapView.getOverlays().add(myLocationOverlay); 


    mapOverlays = mapView.getOverlays(); 
} 

public class GeoUpdateHandler implements LocationListener { 

    @Override 
    public void onLocationChanged(Location location) {} 

    @Override 
    public void onProviderDisabled(String provider) {} 

    @Override 
    public void onProviderEnabled(String provider) {} 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) {} 
} 

我錯過了什麼?

回答

0

我想你需要在你打開GPS後再次觸發locationListener以獲得位置更新。

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
     50, new GeoUpdateHandler()); 

希望它的幫助。

編輯:在我的情況下,觸發位置更新代碼是在onResume方法。因此,如果GPS未打開並且用戶將其打開,而進入活動狀態,則會自動再次觸發。

+0

更確切地說,我應該怎麼做才能觸發locationListener? – lory105

+0

再次需要請求位置更新,如answer(locationManager.requestLocationUpdates(...))所示 – Braj

0

我想你只需要設置requestLocationUpdates()函數,你有。

GPS需要長達30秒的時間來給出第一次修復。

我個人讓我的Activity執行LocationListener它運作良好(API 7+)。

嘗試添加一個按鈕,將animateTo()您當前的位置,以檢查GPS在您的應用程序中正常工作,即使它已被啓用後。

也嘗試將您的位置相關代碼從onCreate()移至onResume()

相關問題