title: How do I access current location in fragments?
body: <p>I want to access my current location in fragments. I wrote this program. There are no errors in the code, but it just show Google map only; it does not show my current location and marker.</p>
  
  <p>I'm confused in accessing permission from manifest and in getApplicationContext because I use <code>getActivty()</code> there instead of <code>this</code>, because it gives me error.</p>
  
  <p>What is wrong with my code?</p>
  
  <pre><code>void locationTracker() {
      SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
              .findFragmentById(R.id.map);
      mapFragment.getMapAsync(this);
      locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
      if ((ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) &amp;&amp; (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
          // TODO: Consider calling
          //    ActivityCompat#requestPermissions
          // here to request the missing permissions, and then overriding
          //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
          //                                          int[] grantResults)
          // to handle the case where the user grants the permission. See the documentation
          // for ActivityCompat#requestPermissions for more details.
          return;
      }
  
      if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
          locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
              @Override
              public void onLocationChanged(Location location) {
                  double latitude = location.getLatitude();
                  double longitude = location.getLongitude();
                  LatLng latLng= new LatLng(latitude,longitude);
                  Geocoder geocoder=new Geocoder(getActivity().getApplicationContext());
                  try {
                      List&lt;Address&gt; addressList = geocoder.getFromLocation(latitude,longitude,1);
                      String str = addressList.get(0).getLocality()+",";
                      str+= addressList.get(0).getCountryName();
                      mMap.addMarker(new MarkerOptions().position(latLng).title(str));
                      mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
  
  
              }
  
              @Override
              public void onStatusChanged(String provider, int status, Bundle extras) {
  
              }
  
              @Override
              public void onProviderEnabled(String provider) {
  
              }
  
              @Override
              public void onProviderDisabled(String provider) {
  
              }
          });
      } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
              @Override
              public void onLocationChanged(Location location) {
                  double latitude = location.getLatitude();
                  double longitude = location.getLongitude();
                  LatLng latLng= new LatLng(latitude,longitude);
                  Geocoder geocoder=new Geocoder(getActivity().getApplicationContext());
                  try {
                      List&lt;Address&gt; addressList = geocoder.getFromLocation(latitude,longitude,1);
                      String str = addressList.get(0).getLocality()+",";
                      str+= addressList.get(0).getCountryName();
                      mMap.addMarker(new MarkerOptions().position(latLng).title(str));
                      mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
  
  
              }
  
              @Override
              public void onStatusChanged(String provider, int status, Bundle extras) {
  
              }
  
              @Override
              public void onProviderEnabled(String provider) {
  
              }
  
              @Override
              public void onProviderDisabled(String provider) {
  
              }
          });
      }
  }
  </code></pre>