In my android application i am trying to get the gps location on a button click. i have tried many examples but the examples work on certain devices. The device on which i am testing i.e Micromax A90S, i am unable to get a location on it. i am unable to find out what exactly is the issue. Is it the device or do i need to do some changes in my code??? The code i am using is given below
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(SettingsActivity.this, message,
Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(SettingsActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(SettingsActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(SettingsActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(SettingsActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
Please help!! Thanks in advance!
Related
I have the following code that takes around 1.5 minutes to find a location on two tablets that I've tested on, and it also takes around 30 seconds to load on a phone without GSM/CDMA enabled. Does anyone have any ideas why this takes so long to run without cell service? I have declared the class containing this code to be a LocationListener.
Here is my code:
This is in my OnCreate method:
locationNotificationBus = new EventBus();
locationManager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
Criteria locationCriteria = new Criteria();
locationCriteria.setAccuracy( Criteria.ACCURACY_COARSE );
String provider = locationManager.getBestProvider( locationCriteria, false );
locationManager.requestLocationUpdates(provider,0,0,this);
This is declared inside of the class itself:
#Override
public void onLocationChanged(Location location) {
Log.wtf( "LOCATOR: ", "Location Has Changed." );
Log.d("New Location", "Lat: " + location.getLatitude() + " Long: " + location.getLongitude());
locationManager.removeUpdates(this);
locationNotificationBus.post(location);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("LOCATOR: ", "Status Has Changed.");
}
#Override
public void onProviderEnabled(String provider) {
Log.d("LOCATOR: ", "Provider enabled.");
}
#Override
public void onProviderDisabled(String provider) {
Log.d("LOCATOR: ", "Provider disabled.");
}
It is my understanding that Coarse location can also use Wi-Fi to determine location. Is this not the case? If not, how do I go about using Wi-Fi?
i am trying to find location using gps but the following code of snippet creating problem. The error is displaying as
The method makeText(Context, CharSequence, int) in the type Toast is not applicable
for the arguments (GeocodingMainActivity, String, int)
..........
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String format = String.format( "New Location \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude());
String message = format;
Toast.makeText(GeocodingMainActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b)
{Toast.makeText(GeocodingMainActivity.this, "Provider status changed",Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(GeocodingMainActivity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(GeocodingMainActivity.this,"Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
}
Try this...
Toast.makeText(getApplicationContext(), "message", Toast.LENGTH_LONG).show();
Try this code
Toast.makeText(MyAndroidAppActivity.this,"String!", Toast.LENGTH_LONG).show();
String address=locationText.getText() + "\n"+addresses.get(0).getAddressLine(0)+", "+
addresses.get(0).getAddressLine(1)+", "+addresses.get(0).getAddressLine(2);
Toast.makeText(getApplicationContext(),address,Toast.LENGTH_LONG).show();
Replace your MyLocationListener with below code. and pass context when you use your MyLocationListener.
private class MyLocationListener implements LocationListener {
private Activity mContext;
public MyLocationListener(Activity context) {
this.mContext = context;
}
public void onLocationChanged(Location location) {
String format = String.format( "New Location \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude());
String message = format;
Toast.makeText(mContext, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b)
{
Toast.makeText(mContext, "Provider status changed",Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(mContext,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(mContext,"Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
}
I wrote code for finding the latitude and longitude of a place and it works well in my PC using Eclipse but it does not work on my Android phone.
public class MainActivity extends Activity {
protected LocationManager locationManager;
protected Button retrieveLocationButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1,
1000, new MyLocationListener());
retrieveLocationButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG)
.show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG)
.show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(MainActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(MainActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(MainActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
}
Ensure that you have given all the required permissions in AndroidManifest file to access gps...
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.permission.READ_PHONE_STATE
android.permission.INTERNET
See this link : https://sites.google.com/site/androidhowto/how-to-1/using-the-gps
Here is the code to find the current location, but nothing shows on the screen after pressing the retreive button.
I have reset adb and tried to send the data from emulator control manually. But when I send data using emulator control, then the emulator restarts but never shows the home screen.
Please help. Thanks in advance.
public class LbsGeocodingActivity extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
protected LocationManager locationManager;
protected Button retrieveLocationButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
retrieveLocationButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(LbsGeocodingActivity.this, message,
Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(LbsGeocodingActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(LbsGeocodingActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
}
In you code you might not be receiving the current location(this is because in you need to push your location manually through emulator control).
Just have else case also in showCurrentLocation() and check whether the location object is null or not like below.
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(LbsGeocodingActivity.this, message,
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(LbsGeocodingActivity.this, "location is null",
Toast.LENGTH_LONG).show();
}
}
On our first attempts to use the location sensors we had similar problems. Two things help the most in order to get the best results from GPS.
On the emulator the Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); causes a lot force closes.
And most important. Its far more stable to input the gps data through telnet. telnet localhost 5554 (You can see the port on the emulators window title). The command for a new gps fix is: geo fix lat lng
I want to determine my curent position (latitude, longitude and altitude).
In my database, I have a table contents by table location (id,name,address,longitude,latitude,altitude)
How can I determine my curent position and the most near location?
I Added this in manifest.xml:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
How can i use the methods (.getlongitude(); .getLatitude(); .getaltitude()
In documentation this is not clear.
Thank you in advance.
You can choose to either get the last known location, or listen for updates. The easiest way is to get the latest:
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
If you want an accurate location, you need to listen forupdates using requestLocationUpdates().
Also, remember to check whether the GPS device is enabled, see How do I find out if the GPS of an Android device is enabled
To find the nearest addresses, see http://developer.android.com/reference/android/location/Geocoder.html
First Enable Ur GPS.
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
public static double lontitube,latitude;
protected LocationManager locationManager;
protected Location currentLocation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
}
protected void performReverseGeocodingInBackground() {
showCurrentLocation();
}
protected void showCurrentLocation() {
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (currentLocation != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
currentLocation.getLongitude(), currentLocation.getLatitude()
);
lontitube=currentLocation.getLongitude();
latitude=currentLocation.getLatitude();
System.out.println("----lati----longi----"+latitude+"\n"+lontitube);
Toast.makeText(Demo.this, message,
Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(Demo.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(Demo.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(Demo.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(Demo.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
In AndroidManifest add
ACCESS_FINE_LOCATION
permission for GPS use.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />