Skip to content
Snippets Groups Projects
Commit 3c515b71 authored by kishoreraju2's avatar kishoreraju2
Browse files

Location Updated

parent 7b7f00c0
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,8 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.camera2"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
......
......@@ -37,6 +37,12 @@ public class ComposeFragment extends Fragment implements View.OnClickListener{
private static int RESULT_LOAD_IMAGE = 1;
private static int RESULT_MAIL = 2;
private Button b_get;
private com.mapps.seproject.TrackGPS gps;
double longitude;
double latitude;
String city;
String postalCode;
......@@ -52,10 +58,11 @@ public class ComposeFragment extends Fragment implements View.OnClickListener{
bComposeMail = (Button) view.findViewById(R.id.bComposeMail);
emailText = (TextView) view.findViewById(R.id.tvEmailMessage);
bAddImage = (Button) view.findViewById(R.id.bAddImage);
b_get = (Button) view.findViewById(R.id.button2);
bAddImage.setOnClickListener(this);
bComposeMail.setOnClickListener(this);
b_get.setOnClickListener(this);
return view;
}
......@@ -141,6 +148,27 @@ public class ComposeFragment extends Fragment implements View.OnClickListener{
}
//Location
if(v == b_get){
gps = new TrackGPS(getActivity());
if(gps.canGetLocation()){
longitude = gps.getLongitude();
latitude = gps .getLatitude();
city = gps.getCity();
postalCode = gps.getPostalCode();
Toast.makeText(getActivity(),"Longitude:"+Double.toString(longitude)+"\nLatitude:"+Double.toString(latitude)+"\nCity:"+city+"\nPostal:"+postalCode,Toast.LENGTH_SHORT).show();
}
else
{
gps.showSettingsAlert();
}
}
......
package com.mapps.seproject;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;
import java.util.List;
import java.util.Locale;
/**
* Created by Kishore on 8/8/2016.
*/
public class TrackGPS extends Service implements LocationListener {
private final Context mContext;
boolean checkGPS = false;
boolean checkNetwork = false;
boolean canGetLocation = false;
Location loc;
double latitude;
double longitude;
String city;
String postalCode;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
protected LocationManager locationManager;
public TrackGPS(Context mContext) {
this.mContext = mContext;
getLocation();
}
private Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
checkGPS = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
checkNetwork = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!checkGPS && !checkNetwork) {
Toast.makeText(mContext, "No Service Provider Available", Toast.LENGTH_SHORT).show();
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (checkNetwork) {
//Toast.makeText(mContext, "Network", Toast.LENGTH_SHORT).show();
try {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,1000,0, this);
Log.d("Network", "Network");
if (locationManager != null) {
loc = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (loc != null) {
Toast.makeText(mContext,"Gps enabled",Toast.LENGTH_SHORT).show();
latitude = loc.getLatitude();
longitude = loc.getLongitude();
Geocoder geocoder= new Geocoder(mContext,Locale.getDefault());
List<Address> addresses;
addresses = geocoder.getFromLocation(latitude, longitude, 1);
// String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
city = addresses.get(0).getLocality();
//String state = addresses.get(0).getAdminArea();
//String country = addresses.get(0).getCountryName();
postalCode = addresses.get(0).getPostalCode();
//String knownName = addresses.get(0).getFeatureName();
}
}
catch(SecurityException e){
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (checkGPS) {
if (loc == null) {
try {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,1000,0, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
loc = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc != null) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
Geocoder geocoder= new Geocoder(mContext,Locale.getDefault());
List<Address> addresses;
addresses = geocoder.getFromLocation(latitude, longitude, 1);
// String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
city = addresses.get(0).getLocality();
//String state = addresses.get(0).getAdminArea();
//String country = addresses.get(0).getCountryName();
postalCode = addresses.get(0).getPostalCode();
//String knownName = addresses.get(0).getFeatureName();
}
}
} catch (SecurityException e) {
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return loc;
}
public double getLongitude() {
if (loc != null) {
longitude = loc.getLongitude();
}
return longitude;
}
public double getLatitude() {
if (loc != null) {
latitude = loc.getLatitude();
}
return latitude;
}
public String getCity(){
return this.city;
}
public String getPostalCode(){
return this.postalCode;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle("GPS Not Enabled");
alertDialog.setMessage("Do you wants to turn On GPS");
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(TrackGPS.this);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
\ No newline at end of file
......@@ -53,6 +53,14 @@
android:layout_marginBottom="19dp"
android:hint="Enter your complaint here" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/tvEmailMessage"
android:layout_centerHorizontal="true"
android:text="Get Location" />
</RelativeLayout>
</FrameLayout>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment