Download Code

Android GPS Location Services Map

In android gps location services map tutorial you will learn how to map a marker on a current location of any user using location services(GPS).Before starting this tutorial you must have some knowledge of how to use map in android application.How to use location services GPS in android.You can read basic from this Google_Map_V2 tutorial.

When you go through the Google_Map_V2 tutorial.You will know how to create Android Map API key.How to import Google_Play_Services_Lib.How to load map in fragment.

Create new android gps location services map application project.Go to activity_main.xml and add fragment.

location services,map,map in android,google map,google map in android,how to display google map in android application,what is API KEY,how to generate android API key, fragments,mapfragment,mapfragment in android,how to add marker on google map, how to add marker on google map android,how to add marker on map android, how to add marker on map android tutorial,google map android tutorial, how to get current location on map android, how to get current location using GPS and plot on google map, how to get current location using GPS and plot on map android, how to get current location using GPS and plot on google map android, how to get current location using GPS and plot on google map tutorial, how to animate camera on google map,how to animate camera to marker, how to animate camera on marker on google map, how to animate camera on marker android tutorial, GPS,how to use GPS with google map android,how to get current location using gps android,onlocationchanged,onlocationchange in android,locationManager, alertdialog,alertdialog in android,how to popup a dialog for GPS setting, how to popup location setting in android,how to get latitude and longitude, how to get latitude and longitude in android,how to get latitude and longitude using GPS in android,android gps,android gps location,android gps location services,android gps location services map,

Create a new class GPS.java.It’s a Service class used to get location using NETWORK or GPS.

GPS.java

package developer.codecmap;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

public class GPS extends Service implements LocationListener{

	private final Context con;     // pointer this class means context(this)
	boolean GPS_Status = false;     // GPS status variable activated or not
	boolean Network_Status = false; // Network service status checker
	boolean able_To_Get_Location = false;   // status to check whether whether GPS or Network gives the location
	Location location;                    // location variable through which we will get latitude and longitude

