Creating Simple Android Vehicle Tracking System Application

Creating Vehicle Tracking system is very common project in Engineering field. It can easily accomplish using a simple GSM & GPS moduleS. Which will send a location message on your mobile after a specific interval.  The project can be enhance and can be more attractive if you create a android application and web portal. Which will show exact location of your Vehicle on a map. So, lets talk about Creating Simple Android Vehicle Tracking System Application.

You will find three java files in attached zip.

  • Main.java
  • SmsReceiver
  • AddItemizedOverlay

Main.java

This is code from Main.java. I will explain at the end of this code.

package com.robot.burraq2;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.robot.burraq2.R;

import android.os.Bundle;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.Toast;

public class Main extends MapActivity {

	long start;
	long stop;

	@Override
	public void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		Log.d("MapActivity", "Got new Data again");
		setMaptoProvidedLocation();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_burraq);

		setMaptoProvidedLocation();

	}

	// Setting Google Map to provided location
	private void setMaptoProvidedLocation() {
		// Default position
		String LON = "33.651686";
		String LAT = "73.156333";

		if (SmsReceiver.chk == 1) {

			LON = SmsReceiver.up_lon;
			LAT = SmsReceiver.up_lat;

			Log.v("CHECK", Integer.toString(SmsReceiver.chk));

			Log.v("SMS LAT", SmsReceiver.up_lat);
			Log.v("SMS Lon", SmsReceiver.up_lon);
		} else {
			SmsReceiver.chk = 0;
			Log.v("CHECK", Integer.toString(SmsReceiver.chk));
		}

		MapView mapView = (MapView) findViewById(R.id.themap);
		MapController control = mapView.getController();

		mapView.setBuiltInZoomControls(true);
		List<Overlay> overlayList = mapView.getOverlays();

		// Map Animate to new GeoPoints

		double lat = Double.parseDouble(LON); // latitude
		double lon = Double.parseDouble(LAT); // longitude
		GeoPoint geoPoint = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
		control.animateTo(geoPoint);
		control.setZoom(35);
		Toast.makeText(getApplicationContext(),
				"Your Location is - nLat: " + lat + "nLong: " + lon, 0)
				.show();

		// Display PIN
		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.pin_red);
		AddItemizedOverlay itemizedOverlay = new AddItemizedOverlay(drawable,
				this);
		OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello",
				"Sample Overlay item");
		itemizedOverlay.addOverlay(overlayitem);
		mapOverlays.add(itemizedOverlay);

	}

	// Menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_burraq, menu);
		return true;
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}

Main.java is the main file which shows map and location of your Vehicle. For map we have use google map API. You can set deafult location by changing following.

	// Default position
		String LON = "33.651686";
		String LAT = "73.156333";

You can also set it to exact location of your Vehicle by receiving a a message before app start. It may need a little logic. I hope you will able to make changes.

control.animateTo(geoPoint);

control.animateTo function is used to move map activity to new location.

SmsReceiver

package com.robot.burraq2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {
	// setting global variable.
	public static String up_lon;
	public static String up_lat;
	public static int chk = 0;

	@Override
	public void onReceive(Context context, Intent intent) {
		// ---get the SMS message passed in---
		Bundle bundle = intent.getExtras();
		SmsMessage[] msgs = null;
		String str = "";
		String str2 = "";

		// fetching message string
		if (bundle != null) {
			// ---retrieve the SMS message received---
			Object[] pdus = (Object[]) bundle.get("pdus");
			msgs = new SmsMessage[pdus.length];
			String sender_num = null;
			for (int i = 0; i < msgs.length; i++) {
				msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
				// str += "SMS from " + msgs[i].getOriginatingAddress();
				// str += " :";
				str = msgs[i].getMessageBody().toString();
				// str += "n";

				sender_num = msgs[i].getOriginatingAddress();

				// Divide message string to 2 parts longitude and latitude by
				// detection ","
				int flag = 0;
				str2 = str.substring(3, 4);
				up_lon = "";
				up_lat = "";
				for (int j = 0; j < str.length(); j++) {
					str2 = str.substring(j, j + 1);
					if (flag == 1) {
						up_lat += str2;
					}
					if (str2.equals(",")) {
						flag = 1;
					}
					if (flag == 0) {
						up_lon += str2;
					}
				}

				chk = 1;
				//Replace following Mobile number with yours
				if (sender_num.equals("+923315373547")) {
					abortBroadcast();

					// New Location fetched
					Toast.makeText(context, up_lat + up_lon, Toast.LENGTH_SHORT)
							.show();

					final Intent k = new Intent(context, Main.class);
					k.setFlags(k.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(k);
				}

			}
		}

	}

}

You only need to change your number here. It should be the sim number which you have inserted in your GSM module.

if (sender_num.equals("+923315373547")) {

AddItemizedOverlay.java

AddItemizedOverlay.java file is used to show pin on map and an extra function to show location as toast on touch event.

	public boolean onTouchEvent(MotionEvent event, MapView mapView) {

		if (event.getAction() == 1) {
			GeoPoint geopoint = mapView.getProjection().fromPixels(
					(int) event.getX(), (int) event.getY());
			// latitude
			double lat = geopoint.getLatitudeE6() / 1E6;
			// longitude
			double lon = geopoint.getLongitudeE6() / 1E6;

			Toast.makeText(context, "Lat: " + lat + ", Lon: " + lon,
					Toast.LENGTH_SHORT).show();

		}
		return false;
	}

 

All source files are attached in working form. You can make changes according to your needs. You may ask question in comments section if you need any help.

Download Android Vehicle Tracking System Application

Let's Discuss

  • Muhammad Shehzad Arshad Virk

    dear nauman thats nice artical please help me i want to run a function from android every 2 seconds and go on java server to get lat and long again and again is that posible

  • Muhammad Shehzad Arshad Virk

    i really need your help i have final university project on vehical tracking system in android have to get lat long on device and also have to send to server but the formate should be like i attached with picture

  • Azeem khan

    please visit this link http://eocean.pk/

  • Mohib Ahmed

    Nouman can you please tell me which is the best software for android programming??

  • Priyesh Sharma

    For android vehicle tracking system can you tell me the hardware components and their working?