0

I am trying to create an android application where I want to fetch my current location and want to zoom it. When I am outdoor or in open sky, I can fetch my location. But after some time when I try to debug the app in my room or indoors, its not able to fetch my current location. I still don't understand what is the problem. Any help would be appreciated. Down below is my code. Thank You!

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    tvDistanceDuration = (TextView)findViewById(R.id.textView5);

    super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);

    // Makes Progress bar Visible
    getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

    //tvDistanceDuration = (TextView) findViewById(R.id.tv_distance_time);

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {

    mMap = googleMap;


    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        mMap.setMyLocationEnabled(true);
    }
    mMap.setMyLocationEnabled(true);
    mMap.getUiSettings().setCompassEnabled(true);
    mMap.getUiSettings().setZoomControlsEnabled(true);
    mMap.getUiSettings().setMyLocationButtonEnabled(true);
    mMap.getUiSettings().setMapToolbarEnabled(true);

    // Determining Current Location

    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(provider);
    Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());


    if (location != null) {
        double latitude = location.getLatitude();  // get current latitude
        double longitude = location.getLongitude();  // get current longitude
        myPosition = new LatLng(latitude, longitude);

        LatLng coordinate = new LatLng(latitude, longitude);
        CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 13);
        mMap.animateCamera(yourLocation);
    }

}
Nair123
  • 1
  • 4
  • where are you getting null pointer exception ? Show the logcat. – tahsinRupam Feb 19 '17 at 08:08
  • GPS needs line-of-site to several satellites in order to work. You should not expect to get an accurate (or indeed any) location fix indoors. – Jameson Feb 19 '17 at 08:10
  • @tahsinRupam I was getting Exception in other line, I have edited my question. Its not giving me any Exception. Its just not able to fetch my current location. – Nair123 Feb 19 '17 at 08:18
  • @Jameson But when i open in built Google Maps in my phone, it shows my current location. – Nair123 Feb 19 '17 at 08:18

0 Answers0