Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i write this code for get current location of mobile but i have a problem bellow code is working fine for genymotion emulater and return longitude and latitude. but when run on mobile it show longitude 0.0 and latitude 0.0

plz help me about this problem
thanks

Java
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
	Button showlocation;
	GPSTracker gps;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	showlocation=(Button)findViewById(R.id.btnlocationfound);
	
	
	showlocation.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			gps=new GPSTracker(MainActivity.this);
			if(gps.cangetlocation()){
				double latitude=gps.getlatitude();
				double longitude=gps.getlongitude();
			Toast.makeText(getApplicationContext(),"your location is lati \n"+latitude+"\n"+"longitude:"+longitude,
					Toast.LENGTH_LONG).show();;
		
				
			}
			else{
				
				gps.showsettingalerts();
				
			}
			
		}
	});
	
	
	}

	}



/////////////GPSTracker class


Java
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.media.audiofx.BassBoost.Settings;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AlertDialog;

public class GPSTracker extends android.app.Service implements LocationListener{

	 final Context context;
	boolean isGPSEnable=false;
	boolean isNetworkEnable=false;
	
	boolean canGetLocation=false;
	Location location;
	double latiude;
	double longitude;
	private static final long min_time_between_updates=1000*60*1;
	private static final long min_diatance_cahnge_for_updates=10;
	
	protected LocationManager locationManager;
	
	
	
		
		
	
	public GPSTracker(Context context) {
	
	this.context=context;
	getlocation();
	
	}
	

	public Location getlocation(){
		try{
			locationManager=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
			isGPSEnable=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
			isNetworkEnable=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);			
			
			if (!isGPSEnable&&!isNetworkEnable) {
				
			}
			else{
				
				
				this.canGetLocation=true;
				if(isNetworkEnable){
					locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,min_time_between_updates, 
							min_diatance_cahnge_for_updates,  this);
					if(locationManager!=null){
						
						location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
						if(location!=null){
							latiude=location.getLatitude();
							longitude=location.getLongitude();							
						}//third inner						
					}//inner second					
				}//main if
				if(isGPSEnable){
					locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,min_time_between_updates, 
							min_diatance_cahnge_for_updates,  this);
					if(locationManager!=null){
						
						location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
						if(location!=null){
							latiude=location.getLatitude();
							longitude=location.getLongitude();							
						}//third inner						
					}//inner second					
				}//main if
				
				
				
				
			}
			
			
			
		}catch(Exception e){			
			e.printStackTrace();			
		}
		
		
		
		
		return location;
	}
	
	public void stopusingGPS(){
		
		if(locationManager!=null){
			locationManager.removeUpdates(GPSTracker.this);
			
		}
		
		
	}
	
	public double getlatitude(){
		if(location!=null){
			
			latiude=location.getLatitude();
			
		}
		
		return latiude;
	}
	public double getlongitude(){
		if(location!=null){
			
			longitude=location.getLongitude();
			
		}
		
		return longitude;
	}
	
	public boolean cangetlocation(){
		
		return this.canGetLocation;
		
	}
	
	public void showsettingalerts(){
		
		AlertDialog.Builder alrtdialog= new AlertDialog.Builder(context);
		alrtdialog.setTitle("GPS is setting");
		alrtdialog.setMessage("GPS is not enable do you want to go to setting");
		alrtdialog.setPositiveButton("Setting",new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent i=new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
				context.startActivity(i);
			}
		});
alrtdialog.setNegativeButton("cancel",new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();}
		});
		alrtdialog.show();
	}
	
	
	
	
	@Override
	public void onLocationChanged(Location arg0) {
		// TODO Auto-generated method stub
		
	}

	@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
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

}
Posted
Comments
Richard MacCutchan 28-Nov-15 11:15am    
Is the tracker feature switched on?
Member 11504333 28-Nov-15 13:23pm    
if u talking about GPS its on.
Member 11504333 28-Nov-15 12:23pm    
how can check tracker feature on or not ?

1 solution

Its a very known problem. There are couple of issues behind the scene and you need to solve them in order to make the code working.

First, is your device connected with internet? If not, give you device some time to extract your location info from GPS.Or, connect your device with internet.

Second, is your device's gps working properly? Is it ON?

Third, check the condition if the code can extract user location from his device. Debug this line
Java
if(gps.cangetlocation())
carefully. If all are ok, this will return a TRUE values and you are ready to go.

Fourth, GPS needs sometime to get user location depending on your location depth. So wait patiently, sometimes device restart solves the problem too.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900