-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3 : Added Location Button and current Location Services
- Loading branch information
1 parent
36556e5
commit 948130f
Showing
5 changed files
with
202 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
app/src/main/java/com/pulkit4tech/privy/Utilities/LocationServices.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.pulkit4tech.privy.Utilities; | ||
|
||
import android.Manifest; | ||
import android.content.Context; | ||
import android.content.pm.PackageManager; | ||
import android.location.Criteria; | ||
import android.location.Location; | ||
import android.location.LocationManager; | ||
import android.location.LocationListener; | ||
import android.os.Bundle; | ||
import android.support.v4.app.ActivityCompat; | ||
import android.util.Log; | ||
import android.widget.Toast; | ||
|
||
|
||
import com.google.android.gms.maps.model.LatLng; | ||
|
||
import com.pulkit4tech.privy.data.LocationData; | ||
|
||
import static android.content.Context.LOCATION_SERVICE; | ||
import static com.pulkit4tech.privy.MainActivity.DEBUG; | ||
|
||
/** | ||
* Created by pulkit on 01/01/17. | ||
*/ | ||
|
||
public class LocationServices implements LocationListener { | ||
|
||
private Context mContext; | ||
private LocationData myLocation; | ||
|
||
public LocationServices(Context context) { | ||
mContext = context; | ||
} | ||
|
||
public LocationData getCurrentLocation() { | ||
LocationManager locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); | ||
Criteria criteria = new Criteria(); | ||
criteria.setAccuracy(Criteria.ACCURACY_COARSE); | ||
criteria.setAltitudeRequired(false); | ||
criteria.setBearingRequired(false); | ||
criteria.setCostAllowed(true); | ||
criteria.setPowerRequirement(Criteria.POWER_MEDIUM); | ||
String bestProvider = locationManager.getBestProvider(criteria, true); | ||
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | ||
// TODO: Consider calling | ||
// ActivityCompat#requestPermissions | ||
// here to request the missing permissions, and then overriding | ||
// public void onRequestPermissionsResult(int requestCode, String[] permissions, | ||
// int[] grantResults) | ||
// to handle the case where the user grants the permission. See the documentation | ||
// for ActivityCompat#requestPermissions for more details. | ||
return myLocation; | ||
} | ||
Location location = locationManager.getLastKnownLocation(bestProvider); | ||
locationManager.requestLocationUpdates(bestProvider, 1000, 0, this); | ||
if (location != null) { | ||
onLocationChanged(location); | ||
} | ||
return myLocation; | ||
} | ||
|
||
|
||
@Override | ||
public void onLocationChanged(Location location) { | ||
|
||
double latitude = location.getLatitude(); | ||
double longitude = location.getLongitude(); | ||
LatLng latLng = new LatLng(latitude, longitude); | ||
myLocation = new LocationData(); | ||
myLocation.setLatLng(latLng); | ||
} | ||
|
||
@Override | ||
public void onStatusChanged(String s, int i, Bundle bundle) { | ||
|
||
} | ||
|
||
@Override | ||
public void onProviderEnabled(String s) { | ||
|
||
} | ||
|
||
@Override | ||
public void onProviderDisabled(String s) { | ||
Toast.makeText(mContext,"Provider Disabled",Toast.LENGTH_SHORT).show(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
app/src/main/java/com/pulkit4tech/privy/data/LocationData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.pulkit4tech.privy.data; | ||
|
||
import com.google.android.gms.maps.model.LatLng; | ||
|
||
/** | ||
* Created by pulkit on 01/01/17. | ||
*/ | ||
|
||
public class LocationData { | ||
private LatLng latLng; | ||
private String Description; | ||
|
||
public LatLng getLatLng() { | ||
return latLng; | ||
} | ||
|
||
public void setLatLng(LatLng latLng) { | ||
this.latLng = latLng; | ||
} | ||
|
||
public String getDescription() { | ||
return Description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
Description = description; | ||
} | ||
} |