Find my location don't work (android 2.2) - android

I tried to implement the code found in this link location find
The truth is I want to get my location using network or GPS, ( I tested it on Network)
private Location getCurrentLocation(){
// 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.
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
mapController.animateTo(myGeoPoint);
}
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);
String locationProvider = LocationManager.NETWORK_PROVIDER;
// Or use LocationManager.GPS_PROVIDER
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
if(lastKnownLocation==null){
locationProvider = LocationManager.GPS_PROVIDER;
// Or use LocationManager.GPS_PROVIDER
lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
}
return lastKnownLocation;
}
How to return the result:
myLocation = getCurrentLocation();
if(myLocation != null)
{
mLatitude = myLocation.getLatitude();
mLongitude = myLocation.getLongitude();
myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
mapController.animateTo(myGeoPoint);
}else {
mLatitude = 36.859502;
mLongitude = 10.168097;
myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
mapController.animateTo(myGeoPoint);}
This wasn't the only code i tried and i dont found any result on a android 2.2 phone.
Any idea about how can i fix it
i check most of the tutorials on the net!!

First check whether did you add the following permissions or not.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
The second thing is that you should give some time to locate location info.You can do that using timer task or posDelayed.
private Location getCurrentLocation(){
// 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.
t.cancel();
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
mapController.animateTo(myGeoPoint);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Location lastKnownLocation;
Timer t = new Timer();
t.schedule(new TimerTask() {
#Override
public void run() {
this.cancel();
lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
if(lastKnownLocation==null){
locationProvider = LocationManager.GPS_PROVIDER;
// Or use LocationManager.GPS_PROVIDER
lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
return lastKnownLocation;
}
},30000);
}
it will give you results after 30seconds.

After a long research, I found my pleasur Here :)
Hope this will help someone.

Related

How to get the new latitude and longitude cordinates from android

How i am getting location details:
AppLocationService appLocationService = new AppLocationService(getApplicationContext());
Location nwLocation= appLocationService.getLocation(LocationManager.NETWORK_PROVIDER);
nwLocation.getLatitude();
nwLocation.getLongitude();
What is happening since my code has llast known location .... its giving me the last location that was updated say now its evening. its giving me the location updated during morning
What i want:: how can i make a fresh network request to get the current location at my position
AppLocationService.java
public class AppLocationService extends Service implements LocationListener {
protected LocationManager locationManager;
Location location;
private static final long MIN_DISTANCE_FOR_UPDATE = 0;
private static final long MIN_TIME_FOR_UPDATE = 0;
public AppLocationService(Context context) {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
}
public Location getLocation(String provider) {
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider,
MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider);
return location;
}
}
return null;
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
Your code is registering to receive location updates and also returns the last known location. What happens is expected because you didn't wait to receive an updated one.
Once the method "onLocationChanged" was called by the system you'll have the new one. You can then send a broadcast from the service.
You are trying to get the current location in a synchronous mode which is not possible as it takes some time to lookup the updated one.
The primary reason why you aren't getting updated location information quickly is that you're relying on the NETWORK_PROVIDER.
You should instead use this
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
How to call Explicitly onLocationChanged
public void locationlist()
{
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER,
50, 0, this);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {}
if(location == null)
{
}
else{
lat = (double) (location.getLatitude());
lng = (double) (location.getLongitude());
}
}
now call the method locationlist where you want like in a timer.
Use onLocationChanged method and get the longitude and latitude from the parametr location
Code:
#Override
public void onLocationChanged(Location location) {
mCurrentLocation = new Location(location);
latitude=mCurrentLocation .getLatitude();
longitude=mCurrentLocation .getLongitude();
}

Get current latitude and longitude in the string

I am trying to get latitude and longitude but sometime network is available but I am not getting value of latitude and longitude. I am using MyLocationListener class and put condition all but some time value is not getting.
protected void showCurrentLocation()
{
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
counter++;
latitude=location.getLatitude();
longitude=location.getLongitude();
altitude=location.getAltitude();
}
}
private class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location location)
{
counter++;
latitude=location.getLatitude();
longitude=location.getLongitude();
altitude=location.getAltitude();
}
#Override
public void onStatusChanged(String s, int i, Bundle b)
{
}
#Override
public void onProviderDisabled(String s)
{
}
#Override
public void onProviderEnabled(String s)
{
}
}
Here Best way to get Longitude Latitude:
/** PROCESS for Get Longitude and Latitude **/
locationManager = (LocationManager) getSystemService(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.
longitude = String.valueOf(location.getLongitude());
latitude = String.valueOf(location.getLatitude());
Log.d("msg", "changed Loc : "+longitude + ":"+latitude);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// check if GPS enabled
if(isGPSEnabled){
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
longitude = String.valueOf(location.getLongitude());
latitude = String.valueOf(location.getLatitude());
Log.d("msg", "Loc : "+longitude + ":"+latitude);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}else
{
/*
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(true);
String provider = locationManager.getBestProvider(criteria, true);
*/
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null)
{
longitude = String.valueOf(location.getLongitude());
latitude = String.valueOf(location.getLatitude());
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}else
{
longitude = "0.00";
latitude = "0.00";
}
}
}
if device is not able to get currentLocation using GPS then it'll be take fron NetworkProvider or else it'll take 0,0 as my requirement but you can modify as per your requirement
May it'll helpful to you.
Happy Coding :D
See, this is a very common problem. In order to have exact Latitude and Longitude ,You must use GPS also it must me activated on the mobile device but GPS too have some limitations. GPS works most efficiently under open sky. you will not have a clear range inside a building. So try your code under open sky and check the response.

