Get last location latitude and longitude - android

This is my code.
public class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
Location location = getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
LastLocationLat = location.getLatitude();
LastLocationLongi = location.getLongitude();
LocationLat = loc.getLatitude();
LocationLongi = loc.getLongitude();
if(loc.hasSpeed()) {
float mySpeed = loc.getSpeed();
Toast.makeText(getApplicationContext(), ""+mySpeed, 2000).show();
}
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(getApplicationContext(), "Gps onStatusChanged", Toast.LENGTH_SHORT).show();
}
}
But i did not get last location in this code i got same latitude and longitude

You are using LocationManager.NETWORK_PROVIDER to get locations. Your code implies you want GPS ("Gps Disabled")? In this case you should use LocationManager.GPS_PROVIDER.
LocationManager.NETWORK_PROVIDER is not very accurate (few hundred meters if locking to cell towers), so you might not detect the location change if you move less then 100m.
On the other hand, GPS is usually unavailable indoors or between tall buildings. Also GPS uses power so should not be used all the time (when app is in the background).
Read about Obtaining User Location, for the best approach to getting location with regards to accuracy vs service availability.

Hope this can help:
http://www.tutorials-android.com/learn/How_to_get_the_last_known_GPS_location.rhtml

Related

Get Location without using GPS and without Google Services via official API

I wonder if it's possible to get a location without Google Services and without using GPS (and thus using NETWORK_PROVIDER) on an Android device. My current code looks like this:
public void onButtonTap(View view) {
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// we're done here
printLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) { }
public void onProviderEnabled(String provider) { }
public void onProviderDisabled(String provider) { }
};
String provider = LocationManager.NETWORK_PROVIDER;
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
Location location = null;
try {
location = locationManager.getLastKnownLocation(provider);
} catch (SecurityException se) {
}
if(location != null) {
// we're done here
printLocation(location);
}
if(location == null) {
try {
locationManager.requestSingleUpdate(provider, locationListener, null);
} catch (SecurityException se) {
}
} // end of onButtonTab (formatting issues)
private void printLocation(Location location) {
String latitude = String.format(Locale.US, "%.4f", location.getLatitude());
String longitude = String.format(Locale.US, "%.4f", location.getLongitude());
String slocation = "lat: " + latitude + ", lon: " + longitude;
Toast.makeText(getApplicationContext(), slocation, Toast.LENGTH_LONG).show();
}
So I basically query for the last location and if there's none I request an update. This works for PASSIVE_PROVIDER (if there's GPS activity) and of course for GPS_PROVIDER but I don't get a location with NETWORK_PROVIDER. The listeners method onLocationChanged never gets called. The app has the right permissions and locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) is true.
Is this functionality just not working because there's no database (owned by Google and provided by Google Services) to compare against? Or does AOSP build it's own database over time (learning from GPS Data)? I doubt the later.
Little extra question: I can't use Fused Location Provider API as I don't have Google Services installed, can I?

How do I get walking speed using GPS?

I am trying to track my accurate walking speed. I am using the getSpeed() method of the LocationManager class. The problem is, the speed is not accurate, also answered by AlexWien here . Wein claims "The higher the speed, the more acurate it is. Under 10km/h it does not work well." So what would be the most precise way to do this if getSpeed is not built for precision? I did not stumble upon anything else yet.
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
location.getLatitude();
Toast.makeText(context, "Current speed:" + location.getSpeed(),
Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
PS:: I understand that both Location and Fine Location permissions are required in this case.

How do i get my current location without GPS?

I want to get my current latitude and longitude using Mobile network provider, not GPS. AKA it should work without GPS with just your sim card. i have found many tutorials online claiming they find location with gps or network. however, they all seem to use GPS only! here or here and also here i don't want last knows location, as i don't wish to use GPS
Try This Code you can get current location without opening GPS
btnNWShowLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Location nwLocation = appLocationService
.getLocation(LocationManager.NETWORK_PROVIDER);
if (nwLocation != null) {
double latitude = nwLocation.getLatitude();
double longitude = nwLocation.getLongitude();
Toast.makeText(
getApplicationContext(),
"Mobile Location (NW): \nLatitude: " + latitude
+ "\nLongitude: " + longitude,
Toast.LENGTH_LONG).show();
} else {
showSettingsAlert("NETWORK");
}
}
});
Do it via Location Manager.
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

device location is always null

I have gone through many posts on SO regarding this issue:
Tried everything in Here,
Here and Here Nothing works. Everytime location is null. On device and on emulator :(. My GPS is on,internet is on and my manifest has following permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
I use following code to get the location i.e the longitude and latitude of the device...however in every case I get location as null
public void find_Location() {
Log.d("Find Location", "in find_location");
String location_context = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getApplicationContext()
.getSystemService(location_context);
List<String> providers = locationManager.getProviders(true);
for (String provider : providers) {
locationManager.requestLocationUpdates(provider, 1000, 0,
new LocationListener() {
public void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider,
int status, Bundle extras) {
}
});
Location location = locationManager.getLastKnownLocation(provider);
if (location == null) {
Toast.makeText(LbsGeocodingActivity.this, "Location Not found",
Toast.LENGTH_LONG).show();
} else {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Toast.makeText(LbsGeocodingActivity.this,
"LAT..." + latitude + "\nLONG..." + longitude,
Toast.LENGTH_LONG).show();
// addr=ConvertPointToLocation(latitude,longitude);
// String temp_c=SendToUrl(addr);
}
}
}
It takes some time before the position is determined, and it can change over time. It's probably just not available yet when you check it.
When a position fix becomes available, you will be notified in onLocationChanged.
How could I be so stupid..My bad....In settings Use Wireless networks was not selected!....That cause all the problems !!
Now I am able to get a location.
It took me 16 hrs to get to this...my productivity going down for sure! :(

Receive GPS Information in Android?

I used to receive the GPS information of the code is as follows. The app error is a real phone. I think the problem of the application have to wait for Gps to receive it. How can I do this?
public class Main extends Activity {
TextView text;
Location currentLocation;
double latitude;
double longitude;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView)findViewById(R.id.text);
LocationManager locationManager =
(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateLocation(location);
}
public void onStatusChanged(
String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 1000, 1, locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
latitude=location.getLatitude();
longitude=location.getLongitude();
addressText.setText("Latitude: "+latitude+" \n"+"Longitude: "+longitude);
void updateLocation(Location location){
currentLocation = location;
latitude = currentLocation.getLatitude();
longitude = currentLocation.getLongitude();
}
}
I believe the official guide explains this quite well. Please take a look there: http://developer.android.com/guide/topics/location/obtaining-user-location.html
This will return null if there no last know location for the GPS.
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
You need to wait until the GSP has got a location fix for it to give you a location by calling onLocationChanged. You can add a GPS status listener to find out when this occurs.
In onCreate add:
locationManager.addGpsStatusListener(locationListener);
Add this method to locationListener:
#Override
public void onGpsStatusChanged(int event) {
switch (event) {
case GpsStatus.GPS_EVENT_FIRST_FIX:
Log.d("GPSStatus", "GPS First Fix");
break;
case GpsStatus.GPS_EVENT_STARTED:
Log.d("GPSStatus", "GPS Started");
break;
case GpsStatus.GPS_EVENT_STOPPED:
Log.d("GPSStatus", "GPS Stopped");
break;
}
}
You should also check if GPS is enabled before requesting location updates.
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))

Categories

Resources