Pass Latitude and Longitude to Google API Places Search in Android - java

I have literally been searching for this for weeks. I am a novice java programmer but I have been able to piece together an app that can use a double latitude and longitude hard coded in the same class. It will show a list of current places surrounding those points. I have another separate class with a method that is able to get the current location based on the gps/network but I can't pass the variables created from this second class to the PlaceRequest class. I have looked through all of the tutorials on the above subjects but there isn't anything combining current location and place search results. I have two getters declared but can't call the variables in these. Again sort of a rookie so may be an easy fix. Any ideas?
Update - Here is my code so far:
GooglePlaceActivity.java
public class GooglePlaceActivity extends Activity {
/** Called when the activity is first created. */
Button btn1;
TextView txt1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
btn1 = (Button)findViewById(R.id.button1);
txt1 = (TextView)findViewById(R.id.textView1);
btn1.setOnClickListener(l);
}
private class SearchSrv extends AsyncTask<Void, Void, PlacesList>{
#Override
protected PlacesList doInBackground(Void... params) {
PlacesList pl = null;
try {
pl = new PlaceRequest().performSearch();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pl;
}
#Override
protected void onPostExecute(PlacesList result) {
String text = "Result \n";
if (result!=null){
for(Place place: result.results){
text = text + place.name +"\n";
}
txt1.setText(text);
}
setProgressBarIndeterminateVisibility(false);
}
}
View.OnClickListener l = new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
SearchSrv srv = new SearchSrv();
setProgressBarIndeterminateVisibility(true);
srv.execute();
}
};
}
//////////////////////
PlaceRequest.java
public class PlaceRequest {
private static final HttpTransport transport = new ApacheHttpTransport();
private static final String API_KEY = "keyhere";
private static final String LOG_KEY = "GGPlace";
// The different Places API endpoints.
private static final String PLACES_SEARCH_URL = "https://maps.googleapis.com/maps/api/place/search/json?";
private static final String PLACES_AUTOCOMPLETE_URL = "https://maps.googleapis.com/maps/api/place/autocomplete/json?";
private static final String PLACES_DETAILS_URL = "https://maps.googleapis.com/maps/api/place/details/json?";
private static final boolean PRINT_AS_STRING = true;
//double latitude;
//double longitude;
CurrentLocation clo = new CurrentLocation(null);
//clo.onLocationChanged(latitude);
//double longitude = CurrentLocation.getLongitude();
//double latitude = CurrentLocation.getLatitude();
double longi = clo.getLongitude();
double lat = clo.getLatitude();
public PlacesList performSearch() throws Exception {
try {
//CurrentLocation currlo = new CurrentLocation();
//double lat = currlo.getLatitude();
//double longi = currlo.getLongitude();
Log.v(LOG_KEY, "Start Search");
GenericUrl reqUrl = new GenericUrl(PLACES_SEARCH_URL);
reqUrl.put("key", API_KEY);
//reqUrl.put("location", latitude + "," + longitude);
//reqUrl.put("location", getLatitude(latitude) + "," + getLongitude());
reqUrl.put("location", lat + "," + longi);
reqUrl.put("radius", 1600);
reqUrl.put("types", "food");
reqUrl.put("sensor", "false");
Log.v(LOG_KEY, "url= " + reqUrl);
HttpRequestFactory httpRequestFactory = createRequestFactory(transport);
HttpRequest request = httpRequestFactory.buildGetRequest(reqUrl);
Log.v(LOG_KEY, request.execute().parseAsString());
PlacesList places = request.execute().parseAs(PlacesList.class);
Log.v(LOG_KEY, "STATUS = " + places.status);
for (Place place : places.results) {
Log.v(LOG_KEY, place.name);
}
return places;
} catch (HttpResponseException e) {
Log.v(LOG_KEY, e.getResponse().parseAsString());
throw e;
}
catch (IOException e) {
// TODO: handle exception
throw e;
}
}
public static HttpRequestFactory createRequestFactory(final HttpTransport transport) {
return transport.createRequestFactory(new HttpRequestInitializer() {
public void initialize(HttpRequest request) {
GoogleHeaders headers = new GoogleHeaders();
headers.setApplicationName("Google-Places-DemoApp");
request.setHeaders(headers);
JsonHttpParser parser = new JsonHttpParser(new JacksonFactory()) ;
//JsonHttpParser.builder(new JacksonFactory());
//parser.jsonFactory = new JacksonFactory();
request.addParser(parser);
}
});
}
}
/////////////
CurrentLocation.java
public class CurrentLocation {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
LocationManager locationManager ;
double latitude=0;
double longitude=0;
public CurrentLocation(Context ctxt) {
super();
locationManager = (LocationManager) ctxt.getSystemService(Context.LOCATION_SERVICE);
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onProviderDisabled(String provider) {}
#Override
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
});
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
}

Edit: After looking your complete code, I see a few fundamental design flaws so I'm going to show you how I did it and you can adapt it to your program flow. Please keep in mind that this example is vastly simplified from my original, but it should be enough to get you going.
First, the CurrentLocation.java file. My design decision for wrapping this in a Future was so that I can re-use it in multiple activities with the added bonus of killing it when necessary.
public class CurrentLocation implements Callable<Location> {
private static final String TAG = "CurrentLocation";
private Context context;
private LocationManager lm;
private Criteria criteria;
private Location bestResult;
private boolean locationListenerWorking;
public CurrentLocation(Context context) {
lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
this.context = context;
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
bestResult = null;
locationListenerWorking = false;
}
public Location call() {
return getLoc();
}
private Location getLoc() {
String provider = lm.getBestProvider(criteria, true);
if (provider != null) {
Log.d(TAG, "Using provider: " +provider);
locationListenerWorking = true;
lm.requestLocationUpdates(provider,
0,
0,
singeUpdateListener,
context.getMainLooper());
} else {
Log.w(TAG, "Couldn't find a location provider");
return null;
}
while (locationListenerWorking) {
// Check for the interrupt signal - terminate if necessary
if (Thread.currentThread().isInterrupted()) {
Log.i(TAG, "User initiated interrupt (cancel signal)");
cleanup();
break;
}
try {
// ghetto implementation of a busy wait...
Thread.sleep(500); // Sleep for half a second
} catch (Exception e) {
Log.d(TAG, "Thread interrupted..");
cleanup();
break;
}
}
return bestResult;
}
private void cleanup() {
if (lm != null) {
Log.d(TAG, "Location manager not null - cleaning up");
lm.removeUpdates(singeUpdateListener);
} else {
Log.d(TAG, "Location manager was NULL - no cleanup necessary");
}
}
/**
* This one-off {#link LocationListener} simply listens for a single location
* update before unregistering itself. The one-off location update is
* returned via the {#link LocationListener} specified in {#link
* setChangedLocationListener}.
*/
private LocationListener singeUpdateListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.d(TAG, "Got a location update");
if (location == null) {
Log.d(TAG, "Seems as if we got a null location");
} else {
bestResult = location;
}
cleanup();
locationListenerWorking = false;
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
}
Then in your calling class (i.e. where you need the lat/lon coordinates - you want to do this from an Activity):
private class GetLocationTask extends AsyncTask <Void, Void, Location> {
private Future<Location> future;
private ExecutorService executor = new ScheduledThreadPoolExecutor(5);
private boolean cancelTriggered = false;
protected void onPreExecute() {
Log.d(TAG, "Starting location get...");
}
public Location doInBackground(Void... arg) {
CurrentLocation currLoc = new CurrentLocation(getApplicationContext());
future = executor.submit(currLoc);
long LOCATION_TIMEOUT = 20000; // ms = 20 sec
try {
// return future.get(Constants.LOCATION_TIMEOUT, TimeUnit.MILLISECONDS);
return future.get(LOCATION_TIMEOUT, TimeUnit.MILLISECONDS);
} catch (Exception e) {
Log.w(TAG, "Location get timed out");
future.cancel(true);
return null;
}
}
public boolean killTask() {
cancelTriggered = true;
boolean futureCancelRes = future.cancel(true);
this.cancel(true);
Log.d(TAG, "Result of cancelling task: " +futureCancelRes);
return futureCancelRes;
}
protected void onPostExecute(Location res) {
if (cancelTriggered) {
Log.d(TAG, "User initiated cancel - this is okay");
cancelTriggered = false;
} else if (res == null) {
Log.d(TAG, "Could not get a location result");
} else {
double lat = res.getLatitude();
double lon = res.getLongitude();
Log.d(TAG, "Latitude: " +lat);
Log.d(TAG, "Longitude: " +lon);
}
}
}
Finally to wrap things up, here's how you call it:
GetLocationTask t = new GetLocationTask();
t.execute();
And if you need to kill the location update for whatever reason (if your user switches out of your activity, etc), this will kill the AsyncTask as well as the associated Future task.
t.killTask();
P.S. You may want to get your API keys changed and edit it out of your post.

