title: LocationManager Provider
body: <p>I dont know how to writr the code to choose if possible the Network provider or if Network is not available then GPS provider.
  How can i change the code to get this.
  This is my first android application and i tried to do it but i wasn't successfull.</p>
  
  <pre><code>package com.example.googlemapslbs;
  
  import java.util.List;
  import com.google.android.gms.maps.GoogleMap;
  import com.google.android.gms.maps.MapFragment;
  import com.google.android.gms.maps.model.LatLng;
  import com.google.android.gms.maps.model.MarkerOptions;
  import android.app.Activity;
  import android.content.Context;
  import android.os.Bundle;
  import android.widget.Toast;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import com.google.android.gms.maps.CameraUpdateFactory;
  import android.location.Criteria;
  import android.util.Log;
  
  public class MainActivity extends Activity {
      GoogleMap Map;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          try {
              // Load map
              initilizeMap();
              } 
          catch (Exception e) {
              e.printStackTrace();
          }
  
      }
  
  
      private void initilizeMap() {
          if (Map == null) {
              Map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
  
              // map type normal (other possible choices: hybrid, terrain, satellite)
              Map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
  
             // enable my-location 
             Map.setMyLocationEnabled(true);
             LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  
             //
             Criteria criteria = new Criteria(); 
             String provider = locationManager.getBestProvider(criteria, true);
  
             //getting Current Location
             Location location = locationManager.getLastKnownLocation(provider);
  
             if(location == null){
                 LocationListener locationListener = new LocationListener() {
  
                  @Override
                  public void onLocationChanged(Location location) {
                      double lat= location.getLatitude();
                      double lng = location.getLongitude();
                      LatLng ll = new LatLng(lat, lng);
                      Map.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 18));                    
                  }
                  @Override
                  public void onProviderDisabled(String provider) {}
                  @Override
                  public void onProviderEnabled(String provider) {}
                  @Override
                  public void onStatusChanged(String provider, int status,
                          Bundle extras) {}
                  };
               //Log.d("APpln", "well hello there &gt;&gt;"+location);
  
                  //location updates - requestLocationUpdates(provider, minTime, minDistance, listener)
                  locationManager.requestLocationUpdates(provider, 20000, 0, locationListener);
  
             }else{
                 double lat= location.getLatitude();
                 double lng = location.getLongitude();
  
                 LatLng ll = new LatLng(lat, lng);
  
                 //modify the map's camera - zoom 18
                 Map.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 18));
             }
  
  
          // Define a listener that responds to location updates
             LocationListener locationListener = new LocationListener() {
  
                 public void onLocationChanged(Location location) {
  
                   // Called when a new location is found by the network location provider.
                   makeUseOfNewLocation(location);
                 }
  
                 private void makeUseOfNewLocation(Location location) {
                     double lat = location.getLatitude();
              double lng = location.getLongitude();
                  LatLng ll = new LatLng(lat, lng);
                  Toast.makeText(getApplicationContext(), "Your Current Location \nLat: " + lat + "\nLng: " + lng, Toast.LENGTH_LONG).show();
  
              }
  
              public void onStatusChanged(String provider, int status, Bundle extras) {}
  
                 public void onProviderEnabled(String provider) {}
  
                 public void onProviderDisabled(String provider) {}
               };
  
               locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), 0, 0, locationListener);
  
              // check if map is created successfully or not
              if (Map == null) {
                  Toast.makeText(getApplicationContext(),
                          "ups... can not create map", Toast.LENGTH_SHORT)
                          .show();
              }
          }
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          initilizeMap();
      }
  
      }
  </code></pre>