2015-12-29 82 views
1

這是我的主要活動,在這裏它的工作正常,但是如果我們再次加載地圖時它會一次不顯示當前位置,因爲它是第一次(即當onCreate()被調用。)GPSTracker文件實現LocationListner接口,並提供我哪些我傳遞給goToLocation(緯度,經度)當前位置谷歌地圖不是恢復當前位置的動畫

protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.before_login_map_activity); 
 
     progressView = (View) findViewById(R.id.progress_bar); 
 
     if (initMap()) { 
 
     gpsTracker = new GPSTracker(this); 
 
     if (gpsTracker.canGetLoaction()) { 
 
      originlon = gpsTracker.getLongitide(); 
 
      originlat = gpsTracker.getLattitude(); 
 
      mMap.setMyLocationEnabled(true); 
 
      mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
 
      goToLocatoin(originlat, originlon); 
 
     } 
 
     getJsonLatLon(); 
 
     } else { 
 
     Toast.makeText(BeforeLoginMapActivity.this, "Map Not Available!", Toast.LENGTH_SHORT).show(); 
 
     }
<fragment android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" tools:ignore="MissingPrefix" android:layout_width="match_parent" android:layout_height="match_parent" />

+0

你爲什麼不乾脆轉移您的實現'onResume'? – Androiderson

+0

謝謝Androiderson,爲您提供幫助,但是我使用了CameraPosition類,它對我很有幫助。提供下面的代碼 – Mrinmoy

回答

2

1)聲明一個私人CameraPosition變量 2)然後覆蓋onPause()從GoogleMap的實例getCameraPosition 3)再一次重寫的onResume(),以instace的GoogleMap提供CameraPosition

//Globally declare CameraPosition 
 
private CameraPosition cp; 
 

 

 
@Override 
 
protected void onPause() { 
 
    super.onPause(); 
 
    cp = mMap.getCameraPosition(); 
 
    mMap = null; 
 
} 
 

 
@Override 
 
protected void onResume() { 
 
    super.onResume(); 
 
    if (cp != null) { 
 
    mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cp)); 
 
    cp = null; 
 
    } 
 
}

相關問題