2017-03-03 51 views
0

我第一次使用googleMap,所以PLZ幫助我。我想在地圖上顯示用戶在打開應用程序時的位置。當用戶已經打開gps時,我已經這樣做了。開啓GPS後,我如何獲得自己的位置

LocationManager locationManagerCt = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE); 

    locationCt = locationManagerCt 
      .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
LatLng latLng=new LatLng(locationCt.getLatitude(), locationCt.getLongitude()); 
      MarkerOptions marker = new MarkerOptions().position(latLng).title("Current Location").snippet("Discription"); 

      marker.icon(BitmapDescriptorFactory 
        .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)); 

      // Moving Camera to a Location with animation 
      CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(latLng).zoom(15).build(); 

      HomeActivity.mGoogleMap.animateCamera(CameraUpdateFactory 
        .newCameraPosition(cameraPosition)); 

      HomeActivity.mGoogleMap.addMarker(marker); 

在GPS關閉。我打開Gps後嘗試做同樣的事情。 我開啓GPS,從這裏開始: https://stackoverflow.com/a/33555732/3393578

用戶後打開GPS

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
if (requestCode == 1000) { 
    if(resultCode == Activity.RESULT_OK){ 
     //after user press ok button on GPS popup 

     /*I've apply same code above but unable to get location 
      its give me error on Location (nullpointer) */ 


    } if (resultCode == Activity.RESULT_CANCELED) { 
    } 
} 

}

如何在幾秒鐘內打開GPS後得到的位置。 我不想刷新活動。

謝謝!

回答