I have gone through a tutorial which was psoted on androidhive.com.I followed each and every step mentioned over there.I'm getting Latitude and Longitude as 0.0 and 0.0 respectively.. please help me...
I want to read current location of user through gps and want provide other active users locations to the current user.. My project is based on bus tracking via gps system... please help me..
here m posting my code...
Dynamic_Map.java
public class Dynamic_Map extends FragmentActivity {
private GoogleMap googleMap;
GPSTracker gps;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamic_map);
try{
gps = new GPSTracker(this);
if(gps.canGetLocation()){
if(gps.isGPSEnabled){
double latitudee = gps.getLatitude();
double longitudee = gps.getLongitude();
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitudee + "\nLong: " + longitudee, Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Your Location is not available bcos gps is "+gps.isGPSEnabled,Toast.LENGTH_LONG).show();
gps.showSettingsAlert();
}
}else{
gps.showSettingsAlert();
}
initilizeMap();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void initilizeMap(){
if(googleMap == null){
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.dynamic_map)).getMap();
googleMap.setMyLocationEnabled(true);
googleMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(gps.getLatitude(), gps.getLongitude())));
MarkerOptions marker = new MarkerOptions().position(new LatLng(gps.getLatitude(), gps.getLongitude())).title("Hello Maps");
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_maker_icon));
googleMap.addMarker(marker);
if(googleMap == null){
Toast.makeText(getApplicationContext(), "Sorry not able to create maps", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}
GPSTracker.java
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
private String provider;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000*60*1;
protected LocationManager locationManager;
public GPSTracker(Context context){
this.mContext = context;
getLocation();
}
public Location getLocation(){
try{
locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnabled && !isNetworkEnabled){
}else{
this.canGetLocation = true;
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(isNetworkEnabled){
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.GPS_PROVIDER);
if(location !=null){
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
return location;
}
@Override
public void onLocationChanged(Location location){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
@Override
public void onProviderDisabled(String provider){
Toast.makeText(this, "Disabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0){
return null;
}
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude, Toast.LENGTH_LONG).show();
}
return latitude;
}
public double getLongitude(){
if(location !=null){
longitude = location.getLongitude();
Toast.makeText(getApplicationContext(), "Your Location is - \nLng: " + longitude, Toast.LENGTH_LONG).show();
}
return longitude;
}
public boolean canGetLocation(){
return this.canGetLocation;
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle("GPS is settings");
alertDialog.setMessage("GPS is not enabled.Do you want to go to settings menu?");
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which){
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which){
dialog.cancel();
}
});
alertDialog.show();
}
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
}
I have included necessary permissions in AndroidManifest file...
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
Please help me.... this is my final year project.. please help
thank you :)