Related

How to call inbuilt method of location listener in another class

I am using location listener to track the location.I implement the location listener, there are inbuilt methods of location listener such as onLocationChanged,OnstatusChanged,onProviderEnabled etc.Whenerver the location changed it will hit on server but I want to do at every 10 min of interval So I am using timer for that after every 10 mins it will hit the location. I cannot call the method inside the timer. So how to do it.
public class LocationMonitoringService implements LocationListener, GpsStatus.Listener {
private static final String TAG = LocationMonitoringService.class.getSimpleName();
private final Context mContext;
private final LocationRepository mLocationRepository;
private final ShareLocationAdapterClass mAdapter;
private final SyncOfflineRepository syncOfflineRepository;
private long updatedTime = 0;
private final List<UserLocationPojo> mUserLocationPojoList;
private final SyncOfflineAttendanceRepository syncOfflineAttendanceRepository;
private Handler mHandler = new Handler();
private Timer mTimer = null;
long notify_interval = 1000 * 60 * 5;
public LocationMonitoringService(final Context context) {
mContext = context;
mLocationRepository = new LocationRepository(AUtils.mainApplicationConstant.getApplicationContext());
syncOfflineRepository = new SyncOfflineRepository(AUtils.mainApplicationConstant.getApplicationContext());
syncOfflineAttendanceRepository = new SyncOfflineAttendanceRepository(AUtils.mainApplicationConstant.getApplicationContext());
mUserLocationPojoList = new ArrayList<>();
mAdapter = new ShareLocationAdapterClass();
mAdapter.setShareLocationListener(new ShareLocationAdapterClass.ShareLocationListener() {
#Override
public void onSuccessCallBack(boolean isAttendanceOff) {
if (isAttendanceOff && !syncOfflineAttendanceRepository.checkIsAttendanceIn()) {
AUtils.setIsOnduty(false);
((MyApplication) AUtils.mainApplicationConstant).stopLocationTracking();
}
}
#Override
public void onFailureCallBack() {
}
});
}
public void onStartTacking() {
LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
//Exception thrown when GPS or Network provider were not available on the user's device.
try {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setAltitudeRequired(false);
criteria.setSpeedRequired(true);
criteria.setCostAllowed(false);
criteria.setBearingRequired(false);
//API level 9 and up
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
int gpsFreqInDistance =100;
assert locationManager != null;
locationManager.addGpsStatusListener(this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, AUtils.LOCATION_INTERVAL,
gpsFreqInDistance, this,null);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, AUtils.LOCATION_INTERVAL,
gpsFreqInDistance, this,null);
} catch (IllegalArgumentException | SecurityException e) {
Log.e(TAG, Objects.requireNonNull(e.getLocalizedMessage()));
Log.d(TAG, "onStartTacking: " + e.getMessage());
Log.d(TAG, "onStartTacking: " + e.getLocalizedMessage());
e.printStackTrace();
} catch (RuntimeException e) {
Log.e(TAG, Objects.requireNonNull(e.getLocalizedMessage()));
Log.d(TAG, "onStartTacking: " + e.getMessage());
e.printStackTrace();
}
}
public void onStopTracking(Context context) {
mAdapter.shareLocation();
LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(this);
// cancelAlarm(context, (AlarmManager)context.getSystemService(Context.ALARM_SERVICE));
}
/*
* LOCATION CALLBACKS
*/
//to get the location change
#Override
public void onLocationChanged(Location location) {
Log.d("okh ", "onLocationChanged: "+System.currentTimeMillis());
if (location != null) {
Log.d(TAG, String.valueOf(location.getAccuracy()));
if (!AUtils.isNullString(String.valueOf(location.getLatitude())) && !AUtils.isNullString(String.valueOf(location.getLongitude()))) {
Prefs.putString(AUtils.LAT, String.valueOf(location.getLatitude()));
Prefs.putString(AUtils.LONG, String.valueOf(location.getLongitude()));
if (Prefs.getBoolean(AUtils.PREFS.IS_ON_DUTY, false)) {
if (updatedTime == 0) {
updatedTime = System.currentTimeMillis();
Log.d(TAG, "updated Time ==== " + updatedTime);
}
if ((updatedTime + AUtils.LOCATION_INTERVAL_MINUTES) <= System.currentTimeMillis()) {
updatedTime = System.currentTimeMillis();
Log.d(TAG, "updated Time ==== " + updatedTime);
}
mTimer = new Timer();
mTimer.schedule(new TimerTaskToGetLocation(), 5,notify_interval);
// sendLocation();
}
}
} else {
Log.d(TAG, "onLocationChanged: no location found !!");
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, "onStatusChanged" + provider + "Status" + status);
}
#Override
public void onProviderEnabled(String provider) {
Log.d(TAG, " onProviderEnabled" + provider);
}
#Override
public void onProviderDisabled(String provider) {
Log.d(TAG, " onProviderDisabled" + provider);
}
#Override
public void onGpsStatusChanged(int event) {
}
private void sendLocation() {
// mAdapter.shareLocation(getTempList());
Log.d("okh", "sendLocation: Current Time In Millies "+ System.currentTimeMillis());
try {
Calendar CurrentTime = AUtils.getCurrentTime();
Calendar DutyOffTime = AUtils.getDutyEndTime();
if (CurrentTime.before(DutyOffTime)) {
Log.i(TAG, "Before");
UserLocationPojo userLocationPojo = new UserLocationPojo();
userLocationPojo.setUserId(Prefs.getString(AUtils.PREFS.USER_ID, ""));
userLocationPojo.setLat(Prefs.getString(AUtils.LAT, ""));
userLocationPojo.setLong(Prefs.getString(AUtils.LONG, ""));
double startLat = Double.parseDouble(Prefs.getString(AUtils.LAT, "0"));
double startLng = Double.parseDouble(Prefs.getString(AUtils.LONG, "0"));
userLocationPojo.setDistance(String.valueOf(AUtils.calculateDistance(
AUtils.mainApplicationConstant.getApplicationContext(), startLat, startLng)));
// userLocationPojo.setDatetime(AUtils.getServerDateTime()); //TODO
userLocationPojo.setDatetime(AUtils.getServerDateTimeLocal());
userLocationPojo.setOfflineId("0");
userLocationPojo.setIsOffline(AUtils.isInternetAvailable() && AUtils.isConnectedFast(mContext));
String UserTypeId = Prefs.getString(AUtils.PREFS.USER_TYPE_ID, AUtils.USER_TYPE.USER_TYPE_GHANTA_GADI);
if (AUtils.isInternetAvailable()) {
TableDataCountPojo.LocationCollectionCount count = syncOfflineRepository.getLocationCollectionCount(AUtils.getLocalDate());
if ((UserTypeId.equals(AUtils.USER_TYPE.USER_TYPE_GHANTA_GADI) || UserTypeId.equals(AUtils.USER_TYPE.USER_TYPE_WASTE_MANAGER))
&& (count.getLocationCount() > 0 || count.getCollectionCount() > 0)) {
syncOfflineRepository.insetUserLocation(userLocationPojo);
} else {
mUserLocationPojoList.add(userLocationPojo);
mAdapter.shareLocation(mUserLocationPojoList);
mUserLocationPojoList.clear();
}
} else {
if (UserTypeId.equals(AUtils.USER_TYPE.USER_TYPE_EMP_SCANNIFY)) {
Type type = new TypeToken<UserLocationPojo>() {
}.getType();
mLocationRepository.insertUserLocationEntity(new Gson().toJson(userLocationPojo, type));
} else {
syncOfflineRepository.insetUserLocation(userLocationPojo);
}
mUserLocationPojoList.clear();
}
}
else {
Log.i(TAG, "After");
syncOfflineAttendanceRepository.performCollectionInsert(mContext,
syncOfflineAttendanceRepository.checkAttendance(), AUtils.getCurrentDateDutyOffTime());
AUtils.setIsOnduty(false);
((MyApplication) AUtils.mainApplicationConstant).stopLocationTracking();
Activity activity = ((Activity) AUtils.currentContextConstant);
if (activity instanceof DashboardActivity) {
((Activity) AUtils.currentContextConstant).recreate();
AUtils.DutyOffFromService = true;
}
if (!AUtils.isNull(AUtils.currentContextConstant)) {
((Activity) AUtils.currentContextConstant).recreate();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private class TimerTaskToGetLocation extends TimerTask {
#Override
public void run() {
sendLocation();
}
}
}
Here is the code please try this:
public class LocationMonitoringService implements LocationListener, GpsStatus.Listener {
private static final String TAG = LocationMonitoringService.class.getSimpleName();
private final Context mContext;
private final LocationRepository mLocationRepository;
private final ShareLocationAdapterClass mAdapter;
private final SyncOfflineRepository syncOfflineRepository;
private long updatedTime = 0;
private int gpsFreqInDistance =100;
private LocationManager locationManager;
private final List<UserLocationPojo> mUserLocationPojoList;
private final SyncOfflineAttendanceRepository syncOfflineAttendanceRepository;
private Handler mHandler = new Handler();
private Timer mTimer = null;
long notify_interval = 1000 * 60 * 5;
public LocationMonitoringService(final Context context) {
mContext = context;
// here we declare the variable as a field for this class instance
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
mLocationRepository = new LocationRepository(AUtils.mainApplicationConstant.getApplicationContext());
syncOfflineRepository = new SyncOfflineRepository(AUtils.mainApplicationConstant.getApplicationContext());
syncOfflineAttendanceRepository = new SyncOfflineAttendanceRepository(AUtils.mainApplicationConstant.getApplicationContext());
mUserLocationPojoList = new ArrayList<>();
mAdapter = new ShareLocationAdapterClass();
mAdapter.setShareLocationListener(new ShareLocationAdapterClass.ShareLocationListener() {
#Override
public void onSuccessCallBack(boolean isAttendanceOff) {
if (isAttendanceOff && !syncOfflineAttendanceRepository.checkIsAttendanceIn()) {
AUtils.setIsOnduty(false);
((MyApplication) AUtils.mainApplicationConstant).stopLocationTracking();
}
}
#Override
public void onFailureCallBack() {
}
});
}
public void onStartTacking() {
//Exception thrown when GPS or Network provider were not available on the user's device.
try {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setAltitudeRequired(false);
criteria.setSpeedRequired(true);
criteria.setCostAllowed(false);
criteria.setBearingRequired(false);
//API level 9 and up
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
assert locationManager != null;
locationManager.addGpsStatusListener(this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, AUtils.LOCATION_INTERVAL,
gpsFreqInDistance, this,null);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, AUtils.LOCATION_INTERVAL,
gpsFreqInDistance, this,null);
} catch (IllegalArgumentException | SecurityException e) {
Log.e(TAG, Objects.requireNonNull(e.getLocalizedMessage()));
Log.d(TAG, "onStartTacking: " + e.getMessage());
Log.d(TAG, "onStartTacking: " + e.getLocalizedMessage());
e.printStackTrace();
} catch (RuntimeException e) {
Log.e(TAG, Objects.requireNonNull(e.getLocalizedMessage()));
Log.d(TAG, "onStartTacking: " + e.getMessage());
e.printStackTrace();
}
}
public void onStopTracking(Context context) {
mAdapter.shareLocation();
// removed initialization from here
locationManager.removeUpdates(this);
// cancelAlarm(context, (AlarmManager)context.getSystemService(Context.ALARM_SERVICE));
}
/*
* LOCATION CALLBACKS
*/
//to get the location change
#Override
public void onLocationChanged(Location location) {
Log.d("okh ", "onLocationChanged: "+System.currentTimeMillis());
if (location != null) {
Log.d(TAG, String.valueOf(location.getAccuracy()));
if (!AUtils.isNullString(String.valueOf(location.getLatitude())) && !AUtils.isNullString(String.valueOf(location.getLongitude()))) {
Prefs.putString(AUtils.LAT, String.valueOf(location.getLatitude()));
Prefs.putString(AUtils.LONG, String.valueOf(location.getLongitude()));
if (Prefs.getBoolean(AUtils.PREFS.IS_ON_DUTY, false)) {
if (updatedTime == 0) {
updatedTime = System.currentTimeMillis();
Log.d(TAG, "updated Time ==== " + updatedTime);
}
if ((updatedTime + AUtils.LOCATION_INTERVAL_MINUTES) <= System.currentTimeMillis()) {
updatedTime = System.currentTimeMillis();
Log.d(TAG, "updated Time ==== " + updatedTime);
}
mTimer = new Timer();
mTimer.schedule(new TimerTaskToGetLocation(), 5,notify_interval);
// sendLocation();
}
}
} else {
Log.d(TAG, "onLocationChanged: no location found !!");
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, "onStatusChanged" + provider + "Status" + status);
}
#Override
public void onProviderEnabled(String provider) {
Log.d(TAG, " onProviderEnabled" + provider);
}
#Override
public void onProviderDisabled(String provider) {
Log.d(TAG, " onProviderDisabled" + provider);
}
#Override
public void onGpsStatusChanged(int event) {
}
private void sendLocation() {
// mAdapter.shareLocation(getTempList());
Log.d("okh", "sendLocation: Current Time In Millies "+ System.currentTimeMillis());
try {
Calendar CurrentTime = AUtils.getCurrentTime();
Calendar DutyOffTime = AUtils.getDutyEndTime();
if (CurrentTime.before(DutyOffTime)) {
Log.i(TAG, "Before");
UserLocationPojo userLocationPojo = new UserLocationPojo();
userLocationPojo.setUserId(Prefs.getString(AUtils.PREFS.USER_ID, ""));
userLocationPojo.setLat(Prefs.getString(AUtils.LAT, ""));
userLocationPojo.setLong(Prefs.getString(AUtils.LONG, ""));
double startLat = Double.parseDouble(Prefs.getString(AUtils.LAT, "0"));
double startLng = Double.parseDouble(Prefs.getString(AUtils.LONG, "0"));
userLocationPojo.setDistance(String.valueOf(AUtils.calculateDistance(
AUtils.mainApplicationConstant.getApplicationContext(), startLat, startLng)));
// userLocationPojo.setDatetime(AUtils.getServerDateTime()); //TODO
userLocationPojo.setDatetime(AUtils.getServerDateTimeLocal());
userLocationPojo.setOfflineId("0");
userLocationPojo.setIsOffline(AUtils.isInternetAvailable() && AUtils.isConnectedFast(mContext));
String UserTypeId = Prefs.getString(AUtils.PREFS.USER_TYPE_ID, AUtils.USER_TYPE.USER_TYPE_GHANTA_GADI);
if (AUtils.isInternetAvailable()) {
TableDataCountPojo.LocationCollectionCount count = syncOfflineRepository.getLocationCollectionCount(AUtils.getLocalDate());
if ((UserTypeId.equals(AUtils.USER_TYPE.USER_TYPE_GHANTA_GADI) || UserTypeId.equals(AUtils.USER_TYPE.USER_TYPE_WASTE_MANAGER))
&& (count.getLocationCount() > 0 || count.getCollectionCount() > 0)) {
syncOfflineRepository.insetUserLocation(userLocationPojo);
} else {
mUserLocationPojoList.add(userLocationPojo);
mAdapter.shareLocation(mUserLocationPojoList);
mUserLocationPojoList.clear();
}
} else {
if (UserTypeId.equals(AUtils.USER_TYPE.USER_TYPE_EMP_SCANNIFY)) {
Type type = new TypeToken<UserLocationPojo>() {
}.getType();
mLocationRepository.insertUserLocationEntity(new Gson().toJson(userLocationPojo, type));
} else {
syncOfflineRepository.insetUserLocation(userLocationPojo);
}
mUserLocationPojoList.clear();
}
}
else {
Log.i(TAG, "After");
syncOfflineAttendanceRepository.performCollectionInsert(mContext,
syncOfflineAttendanceRepository.checkAttendance(), AUtils.getCurrentDateDutyOffTime());
AUtils.setIsOnduty(false);
((MyApplication) AUtils.mainApplicationConstant).stopLocationTracking();
Activity activity = ((Activity) AUtils.currentContextConstant);
if (activity instanceof DashboardActivity) {
((Activity) AUtils.currentContextConstant).recreate();
AUtils.DutyOffFromService = true;
}
if (!AUtils.isNull(AUtils.currentContextConstant)) {
((Activity) AUtils.currentContextConstant).recreate();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private class TimerTaskToGetLocation extends TimerTask {
#Override
public void run() {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, AUtils.LOCATION_INTERVAL,
gpsFreqInDistance, this,null);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, AUtils.LOCATION_INTERVAL,
gpsFreqInDistance, this,null);
}
}
}

Confusion regarding Getting Latitude and Longitude from Activity to Fragment

I am now working on a online rent car finder apk. I want this program TO fetch database from server and sort it based on the location (distance from user current location of rent car garage).
But i am confuse here on how to sort the list view base on the distance ? I mean, i know how to calculate the distance but i am blank about how to get the user's current latitude and longitude inside an Activity without opening mapActivity?
This my condition right now :
1. My result list is inside ResultListFragment.java and my google map activity is on MapActivity.java.
2. I have to try and get the longitude,latitude using getLatitude and getLongitude method which i create on MapActivity.java, but it just returning zero (not null).
3. I tried to separate the gps listener on GPStracker class, but still it doesn't work.
Please help me, how do i get the latitude and longitude of the current user position.
NB. This is my ResultListFragment.java
public class ResultListFragment extends Fragment implements InternetConnectionListener,
LocationChangeListener {
public static String catId;
public static String titleId;
public static String searchTerm;
public static LocationChangeListener locationChangeListener;
private final int RESULT_ACTION = 1;
private final int RESULT_LIMIT = 100;
private ArrayList<Item> searchResultList;
private ResultListCallbacks mCallbacks;
private InternetConnectionListener internetConnectionListener;
private ArrayList<Item> resultList;
private ListView resultListView;
private LatLng itemLocation;
GPSTrackStandAlone gTrack = new GPSTrackStandAlone(getContext());
MapActivity mAct = new MapActivity();
public HomeActivity hAct = new HomeActivity();
public ResultListFragment() {
}
public static ResultListFragment newInstance(String id, String title) {
ResultListFragment fragment = new ResultListFragment();
catId = id;
titleId = title;
searchTerm = "";
locationChangeListener = fragment;
return fragment;
}
public static ResultListFragment newInstance(String id, String title,String term) {
ResultListFragment fragment = new ResultListFragment();
catId = id;
titleId = title;
searchTerm = term;
locationChangeListener = fragment;
return fragment;
}
public void getGPSLoc(){
double latitude = mAct.getLatitudeSend();
double longitude = mAct.getLongitudeSend();
Log.d("LatitudeCurrGPSLoc",String.valueOf(latitude));
Log.d("LongCur",String.valueOf(longitude));
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallbacks = (ResultListCallbacks) activity;
} catch (ClassCastException e) {
throw new ClassCastException("Activity must implement ResultListCallbacks.");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_result_list, container, false);
resultListView = (ListView) rootView.findViewById(R.id.resultListView);
setHasOptionsMenu(true);
return rootView;
}
#Override
public void onResume() {
super.onResume();
isHomeOpened = false;
isResultListFragmentOpened = true;
if (UtilMethods.isConnectedToInternet(getActivity())) {
if (!TextUtils.isEmpty(catId))
initResultList();
else if (!TextUtils.isEmpty(searchTerm))
getSearchResults(searchTerm);
} else {
internetConnectionListener = (InternetConnectionListener) ResultListFragment.this;
showNoInternetDialog(getActivity(), internetConnectionListener, getResources().getString(R.string.no_internet),
getResources().getString(R.string.no_internet_text),
getResources().getString(R.string.retry_string),
getResources().getString(R.string.exit_string), RESULT_ACTION);
}
}
private void initResultList() {
Log.d("Pembuktian", catId);
if(Integer.parseInt(catId)>100 && Integer.parseInt(catId)<200 ){
new getCarRent().execute();
}else if(Integer.parseInt(catId)>199 && Integer.parseInt(catId)<300){
new getCarBrand().execute();
}else if(Integer.parseInt(catId)>299 && Integer.parseInt(catId)<400){
new getCarType().execute();
}
/**
* json is populating from text file. To make api call use ApiHandler class
* pass parameter using ContentValues (values)
*
* <CODE> ApiHandler handler = new ApiHandler(this, URL_GET_RESULT_LIST_WITH_AD, values);</CODE> <BR>
* <CODE> handler.doApiRequest(ApiHandler.REQUEST_POST);</CODE> <BR>
*
* You will get the response in onSuccessResponse(String tag, String jsonString) method
* if successful api call has done.
*/
// String jsonString = loadJSONFromAsset(getActivity(), "get_result_list");
//parseJson(jsonString);
}
private void getSearchResults(String query) {
/**
* json is populating from text file. To make api call use ApiHandler class
* pass parameter using ContentValues (values)
*
* <CODE> ApiHandler handler = new ApiHandler(this, URL_GET_SEARCH_LIST_AD, values);</CODE> <BR>
* <CODE> handler.doApiRequest(ApiHandler.REQUEST_POST);</CODE> <BR>
*
* You will get the response in onSuccessResponse(String tag, String jsonString) method
* if successful api call has done.
*/
String jsonString = loadJSONFromAsset(getActivity(), "get_search_list");
parseJson(jsonString);
}
public class getCarRent extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params) {
URL hp = null;
try {
hp = new URL(getString(R.string.liveurl)
+ "getCarRent.php?value=" + catId);
// hp = new URL(
// "http://192.168.1.106/restourant/foodtype.php?value="
// + id);
Log.d("URL", "" + hp);
URLConnection hpCon = hp.openConnection();
hpCon.connect();
InputStream input = hpCon.getInputStream();
Log.d("input", "" + input);
BufferedReader r = new BufferedReader(new InputStreamReader(
input));
String x = "";
x = r.readLine();
String total = "";
while (x != null) {
total += x;
x = r.readLine();
}
Log.d("URL", "" + total);
JSONArray j = new JSONArray(total);
Log.d("URL1", "" + j.length());
Item[] itemList = new Item[j.length()];
resultList = new ArrayList<Item>();
for (int i = 0; i < j.length(); i++) {
Item item = new Item();// buat variabel category
//JSONObject Obj;
JSONObject Obj = j.getJSONObject(i); //sama sperti yang lama, cman ini lebih mempersingkat karena getJSONObject cm d tulis sekali aja disini
item.setId(Obj.getString(JF_ID));
//item.setTitle(Obj.getString(JF_TITLE));
item.setAddress(Obj.getString(JF_ADDRESS));
item.setTelephoneNumber(Obj.optString(JF_TELEPHONE, NO_DATA_FOUND));
item.setEmailAddress(Obj.optString(JF_EMAIL, NO_DATA_FOUND));
item.setWebUrl(Obj.optString(JF_WEB, NO_DATA_FOUND));
item.setFacebookUrl(Obj.optString(JF_FACEBOOK, NO_DATA_FOUND));
item.setLatitude(Obj.optDouble(JF_LATITUDE, NULL_LOCATION));
item.setLongitude(Obj.optDouble(JF_LONGITUDE, NULL_LOCATION));
try {
item.setRating(Float.parseFloat(Obj.optString(JF_RATING, NO_DATA_FOUND)));
} catch (NumberFormatException e) {
item.setRating(0.0f);
}
try {
item.setRatingCount(Integer.parseInt(Obj.optString(JF_RATING_COUNT, NO_DATA_FOUND)));
} catch (NumberFormatException e) {
item.setRatingCount(0);
}
try {
item.setRatingCount(Integer.parseInt(Obj.optString(JF_RATINGSCORE, NO_DATA_FOUND)));
} catch (NumberFormatException e) {
item.setRatingCount(0);
}
item.setTagLine(Obj.optString(JF_TAG_LINE, NO_DATA_FOUND));
item.setDescription(Obj.optString(JF_DESCRIPTION, NO_DATA_FOUND));
item.setVerification(Obj.optString(JF_VERIFICATION, NO_DATA_FOUND).equals("1") ? true : false);
item.setCarId(Obj.optString(JF_CARID, NO_DATA_FOUND));
item.setTitle(Obj.optString(JF_CARTITLE, NO_DATA_FOUND));
item.setCarRentalId(Obj.optString(JF_CARRENTALID, NO_DATA_FOUND));
item.setCarPrice(Obj.optString(JF_CARPRICE, NO_DATA_FOUND));
item.setCarYear(Obj.optString(JF_CARYEAR, NO_DATA_FOUND));
JSONArray imgArr = Obj.getJSONArray("thumbImage");
String[] imageThumb = new String[imgArr.length()];
// String[] imageLarge = new String[imgArr.length()];
for (int k = 0; k < imgArr.length(); k++) {
imageThumb[k] = imgArr.getString(k);
// imageLarge[k] = imgArr.getJSONObject(k).getString(JF_TITLE);
}
for(int l = 0; l <imgArr.length(); l++) {
item.setImageLargeUrls(imageThumb);
}
item.setImageThumbUrls(imageThumb);
// item.setImageLargeUrls(imageLarge);
// JSONArray imgArr = Obj.getJSONArray("thumbImage");
/*String[] imgCount = new String[imgArr.length()];
for(int k = 0 ; k < imgCount.length; k++) {
imgCount[k] = imgArr.getString(k);
item.setImageThumbUrls(imgCount);
}*/
Location trgtLocation = new Location("trgtLocation");
trgtLocation.setLatitude(item.getLatitude());
trgtLocation.setLongitude(item.getLongitude());
Location crntLocation = new Location("crntlocation");
crntLocation.setLatitude(gTrack.getLatitude());
crntLocation.setLongitude(gTrack.getLongitude());
Log.d("latitudeCurr", String.valueOf(gTrack.getLatitude()));
Log.d("curLocLong", String.valueOf(gTrack.getLongitude()));
//LatLng currentLatLng = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
item.setDistance(crntLocation.distanceTo(trgtLocation) / 1000);
Log.d("distance", String.valueOf(item.getDistance()));
Log.d("URL1", "" + Obj.getString(JF_TITLE));
resultList.add(item);
itemList[i]=item;
Log.d("itemList",String.valueOf(itemList[i]));
Arrays.sort(itemList, new Comparator<Item>() {
#Override
public int compare(Item lhs, Item rhs) {
return 0;
}
});
}
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
resultListView.setAdapter(new ResultListAdapterRental(getActivity(), mCallbacks, resultList));
}
});
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
// TODO: handle exception
}
return null;
}
}
I try to take the Lng and Lat from a System service like this one, but it's still doesn't work. Is this code right ?
GPSTracker.java
public class GPSTrackStandAlone extends Service implements LocationListener {
private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
private Context mContext;
private boolean isGpsEnabled = false;
private boolean isNetworkEnabled = false;
private boolean canGetLocation = false;
private Location mLocation;
private double mLatitude;
private double mLongitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATE = 10;
private static final long MIN_TIME_FOR_UPDATE = 60000;
private LocationManager mLocationManager;
private static final long INTERVAL = 1000 * 10;
private static final long FASTEST_INTERVAL = 1000 * 5;
public GPSTrackStandAlone(Context mContext) {
this.mContext = mContext;
}
/**
* #return location
*/
public Location getLocation() {
try {
mLocationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
/*getting status of the gps*/
isGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
/*getting status of network provider*/
isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGpsEnabled && !isNetworkEnabled) {
/*no location provider enabled*/
} else {
this.canGetLocation = true;
/*getting location from network provider*/
if (isNetworkEnabled) {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_FOR_UPDATE,
MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
Log.d("Perms", "Permission for GPS Granted");
if (mLocationManager != null) {
mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (mLocation != null) {
mLatitude = mLocation.getLatitude();
mLongitude = mLocation.getLongitude();
}
}
}
/*if gps is enabled then get location using gps*/
if (isGpsEnabled) {
if (mLocation == null) {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_FOR_UPDATE,
MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
Log.d("GPS Enabled", "GPS Enabled");
if (mLocationManager != null) {
mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mLocation != null) {
mLatitude = mLocation.getLatitude();
mLongitude = mLocation.getLongitude();
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return mLocation;
}
/**
* call this function to stop using gps in your application
*/
public void stopUsingGps() {
if (mLocationManager != null) {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mLocationManager.removeUpdates(GPSTrackStandAlone.this);
}
}
}
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
Toast.makeText(GPSTrackStandAlone.this, "isGooglePlayServiceAvailable = False", Toast.LENGTH_SHORT).show();
return false;
}
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
/**
* #return latitude
* <p/>
* function to get latitude
*/
public double getLatitude() {
getLocation();
if (mLocation != null) {
mLatitude = mLocation.getLatitude();
}
return mLatitude;
}
/**
* #return longitude
* function to get longitude
*/
public double getLongitude() {
getLocation();
if (mLocation != null) {
mLongitude = mLocation.getLongitude();
Log.d("MGPS", String.valueOf(mLocation.getLatitude()));
}
return mLongitude;
}
/**
* #return to check gps or wifi is enabled or not
*/
public boolean canGetLocation() {
return this.canGetLocation;
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Have you consider to use the Google Maps API ? With it you can simply do the following to get the user's current location :
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
mMap.setMyLocationEnabled(true);
Location myLocation = googleMap.getMyLocation();
Do you mean that you can getLng and getLat in activity ?
Try to use a public static to describe your values,and use MyActivity.value in Fragment.
when you want to pass the data from activity to fragment you should use bundle .
I will give you the example.
public class CompleteJobFragment extends Fragment {
private final static String PICKUP_TIME="pickup time";
public static CompleteJobFragment newInstance(String pickUpTime) {
CompleteJobFragment fragment = new CompleteJobFragment();
Bundle bundle = new Bundle();
bundle.putString(PICKUP_TIME, pickUpTime);
fragment.setArguments(bundle);
return fragment;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
pickUpTime = getArguments().getString(PICKUP_TIME);
}
}
Activity file
public class ActiveJobActivity extends AppCompatActivity {
String x1;
public void loadData()
{
CompleteJobFragment completeJobFragment = CompleteJobFragment.newInstance(x1);
final FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.activez, completeJobFragment, COMPLETE_FRAGMENT);
fragmentTransaction.addToBackStack(COMPLETE_FRAGMENT);
completeJobFragment.setCommunicator(this);
fragmentTransaction.commit();
}

How to make app recognize that location is enabled after startup

I solved the issue for "checking if location is enabled", but now I'm stuck. I want the app to show a toast if location is disabled, and once it's enabled by the user, I want the app to proceed as normal, like location was enabled at the first place. My code looks correct to me, I don't know where the problem is. Note that I don't have any issues with accessing the location, network and stuff. It's just that I always need to restart the app after enabling location, otherwise it won't proceed and keep giving me the warning toast. Thanks in advance.
Edit: So I just added an alert dialog method instead of Toast messages. Users can now go to settings and turn location and network on using that dialog. I'd like you to take a look at the Network check if-else statement inside getForecast() method. else contains the alert dialog that leads user to Settings-Data Roaming. And when user activated mobile data and returns to the app, everything's fine and app can get info. I did the same for location if-else statement, yet when user turns location on and returns to the app, no matter what I do (wait several minutes, refresh several times), I'm not getting the location info. I need to close and reopen the app every time. That's the exact issue I'm facing.
Code:
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
public static final String DAILY_FORECAST = "DAILY_FORECAST";
private Forecast mForecast;
#Bind(R.id.timeLabel) TextView mTimeLabel;
#Bind(R.id.tempLabel) TextView mTempLabel;
#Bind(R.id.humidityValue) TextView mHumidityValue;
#Bind(R.id.precipValue) TextView mPrecipValue;
#Bind(R.id.summaryLabel) TextView mSummaryLabel;
#Bind(R.id.windSpeedValue) TextView mWindSpeedValue;
#Bind(R.id.relativeLayout) RelativeLayout mRelativeLayout;
#Bind(R.id.container) SwipeRefreshLayout mSwipeRefreshLayout;
#Bind(R.id.locationLabel) TextView mLocationLabel;
double latitude;
double longitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
final GPSTracker gpsTracker = new GPSTracker(this);
mSwipeRefreshLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.CYAN);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
}
}, 1500);
if (gpsTracker.getIsGPSTrackingEnabled()){
getForecast(gpsTracker);
updateDisplay(gpsTracker);
} else {
Toast.makeText(MainActivity.this, "Please enable location services.!!", Toast.LENGTH_LONG).show();
gpsTracker.showSettingsAlert();
}
}
});
getForecast(gpsTracker);
}
private void getForecast(final GPSTracker gpsTracker) {
latitude = gpsTracker.getLatitude();
longitude = gpsTracker.getLongitude();
String apiKey = "7d22cdb138cd70f2e9e8d2006cd0461c";
String forecastUrl = "https://api.forecast.io/forecast/" + apiKey
+ "/" + latitude + "," + longitude;
if (isNetworkAvailable()) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(forecastUrl).build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
#Override
public void onFailure(Request request, IOException e) {
alertUserAboutError();
}
#Override
public void onResponse(Response response) throws IOException {
try {
String jsonData = response.body().string();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mForecast = parseForecastDetails(jsonData);
runOnUiThread(new Runnable() {
#Override
public void run() {
if (gpsTracker.getIsGPSTrackingEnabled()) {
updateDisplay(gpsTracker);
} else {
Toast.makeText(MainActivity.this, "Please enable location services.", Toast.LENGTH_LONG).show();
}
}
});
} else {
alertUserAboutError();
}
} catch (IOException | JSONException e) {
Log.e(TAG, "Exception caught: ", e);
}
}
});
} else {
gpsTracker.showSettingsAlert2();
}
}
private void updateDisplay(GPSTracker gpsTracker) {
Currently currently = mForecast.getCurrently();
String area = gpsTracker.getSubLocality(this);
String city = gpsTracker.getAdminArea(this);
String country = gpsTracker.getCountryName(this);
mLocationLabel.setText(area + "\n" + city + ", " + country);
mTempLabel.setText(currently.getTemperature() + "");
mTimeLabel.setText(currently.getFormattedTime());
mHumidityValue.setText(currently.getHumidity() + "%");
mPrecipValue.setText(currently.getPrecipChance() + "%");
mSummaryLabel.setText(currently.getSummary());
mWindSpeedValue.setText(currently.getWindSpeed() + "");
Drawable drawable = getResources().getDrawable(currently.getBackgroundId());
mRelativeLayout.setBackground(drawable);
}
// irrelevant after this point. (I guess)
private Forecast parseForecastDetails(String jsonData) throws JSONException {
Forecast forecast = new Forecast();
forecast.setCurrently(getCurrentlyDetails(jsonData));
forecast.setHourlyForecast(getHourlyForecast(jsonData));
forecast.setDailyForecast(getDailyForecast(jsonData));
return forecast;
}
private Daily[] getDailyForecast(String jsonData) throws JSONException {
JSONObject forecast = new JSONObject(jsonData);
String timezone = forecast.getString("timezone");
JSONObject jsonDaily = forecast.getJSONObject("daily");
JSONArray data = jsonDaily.getJSONArray("data");
Daily[] days = new Daily[data.length()];
for (int i = 0; i < data.length(); i++) {
JSONObject daily = data.getJSONObject(i);
Daily day = new Daily();
day.setSummary(daily.getString("summary"));
day.setTempMax(daily.getDouble("temperatureMax"));
day.setTempMin(daily.getDouble("temperatureMin"));
day.setIcon(daily.getString("icon"));
day.setTime(daily.getLong("time"));
day.setTimezone(timezone);
days[i] = day;
}
return days;
}
private Hourly[] getHourlyForecast(String jsonData) throws JSONException{
JSONObject forecast = new JSONObject(jsonData);
String timezone = forecast.getString("timezone");
JSONObject jsonHourly = forecast.getJSONObject("hourly");
JSONArray data = jsonHourly.getJSONArray("data");
Hourly[] hours = new Hourly[data.length()];
for(int i=0; i < data.length(); i++) {
JSONObject hourly = data.getJSONObject(i);
Hourly hour = new Hourly();
hour.setSummary(hourly.getString("summary"));
hour.setTemp(hourly.getDouble("temperature"));
hour.setIcon(hourly.getString("icon"));
hour.setTime(hourly.getLong("time"));
hour.setTimezone(timezone);
hours[i] = hour;
}
return hours;
}
private Currently getCurrentlyDetails(String jsonData) throws JSONException {
JSONObject forecast = new JSONObject(jsonData);
String timezone = forecast.getString("timezone");
JSONObject currently = forecast.getJSONObject("currently");
Currently currentWeather = new Currently();
currentWeather.setHumidity(currently.getDouble("humidity"));
currentWeather.setTime(currently.getLong("time"));
currentWeather.setPrecipChance(currently.getDouble("precipProbability"));
currentWeather.setSummary(currently.getString("summary"));
currentWeather.setTemperature(currently.getDouble("temperature"));
currentWeather.setWindSpeed(currently.getDouble("windSpeed"));
currentWeather.setTimeZone(timezone);
currentWeather.setBackgroundId(currently.getString("icon"));
Log.d(TAG, currentWeather.getFormattedTime());
return currentWeather;
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()){
isAvailable = true;
}
return isAvailable;
}
private void alertUserAboutError() {
AlertDialogFragment dialog = new AlertDialogFragment();
dialog.show(getFragmentManager(), "error_dialog");
}
#OnClick (R.id.dailyButton)
public void startDailyActivity(View view){
Intent intent = new Intent(this, DailyForecastActivity.class);
intent.putExtra(DAILY_FORECAST, mForecast.getDailyForecast());
startActivity(intent);
}
}

