I would like to write an application for people tracking by using GPS in order to send the coordinates (Latitude and Longitude) via SMS to android phone and bring the coordinates values to display the accurate location on Google Map.
The working system is divided into 2 classes
1. SMSReceiver class: receive SMS then convert the coordinates values which are string values to be double values
Here is my code:
package com.google.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class SMSReceiver extends BroadcastReceiver {
public String slat;
public String slng;
public double dLat;
public double dLng;
public double getLat() {
return this.dLat;
}
public double getLng() {
return this.dLng;
}
@Override
public void onReceive(Context context, Intent intent) {
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String position1 = "";
if (bundle != null) {
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
position1 += msgs[i].getMessageBody().toString();
}
String position2 = position1.substring(position1.indexOf(" ")+1, position1.length());
slat = position1.substring(position1.indexOf(":")+1, position1.indexOf(" "));
slng = position2.substring(position2.indexOf(":")+1, position2.indexOf(" "));
dLat = Double.parseDouble(slat);
dLng = Double.parseDouble(slng);
}
}
}
2. Tracking class: bring the double value from SMS receiver class to display the location on Google Map
Here is my code:
package com.google.android;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
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 android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ZoomControls;
public class TrackingActivity extends MapActivity {
/** Called when the activity is first created. */
private LocationManager locationManager;
private LocationListener locationListener;
private MapView mapView;
private MapController mapController;
private Button btnSatelite;
private Button btnStreet;
private ZoomControls zoomControls;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
btnSatelite = (Button)findViewById(R.id.btnSatelite);
btnSatelite.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
mapView.setTraffic(false);
mapView.setSatellite(true);
}
}
);
btnStreet = (Button)findViewById(R.id.btnStreet);
btnStreet.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mapView.setSatellite(false);
mapView.setTraffic(true);
}
}
);
zoomControls = (ZoomControls)findViewById(R.id.zoomControls1);
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mapController.zoomIn();
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mapController.zoomOut();
}
});
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);
mapView = (MapView)findViewById(R.id.mapview1);
mapController = mapView.getController();
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
SMSReceiver Lat = null;
SMSReceiver Lng = null;
/*if (location != null) {
Toast.makeText(getBaseContext(), "Latitude: " + Lat.getLat() + " Longtitude: " + Lng.getLng(), Toast.LENGTH_SHORT).show();
}*/
GeoPoint point = new GeoPoint((int) (Lat.getLat() * 1E6),(int) (Lng.getLng() * 1E6));
mapController.animateTo(point);
mapController.setZoom(18);
...
mapView.invalidate();
}
...
@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
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
After I received the coordinate values from SMS, there is no location changing displayed on the map. I suppose that it may be because of Tracking class which cannot refer the coordinate values from SMS receiver class. Please give me some advice what should I do to solve this problem.
Basically I don’t understand your code : I don’t get the point of your SMSReceiver, if you are supposed to receive a new position through SMS you should implement a connection between your SMSReceiver and your MapActivity.
What you basically do here is to set a function that’s called when your GPS get a new fix (it’s NOT CALLED when you receive a new SMS) :
Moreover in your onLocationChanged() you set two object to null and try to access their property which of course should gives you a NullPointerException.
What I suggest you is to change your SMSReceiver to handle a listener on SMS reception.