2017-03-03 8 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秒以内に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) { 
    } 
} 

}

上のユーザのターンの後 https://stackoverflow.com/a/33555732/3393578

: は、私はここからGPSのターンです。 私は活動をリフレッシュしたくありません。

ありがとうございました!

答えて

0

本当にうまく動作しますが、今古いである代わりにLocationManagerのFusedLocationApiに切り替えます。 FusedLocationAPIのrequestLocationUpdates()メソッドを使用する必要があります。

詳細については、googleサンプルをご覧ください。 https://github.com/googlesamples/android-play-location

関連する問題