0

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

}

m.v.n.kalyani
  • 760
  • 7
  • 20

1 Answers1

1

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

  • k fine.I am working on google map,i am updating latitude and longitude for every 2 seconds.with in current location only i am getting much variances in latitude and logitude.even i am in constant place,then also getting this variance??why we are getting this varience???do u have any idea??I am using network provider. – m.v.n.kalyani Jun 10 '15 at 12:14
  • i have done a similar app which get's latitude longitude via sms. you try to save those latitude a longitudes in a String and then place marker on map. – Praveen Kumar Jun 10 '15 at 12:18
  • i am saving those latitudes and longitudes in arraylist and i used at the end to construct a root .but problem is getting variance if i am in constant place also. – m.v.n.kalyani Jun 10 '15 at 12:20
  • if you stored them in `ArrayList` save them get them and store in String by using their postion in List. Check this [link](http://stackoverflow.com/questions/8082688/convert-arraylist-to-string-array-in-android). – Praveen Kumar Jun 10 '15 at 12:25
  • and then you can draw line between every marker using `addPolyline` method like in the following code snippet `Polyline line=mMap.addPolyline(new PolylineOptions().add(new LatLng(latitude1,longitude1),new LatLng(latitude2,longitude2),new LatLng(latitude3,longitude3),new LatLng(latitude4,longitude4),new LatLng(latitude5,longitude5),new LatLng(latitude6,longitude6)).width(10).color(Color.BLUE));` – Praveen Kumar Jun 10 '15 at 12:27
  • if i have thousands of points in arraylist,how can i add all point?that's why i added entair arraylist at a time.by using the below code. googleMap.addPolyline((new PolylineOptions()).addAll(points) .width(5).color(Color.BLUE).geodesic(true)); – m.v.n.kalyani Jun 11 '15 at 04:39