title: How to get latitude and longitude of current location in Android?
body: <p>I have a Fragment in which I want to show current location of user(device). I use Google Map Android API v2. How get current latitude and longitude of device?</p>
  
  <p>Here below you can see code that I used. Unfortunately app works wrong, not as i expected. It just show map but there is no marker that i set and no current location. Can anyone check it and fix it? Whats wrong? Thanks for any help!</p>
  
  <p>EDIT:</p>
  
  <p><strong>MainFragment.java</strong></p>
  
  <pre><code>public class MainFragment extends Fragment{
  
      private SupportMapFragment mSupportMapFragment;
      private GoogleMap mGoogleMap;
  
      public BroadcastReceiver receiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
  
          Bundle extras = intent.getExtras();
          LatLng temp = new LatLng(extras.getDouble("Latitude"),extras.getDouble("Longitude"));
      }
      };
  
      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          FragmentManager mFragmentManager = getChildFragmentManager();
          mSupportMapFragment = (SupportMapFragment) mFragmentManager.findFragmentById(R.id.map_container);
          if (mSupportMapFragment == null) {
              mSupportMapFragment = SupportMapFragment.newInstance();
              mFragmentManager.beginTransaction().replace(R.id.map_container, mSupportMapFragment).commit();
          }
  
          //Broadcast/ Filter
          IntentFilter filter = new IntentFilter();
          filter.addAction(LocationService.BROADCAST_ACTION);
          getActivity().registerReceiver(receiver, filter);
      }
  
      @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
             //Start service
             getActivity().startService(new Intent(getActivity(),LocationService.class));
      }
  
      @Override
      public void onResume(){
           receiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
  
              Log.d(TAG, "BroadcastReceiver works!");
  
              Bundle extras = intent.getExtras();
              final LatLng temp = new LatLng(extras.getDouble("Latitude"),extras.getDouble("Longitude"));
  
              if (mGoogleMap != null) {
                  mGoogleMap = mSupportMapFragment.getMap();
                  mGoogleMap.addMarker(new MarkerOptions().position(temp).title("Marker").snippet("Location"));
              }
              try {
                  CameraUpdate center = CameraUpdateFactory.newLatLng(temp);
                  CameraUpdate zoom = CameraUpdateFactory.zoomTo(15f);
                  mGoogleMap.moveCamera(center);
                  mGoogleMap.animateCamera(zoom);
              }catch (NumberFormatException e) {
                  //Some code here for exception
              }
          }
      };
      }
  
      @Override
      public void onDetach() {
          super.onDetach();
          try {
              Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
              childFragmentManager.setAccessible(true);
              childFragmentManager.set(this, null);
          } catch (NoSuchFieldException e) {
              throw new RuntimeException(e);
          } catch (IllegalAccessException e) {
              throw new RuntimeException(e);
          }
      }
  }
  </code></pre>
  
  <p><strong>fragment_main.xml</strong></p>
  
  <pre><code>&lt;RelativeLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:id="@+id/map_container"&gt;
  
         &lt;!-- The map fragments will go here --&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>