I'm trying to get the user's Location by showing that he is in which area with Particular location in google maps with the help of Android API while surfing in internet I came to know that getLocationManger() is used to view the Location but it shows only by Latitude,Longitude.
If someone have any idea about this please help me friends.
You can get the user's current location in terms of latitude and longitude and simply make an Implicit Intent to launch map with these values.
To get current location:
LocationManager locationManager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
//Toast.makeText(getApplicationContext(),"Entered onLocationChanged",Toast.LENGTH_SHORT).show();
Log.d("LIFECYCLE","Entered onLocationChanged");
lat = location.getLatitude();
lon = location.getLongitude();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
To show on google map:
Intent intent = null,chooser = null;
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("geo:"+lat+","+lon));
chooser = Intent.createChooser(intent,"Launch Maps");
startActivity(chooser);
/* creating chooser because:
If this is not done, your app may crash when run on the emulator because the emulator may not have an activity installed which can handle the intent.*/
Try this one
LocationManager lm = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Location net_loc = null;
lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
you can get the current location from net_loc.
Related
I don't want to use service which is continuously running in background ! Actually in my project user will send SMS command from any smartphone to his/her misplaced smartphone.My app will detect that particular "SMS command" and in return it will send the current location of misplaced mobile.
can it be done through intent service ? I m damn confused ... Its single time operation how to perform it efficiently ... ?
by using NETWORK_PROVIDER getting geolocation
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);
and add this permission in manifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
How can I get continuous geo-location of android device programmatically ?
I'm having troubles of getting position coordinates continuously of android device and show the navigation move according to the geolocation.
According to Android documentation :
// 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) {
// 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);
More information available on their page : http://developer.android.com/guide/topics/location/strategies.html
In Android if LocationServices is disabled, is there any way that I can get the current location through wifi/network. I know there is one way of showing the user a popup to change his LocationSettings , but I don't want to show the popup.
So is there any way of getting the current location even if LocationServices is disabled ?
use LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER
The entire code and how to use is given here :
http://developer.android.com/guide/topics/location/strategies.html
// 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) {
// 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);
Yes of course, you can use:
// let Android select the right location provider for you
String myProvider = locationManager.getBestProvider(myCriteria, true);
check this for more details good-way-of-getting-the-users-location-in-android and this Location Strategies
I am getting longitude=0.0 and latitude=0.0 on tablet while this is working perfectly on the phone.
I am using LocationManager.NETWORK_PROVIDER and not the GPS_PROVIDER so what could be the cause please?
Logcat output??
that's my code:
// Acquire a reference to the system Location Manager
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
longitude=location.getLongitude();
latitude=location.getLatitude();
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
//Or use LocationManager.GPS_PROVIDER
String locationProvider = LocationManager.NETWORK_PROVIDER;
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
if(lastKnownLocation!=null){
longitude=lastKnownLocation.getLongitude();
latitude=lastKnownLocation.getLatitude();
}
Check out Location settings inside your device settings. This seems not a problem with your code, but your device settings. In my Ginger bread, it's as:
Settings -> Location&security -> Use wireless networks
Hai i develop a application for getting the Gps value its woking fine.But i faced some problem
problem
1.when mobile is screen locked
2.when mobile is swtched off into switch on
3.when mobile display light is off
MyRequirements
Anytime i want to get the gps value ,except the mobile is switched of
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, true);
networkLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (networkLoc != null) {
double l = (double) (networkLoc.getLatitude());
double lng11 = (double) (networkLoc.getLongitude());
latituteField1.setText(Double.toString(l));
longitudeField1.setText(Double.toString(lng11));
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
#Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
this
);
}
#Override
protected void onPause() {
super.onPause();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
this
);
}
#Override
public void onLocationChanged(Location location) {
lat1 = (double) (networkLoc.getLatitude());
lng1 = (double) (networkLoc.getLongitude());
latituteField1.setText(Double.toString(lat1));
longitudeField1.setText(Double.toString(lng1));
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disenabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
Usually the device powers down the screen after some time and than also the CPU. If this happens you will not get any more updates from the GPS. To prevent this behavior you need to aquire a WakeLock.
Wakelocks are described here: http://developer.android.com/reference/android/os/PowerManager.html
And here is another article on WakeLocks: How to get an Android WakeLock to work?
You can use TimerTask Class and Android's Alarm Class. In the TimerTask's Run method keep fetching gps data on particular interval ( suppose on every minute ). And use Alarm Class for closing LocationListener Class & Opening on every hour. This way your application will work very efficiently. I used same for my application. For Auto start on Switch on Mobile, you can use Android Services.
TimerTask's Example is here.
AlarmManager's Example is here.