how to display google map with out having internet connection in android

131 views Asked by At

My google map is not displaying with out having internet connection.google map is not loaded.but getting current location latitude and longitude.how to get google map with out having internet connection.I tried below code. 'public class MainActivity extends FragmentActivity implements LocationListener {

private static final int GPS_ERRORDIALOG_REQUEST = 0;
GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     if (servicesOK()) {
            Toast.makeText(this, "Ready to map!!", Toast.LENGTH_LONG).show();
            setContentView(R.layout.activity_main);

        } else {
             Toast.makeText(this, "Not Ready to show map!!", Toast.LENGTH_LONG).show();

        }


    // Getting reference to the SupportMapFragment of activity_main.xml
    SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    // Getting GoogleMap object from the fragment
    googleMap = fm.getMap();

    // Enabling MyLocation Layer of Google Map
    googleMap.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
    Location location = locationManager.getLastKnownLocation(provider);

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

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

}
public boolean servicesOK() {

    int isAvailable = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);

    if (isAvailable == ConnectionResult.SUCCESS) {

        return true;

    } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {

        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
                this, GPS_ERRORDIALOG_REQUEST);
        dialog.show();

    } else {

        Toast.makeText(this, "Cant connect!!", Toast.LENGTH_SHORT).show();

    }
    return false;
}

@Override
public void onLocationChanged(Location location) {

    TextView tvLocation = (TextView) findViewById(R.id.tv_location);

    // Getting latitude of the current location
    double latitude = location.getLatitude();

    // Getting longitude of the current location
    double longitude = location.getLongitude();     

    // Creating a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);

    // Showing the current location in Google Map
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    // Zoom in the Google Map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

    // Setting latitude and longitude in the TextView tv_location
    tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );      

}

@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      
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

1

There are 1 answers

6
Praveen Kumar On BEST ANSWER

Your phone must need internet connection to load map for the first time. Then it'll show up easily.