	private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // The minimum distance to change Updates in meters
	private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // The minimum time between updates in milliseconds
	protected LocationManager locationManager; // Location Manager variable
	double latitude; // latitude
	double longitude; // longitude
	// constructor for GPS class to set the context and get user location
	public GPS(Context con){
		this.con = con;
		getUserLocation();
	}
	// called this method when location changes
	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		this.location = location;
		getLatitude();
		getLongitude();
	}
	// called this method when location services changed.
	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

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

	}
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}


	public Location getUserLocation(){
		try{
			locationManager = (LocationManager) con.getSystemService(LOCATION_SERVICE);

			// getting GPS status
			GPS_Status = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

			// getting network status
			Network_Status = locationManager
					.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

			if (!GPS_Status && !Network_Status) {
				// no network provider is enabled
			} else {
				this.able_To_Get_Location = true;
				// First get location from Network Provider
				if (Network_Status) {
					locationManager.requestLocationUpdates(
							LocationManager.NETWORK_PROVIDER,
							MIN_TIME_BW_UPDATES,
							MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
					Log.d("Network", "Network");
					if (locationManager != null) {
						location = locationManager
								.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
						if (location != null) {
							latitude = location.getLatitude();
							longitude = location.getLongitude();
						}
					}
				}
				// if GPS Enabled get lat/long using GPS location Services
				if (GPS_Status) {
					if (location == null) {
						locationManager.requestLocationUpdates(
								LocationManager.GPS_PROVIDER,
								MIN_TIME_BW_UPDATES,
								MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
						Log.d("GPS Enabled", "GPS Enabled");
						if (locationManager != null) {
							location = locationManager
									.getLastKnownLocation(LocationManager.GPS_PROVIDER);
							if (location != null) {
								latitude = location.getLatitude();
								longitude = location.getLongitude();
							}
						}
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return location;
	}

	// Stopping GPS
	public void stopUsingGPS(){
		if(locationManager != null){
			locationManager.removeUpdates(GPS.this);
		}       
	}

	/**
	 * Function to get latitude
	 * */
	public double getLatitude(){
		if(location != null){
			latitude = location.getLatitude();
		}

		// return latitude
		return latitude;
	}

	/**
	 * Function to get longitude
	 * */
	public double getLongitude(){
		if(location != null){
			longitude = location.getLongitude();
		}

		// return longitude
		return longitude;
	}

	// check wifi and GPS status working or not
	public boolean able_To_Get_Location() {
		return this.able_To_Get_Location;
	}
	// alert to enable your GPS or Network setting
	public void showSettingsAlert(){
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(con);

		// Setting Dialog Title
		alertDialog.setTitle("GPS is settings");

		// Setting Dialog Message
		alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

		// On pressing Settings button
		alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,int which) {
				Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
				con.startActivity(intent);
			}
		});

		// on pressing cancel button
		alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		// Showing Alert Message
		alertDialog.show();
	}
}

Go to MainActivity.java class.

  • Refer fragment from xml and use MapFragment with GoogleMap class to load map.
  • Create variable for GPS.java class to get current location.
  • Add Marker on Google Map.
  • Animate Camera to that Marker.

MainActivity.java

package developer.codecmap;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends Activity {
	// Google Map
	private GoogleMap googleMap;
	GPS myGPS;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		myGPS = new GPS(this);
		try {
			// Loading map
			initilizeMap();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * function to load map. If map is not created it will create it for you
	 * */
	private void initilizeMap() {
		if (googleMap == null) {
			googleMap = ((MapFragment) getFragmentManager().findFragmentById(
					R.id.map)).getMap();
			// latitude and longitude
			if(myGPS.able_To_Get_Location()){
				double latitude = myGPS.getLatitude();
				double longitude = myGPS.getLongitude();


				// create marker
				MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Android Devevlopers ");

				CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(12).build();

				googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));


				// adding marker
				googleMap.addMarker(marker);
				// check if map is created successfully or not
				if (googleMap == null) {
					Toast.makeText(getApplicationContext(),
							"Sorry! unable to create maps", Toast.LENGTH_SHORT)
							.show();
				}
			}
			else{
				myGPS.showSettingsAlert();   // dialog to enable location services(GPS)
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		initilizeMap();
	}
}

At last go to Android Manifest.xml File and add some permission with Google Map API KEY.

location services,map,map in android,google map,google map in android,how to display google map in android application,what is API KEY,how to generate android API key, fragments,mapfragment,mapfragment in android,how to add marker on google map, how to add marker on google map android,how to add marker on map android, how to add marker on map android tutorial,google map android tutorial, how to get current location on map android, how to get current location using GPS and plot on google map, how to get current location using GPS and plot on map android, how to get current location using GPS and plot on google map android, how to get current location using GPS and plot on google map tutorial, how to animate camera on google map,how to animate camera to marker, how to animate camera on marker on google map, how to animate camera on marker android tutorial, GPS,how to use GPS with google map android,how to get current location using gps android,onlocationchanged,onlocationchange in android,locationManager, alertdialog,alertdialog in android,how to popup a dialog for GPS setting, how to popup location setting in android,how to get latitude and longitude, how to get latitude and longitude in android,how to get latitude and longitude using GPS in android,android gps,android gps location,android gps location services,android gps location services map,

Note: Don’t forget to import “google_play_services_lib” in your project.

Run your android gps location services map application project.

location services,map,map in android,google map,google map in android,how to display google map in android application,what is API KEY,how to generate android API key, fragments,mapfragment,mapfragment in android,how to add marker on google map, how to add marker on google map android,how to add marker on map android, how to add marker on map android tutorial,google map android tutorial, how to get current location on map android, how to get current location using GPS and plot on google map, how to get current location using GPS and plot on map android, how to get current location using GPS and plot on google map android, how to get current location using GPS and plot on google map tutorial, how to animate camera on google map,how to animate camera to marker, how to animate camera on marker on google map, how to animate camera on marker android tutorial, GPS,how to use GPS with google map android,how to get current location using gps android,onlocationchanged,onlocationchange in android,locationManager, alertdialog,alertdialog in android,how to popup a dialog for GPS setting, how to popup location setting in android,how to get latitude and longitude, how to get latitude and longitude in android,how to get latitude and longitude using GPS in android,android gps,android gps location,android gps location services,android gps location services map,

That’s it for this android gps location services map tutorial.

Thanks.