GPS not showing current location on map

1k views Asked by At

I am facing a problem, I am trying to show current location on map. When I use wifi, it works fine, but when I turn the GPS on, it stops showing it, again I turn off GPS it works.. what shall I do, I need to show my location as well as the GPS.

my code is:

SupportMapFragment fragment = ( SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_test);

    // Getting Google Map
    mGoogleMap = fragment.getMap();

    // Enabling MyLocation in Google Map
    mGoogleMap.setMyLocationEnabled(true);



    // Getting LocationManager object from System Service LOCATION_SERVICE
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    // Creating a criteria object to retrieve provider
    Criteria criteria = new Criteria();

    // Getting the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);

    // Getting Current Location From GPS
    Location location = locationManager.getLastKnownLocation(provider);

    if(location!=null){
            onLocationChanged(location);
    }

    locationManager.requestLocationUpdates(provider, 20000, 0, this);



    mGoogleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker arg0) {
            // TODO Auto-generated method stub
            System.out.println("STREEEETTT111:  "+db.getIdn(arg0.getTitle()));
            Intent i=new Intent(MapTest.this,PharmacyDetails.class);

            i.putExtra("name", arg0.getTitle());
            startActivity(i);
        }
    });
}
@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    mLatitude = location.getLatitude();
    mLongitude = location.getLongitude();
    LatLng latLng = new LatLng(mLatitude, mLongitude);

    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(15));


}
@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}
@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

and I want to attach the screenshots to make my question more clear:

When the GPS is off:

gps off

And when the GPS is on:

enter image description here

Please help, thankx

0

There are 0 answers