Finding location in android using Gabe Sechan's tutorial

I have tried using androidhive's standard tutorial for finding location,but it fails at certain times.So i found another tutorial on this link:
http://gabesechansoftware.com/location-tracking/
He uses two classes which implement an interface:
The first class:
package com.example.marinamapseg;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
public class FallbackLocationTracker implements LocationTracker, LocationTracker.LocationUpdateListener {
private boolean isRunning;
private ProviderLocationTracker gps;
private ProviderLocationTracker net;
private LocationUpdateListener listener;
Location lastLoc;
long lastTime;
public FallbackLocationTracker(Context context) {
gps = new ProviderLocationTracker(context, ProviderLocationTracker.ProviderType.GPS);
net = new ProviderLocationTracker(context, ProviderLocationTracker.ProviderType.NETWORK);
}
public void start(){
if(isRunning){
//Already running, do nothing
return;
}
//Start both
gps.start(this);
net.start(this);
isRunning = true;
}
public void start(LocationUpdateListener update) {
start();
listener = update;
}
public void stop(){
if(isRunning){
gps.stop();
net.stop();
isRunning = false;
listener = null;
}
}
public boolean hasLocation(){
//If either has a location, use it
return gps.hasLocation() || net.hasLocation();
}
public boolean hasPossiblyStaleLocation(){
//If either has a location, use it
return gps.hasPossiblyStaleLocation() || net.hasPossiblyStaleLocation();
}
public Location getLocation(){
Location ret = gps.getLocation();
if(ret == null){
ret = net.getLocation();
}
return ret;
}
public Location getPossiblyStaleLocation(){
Location ret = gps.getPossiblyStaleLocation();
if(ret == null){
ret = net.getPossiblyStaleLocation();
}
return ret;
}
public void onUpdate(Location oldLoc, long oldTime, Location newLoc, long newTime) {
boolean update = false;
//We should update only if there is no last location, the provider is the same, or the provider is more accurate, or the old location is stale
if(lastLoc == null){
update = true;
}
else if(lastLoc != null && lastLoc.getProvider().equals(newLoc.getProvider())){
update = true;
}
else if(newLoc.getProvider().equals(LocationManager.GPS_PROVIDER)){
update = true;
}
else if (newTime - lastTime > 5 * 60 * 1000){
update = true;
}
if(update){
lastLoc = newLoc;
lastTime = newTime;
if(listener != null){
listener.onUpdate(lastLoc, lastTime, newLoc, newTime);
}
}
}
}
The second class:
package com.example.marinamapseg;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class ProviderLocationTracker implements LocationListener, LocationTracker {
// The minimum distance to change Updates in meters
private static final long MIN_UPDATE_DISTANCE = 0;
// The minimum time between updates in milliseconds
private static final long MIN_UPDATE_TIME = 0;
private LocationManager lm;
public enum ProviderType{
NETWORK,
GPS
};
private String provider;
private Location lastLocation;
private long lastTime;
private boolean isRunning;
private LocationUpdateListener listener;
public ProviderLocationTracker(Context context, ProviderType type) {
lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
if(type == ProviderType.NETWORK){
provider = LocationManager.NETWORK_PROVIDER;
}
else{
provider = LocationManager.GPS_PROVIDER;
}
}
public void start(){
if(isRunning){
//Already running, do nothing
return;
}
//The provider is on, so start getting updates. Update current location
isRunning = true;
lm.requestLocationUpdates(provider, MIN_UPDATE_TIME, MIN_UPDATE_DISTANCE, this);
lastLocation = null;
lastTime = 0;
return;
}
public void start(LocationUpdateListener update) {
start();
listener = update;
}
public void stop(){
if(isRunning){
lm.removeUpdates(this);
isRunning = false;
listener = null;
}
}
public boolean hasLocation(){
if(lastLocation == null){
return false;
}
if(System.currentTimeMillis() - lastTime > 5 * MIN_UPDATE_TIME){
return false; //stale
}
return true;
}
public boolean hasPossiblyStaleLocation(){
if(lastLocation != null){
return true;
}
return lm.getLastKnownLocation(provider)!= null;
}
public Location getLocation(){
if(lastLocation == null){
return null;
}
if(System.currentTimeMillis() - lastTime > 5 * MIN_UPDATE_TIME){
return null; //stale
}
return lastLocation;
}
public Location getPossiblyStaleLocation(){
if(lastLocation != null){
return lastLocation;
}
return lm.getLastKnownLocation(provider);
}
public void onLocationChanged(Location newLoc) {
long now = System.currentTimeMillis();
if(listener != null){
listener.onUpdate(lastLocation, lastTime, newLoc, now);
}
lastLocation = newLoc;
lastTime = now;
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String arg0) {
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
But the issue is that he has not mentioned as how to use the following two classes in the android app.So i tried the following:
FallbackLocationTracker fobj;
fobj=new FallbackLocationTracker(MainActivity.this);
fobj.start();
if(fobj.hasLocation())
{
Location locobj=fobj.getLocation();
current_latitude=locobj.getLatitude();
current_longitude=locobj.getLongitude();
Toast.makeText(getApplicationContext(),current_latitude+" "+current_longitude,Toast.LENGTH_LONG).show();
placemarkersonmap();
}
When i debug the code the hasLocation method returns false, Because the onLocationChanged method in the first class is getting called only after the haslocation method is called.So the lastlocation object will be null and false would be returned.
But why doesnt onlocationchanged get called as soon as the following line runs:
lm.requestLocationUpdates(provider, MIN_UPDATE_TIME, MIN_UPDATE_DISTANCE, this);
Or is there any way somenone can tell me as how to use the code.Please help!!
onLocationChanged is a callback, meaning that it is event driven and does not run until the device detects a change of location. If you are taking location only from GPS, then it can take several minutes before you get a fix. If you are indoors without a clear view of the sky, then it may never run.
You may add the following in your actitvity's onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewHeading = (TextView) this.findViewById(R.id.text_heading);
textViewLong = (TextView) this.findViewById(R.id.text_long);
textViewLat = (TextView) this.findViewById(R.id.text_lat);
textViewHeight = (TextView) this.findViewById(R.id.text_height);
textViewAccuracy = (TextView) this.findViewById(R.id.text_accuracy);
counter = (long) 0;
gps = new ProviderLocationTracker(this,
ProviderLocationTracker.ProviderType.GPS);
listener = new LocationUpdateListener() {
#Override
public void onUpdate(Location oldLoc, long oldTime, Location newLoc,
long newTime) {
// TODO Auto-generated method stub
counter = counter + (long) 1;
if (gps.hasLocation()) {
text1="GPS has location ( " + counter.toString() + " )";
} else {
text1="GPS has no location ( " + counter.toString() + " )";
}
if (gps.hasPossiblyStaleLocation()) {
text1 = text1 + " plus stale location";
}
Double longitude = newLoc.getLongitude();
Double latitude = newLoc.getLatitude();
Double height = newLoc.getAltitude();
Float accuracy = newLoc.getAccuracy();
text2 = "Longitude = " + longitude.toString();
text3 = "Latitude = " + latitude.toString();
text4 = "Altitude = " + height.toString();
text5 = "Accuracy = " + accuracy.toString();
textViewHeading.setText(text1);
textViewLong.setText(text2);
textViewLat.setText(text3);
textViewHeight.setText(text4);
textViewAccuracy.setText(text5);
}
};
gps.start(listener);
} // end method onCreate
It seems that this post already has an answer that shows how to use the ProviderLocationTracker. I am putting in my answer just to show how easy it get's with the FallbackLocationTracker.
I do not remember making any significant changes to the FallbackLocationTracker so I am not going to post the whole code.
public class MainActivity extends AppCompatActivity {
private FallbackLocationTracker tracker;
private Location location;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tracker = new FallbackLocationTracker(MainActivity.this);
}
#Override
protected void onResume() {
super.onResume();
// Check for permission to access location and start location
// tracking if permissions are granted and location services
// are enabled
if (locationPermissionGranted && locationServicesEnabled) {
startLocationUpdates();
} else {
// Handle accordingly
}
}
#Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
}
private void startLocationUpdates() {
tracker.start(new LocationTracker.LocationUpdateListener() {
#Override
public void onUpdate(Location oldLocation, long oldTime,
Location newLocation, long newTime) {
// You will receive updated changes to your location here
location = newLocation;
}
});
}
private void stopLocationUpdates() {
tracker.stop();
}
}
That's it.