Android: how to find total distance covered using GPS when continuously moving?

This is my code. Please tell me y it is not able to calculate the distance.. In this code res is a long variable which is supposed to store the total distance covered. This code is supposed to calculate distance based on GPS as soon as there is a change in the latitude and longitude..
String serviceString = Context.LOCATION_SERVICE;
LocationManager locationManager;
locationManager= (LocationManager)getSystemService(serviceString);
String provider = LocationManager.GPS_PROVIDER;
final Location loc1=locationManager.getLastKnownLocation(provider);
//Location loc1=new Location("");
String netprovider=LocationManager.NETWORK_PROVIDER;
lat1=loc1.getLatitude();
lon1=loc1.getLongitude();
LocationListener myLocationListener = new LocationListener()
{
public void onLocationChanged(Location loc1)
{
Location loc2=new Location("");
lat2=loc2.getLatitude();
lon2=loc2.getLongitude();
dtvalue.setText(lat1+","+lon1+","+lat2+","+lon2);
Location.distanceBetween(lat1,lon1,lat2,lon2,dist);
res=res+(long)dist[0];
lat1=lat2;
lon1=lon2;
}
public void onProviderDisabled(String provider)
{
// Update application if provider disabled.
}
public void onProviderEnabled(String provider)
{
// Update application if provider enabled.
}
public void onStatusChanged(String provider, int status,
Bundle extras)
{
// Update application if provider hardware status changed.
}
};
locationManager.requestLocationUpdates(provider, 5000, 1, myLocationListener);
locationManager.requestLocationUpdates(netprovider, 5000, 1, myLocationListener);
The problem that you are defining an empty location Location loc2=new Location(""); and then using it.
You can define lat1 and lon1 in your class.
public void onLocationChanged(Location loc1)
{
if(lat1 != 0 && long1 != 0) {
Location.distanceBetween(lat1,lon1,loc1.getLatitude(),loc1.getLongitude(),dist);
res+=(long)dist[0];
}
lat1=loc1.getLatitude();
lon1=loc1.getLongitude();
}

How call requestLocationUpdates in thread?

I want get my location from a thread.
I code as:
LocationManager locationManager;
GeoPoint p;
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria cri = new Criteria();
String tower = locationManager.getBestProvider(cri, false);
Location location = locationManager.getLastKnownLocation(tower);
But How call requestLocationUpdates update location?
Can you help me?
You have everything on the developer documentation: Requesting Location Updates.
// 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);

Unable to get latitude and longitude from android

getLastKnownLocation always return null value. I have enabled geo fix and gps on emulator. However, there is no gps signal on the notification bar when running this on emulator. May I know is there any mistake in my code? Thanks.
public void onCreate(Bundle savedInstanceState) {
LocationManager location = null;
super.onCreate(savedInstanceState);
setContentView(R.layout.near_layout);
location = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
String bestProvider = location.getBestProvider(criteria, true);
LocationListener ll = new mylocationlistener();
Location loc = location.getLastKnownLocation(bestProvider);
if(loc != null)
{
double latitude = loc.getLatitude();
double longitude = loc.getLongitude();
Toast.makeText(nearPlace.this, " nice" + latitude, Toast.LENGTH_LONG).show();
}else{
Toast.makeText(nearPlace.this, "Location not available. GPS is not enabled.", Toast.LENGTH_LONG).show();
}
location.requestLocationUpdates(bestProvider, 0, 0, ll);
}
private class mylocationlistener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
Toast.makeText(nearPlace.this, " nice" + location.getLatitude(), Toast.LENGTH_LONG).show();
nearPlaceDownloaderTask getNearPlace = new nearPlaceDownloaderTask();
getNearPlace.execute(ANDROID_WEB_LINK + "gt.php?la=" + location.getLatitude() + "&lo=" + location.getLongitude());
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
The reason your code is not working, is because you have specified the following criteria:
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
Android will prefer to use network location over GPS, because you don't care about the accuracy. By specifying an accuracy of 20 meters or so, it will probably automatically invoke GPS.
To manually invoke GPS location updates, override bestProvider:
bestProvider = LocationManager.GPS_PROVIDER;
You can really simplify this into two lines of code:
LocationManager locationManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, interval,
precision, listener);
Don't forget to unregister your listener:
locationManager.removeUpdates(listener);
You might also want to check this out geofix not working
Also make sure you have mock locations allowed set in the emulator. Take a look at Pauls answer as well, which is why I was asking in comments what provider was returned

Categories

Resources