Location manager is not working without internet

public class NativeGeolocation extends Plugin {
public long maximumAge = 1000 * 30; // ms
public long timeout = 1000 * 30; // ms
public Location lastPosition = null;
public static final String ACTION_GETCURRENTPOSITION="getCurrentPosition";
protected String callbackId = null;
#Override
public PluginResult execute(String action, JSONArray data, String callbackId)
{
JSONObject options = data.optJSONObject(0);
Log.i("Myactivity","options : "+options);
this.timeout = timeout;
this.callbackId = callbackId;
Log.i("Myactivity","callbackId : "+this.callbackId);
PluginResult result = new PluginResult(Status.NO_RESULT, callbackId);
result.setKeepCallback(true);
final LocationManager locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria,false);
if (ACTION_GETCURRENTPOSITION.equals(action)) {
Log.i("Myactivity","inside getcurrentposition action");
Location lastKnownLocation = locationManager.getLastKnownLocation(provider);
lastPosition = lastKnownLocation;
Log.i("Myactivity","last location "+lastKnownLocation);
if ((null != lastKnownLocation) && lastKnownLocation.getTime() + this.maximumAge > new Date().getTime()) {
Log.i("Myactivity","inside b4 gotLocation");
gotLocation(lastKnownLocation);
} else {
ctx.runOnUiThread(new RunnableLocationListener(this, callbackId, locationManager, provider));
}
} else {
error(new PluginResult(Status.INVALID_ACTION), callbackId);
}
return result;
}
public void gotLocation (Location location) {
Log.i("Myactivity","inside gotLocation");
try {
Log.i("Myactivity","inside try");
JSONObject geoposition = new JSONObject();
JSONObject coords = new JSONObject();
coords.put("latitude", location.getLatitude());
coords.put("longitude", location.getLongitude());
coords.put("altitude", location.getAltitude());
coords.put("accuracy", location.getAccuracy());
coords.put("altitudeAccuracy", null);
coords.put("heading", null);
coords.put("speed", location.getSpeed());
geoposition.put("coords", coords);
geoposition.put("timestamp", location.getTime());
geoposition.put("provider", location.getProvider());
geoposition.put("lastPos", lastPosition);
success(new PluginResult(Status.OK, geoposition), callbackId);
} catch (JSONException jsonEx) {
error(new PluginResult(Status.JSON_EXCEPTION), callbackId);
}
}
protected String getBestProvider (LocationManager locationManager) {
String provider = LocationManager.PASSIVE_PROVIDER;
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
}
else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
}
return provider;
}
class RunnableLocationListener extends Thread {
protected final NativeGeolocation plugin;
protected final LocationManager locationManager;
protected final String provider;
protected final String callbackId;
public Boolean ended = null;
public RunnableLocationListener (NativeGeolocation plugin, String callbackId, LocationManager locationManager, String provider) {
Log.i("Myactivity","inside runnabl");
this.plugin = plugin;
this.locationManager = locationManager;
this.provider = provider;
this.callbackId = callbackId;
}
public void run () {
ended = false;
PluginResult result = null;
final LocationListener locationListener = new LocationListener() {
public void onLocationChanged (Location location) {
Log.i("Myactivity","calling getlocation again1");
if ( false == ended ) {
Log.i("Myactivity","calling getlocation again2");
plugin.gotLocation(location);
locationManager.removeUpdates(this);
}
}
public void onStatusChanged (String provider, int status, Bundle extras) {
Log.i("Myactivity","inside onStatus Changed");
}
public void onProviderEnabled(String provider) {
Log.i("Myactivity","inside onProvider Enabled");
}
public void onProviderDisabled(String provider) {
Log.i("Myactivity","inside onProvider Disabled");
}
};
locationManager.requestLocationUpdates(provider,1, 10, locationListener);//1 minutes and 10 meters
Thread timeouter = new Thread() {
public void run () {
try {
Thread.sleep(plugin.timeout);
ended = true;
locationManager.removeUpdates(locationListener);
plugin.error(new PluginResult(Status.ERROR, "timeout"), callbackId);
} catch (java.lang.InterruptedException ex) {
error(new PluginResult(Status.JSON_EXCEPTION), callbackId);
}
}
};
timeouter.start();
}
}
}
This is java plugin n I am calling through phonegap .
But It gives location only if internet is available. I need to get location only using gps hardware and not through internet . Any help??
I refferred some of the stackoverflow questions related to this. So i got know that GPS will not work inside the building . Ok , but in my case it is not working outside also. Until and unless it is not getting last known location it is not working . Once it has last location some how then it starts working . Any help ??
EDIT : According to this link Android - Trouble in getting location coordinates by only using GPS provider , i can use libwlocate . But how to use that as phonegap plugin ?? Any help ??
Any help is greatly appreciated.
EDIT: How about if i put some dummy lastKnownLocation for the first time. Then it will requestForNew one right??
You can request "ONLY GPS" like this...
LocationProvider gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
if(gpsProvider != null){
locationManager.requestLocationUpdates(gpsProvider.getName(), 0, 0, this);
}
Boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Location lastLocation = null, gpsLocation = null;
if (isGPSEnabled)
gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

Categories