Untitled diff
198 lines
package com.example.kimberlycrawley.gethandynavigator;
import android.Manifest;
import android.Manifest.permission;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity implements LocationListener {
public class MainActivity extends AppCompatActivity implements LocationListener {
boolean permissionCheck = (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION)== PackageManager.PERMISSION_GRANTED);
boolean permissionCheck2 = (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED);
// Only execute if one of the two type of location permission is granted
if( permissionCheck || permissionCheck2){
// Code to initialize the locationManager via getSystemService
Location location = locationManager.getLastKnownLocation(provider);
onLocationChanged(location);
}
Location location = locationManager.getLastKnownLocation(provider);
onLocationChanged(location);
}
LocationManager locationManager;
LocationManager locationManager;
String provider;
String provider;
TextView latTV;
TextView latTV;
TextView lngTV;
TextView lngTV;
TextView addressTV;
TextView addressTV;
TextView altitudeTV;
TextView altitudeTV;
boolean permissionCheck, permissionCheck2;
@Override
@Override
protected void onCreate(Bundle savedInstanceState) {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
latTV = (TextView) findViewById(R.id.lat);
latTV = (TextView) findViewById(R.id.lat);
lngTV = (TextView) findViewById(R.id.lng);
lngTV = (TextView) findViewById(R.id.lng);
addressTV = (TextView) findViewById(R.id.address);
addressTV = (TextView) findViewById(R.id.address);
altitudeTV = (TextView) findViewById(R.id.alt);
altitudeTV = (TextView) findViewById(R.id.alt);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
permissionCheck = (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED);
// No provider criteria
permissionCheck2 = (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
provider = locationManager.getBestProvider(new Criteria(), false);
// Only execute if one of the two type of location permission is granted
if (permissionCheck || permissionCheck2) {
// Code to initialize the locationManager via getSystemService
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Get address at start, no location update required
// No provider criteria
provider = locationManager.getBestProvider(new Criteria(), false);
Location location = locationManager.getLastKnownLocation(provider);
// Get address at start, no location update required
onLocationChanged(location);
Location location = locationManager.getLastKnownLocation(provider);
onLocationChanged(location);
}
}
}
@Override
@Override
public boolean onCreateOptionsMenu(Menu menu) {
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
return true;
}
}
@Override
@Override
public boolean onOptionsItemSelected(MenuItem item) {
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
int id = item.getItemId();
//noinspection SimplifiableIfStatement
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
if (id == R.id.action_settings) {
return true;
return true;
}
}
return super.onOptionsItemSelected(item);
return super.onOptionsItemSelected(item);
}
}
// Just in case GPS is turned off
// Just in case GPS is turned off
@Override
@Override
protected void onPause() {
protected void onPause() {
super.onPause();
super.onPause();
// Stop listening for location
if (locationManager != null && (permissionCheck || permissionCheck2)) {
// Stop listening for location
locationManager.removeUpdates(this);
locationManager.removeUpdates(this);
}
}
}
@Override
@Override
protected void onResume() {
protected void onResume() {
super.onResume();
super.onResume();
if (locationManager != null && (permissionCheck || permissionCheck2)) {
// Resume location getting, every 400 mils, 1 metre, this context
// Resume location getting, every 400 mils, 1 metre, this context
locationManager.requestLocationUpdates(provider, 400, 1, this);
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
}
}
@Override
@Override
public void onLocationChanged(Location location) {
public void onLocationChanged(Location location) {
// Get user's lat and lng
// Get user's lat and lng
Double lat = location.getLatitude();
Double lat = location.getLatitude();
Double lng = location.getLongitude();
Double lng = location.getLongitude();
Double alt = location.getAltitude();
Double alt = location.getAltitude();
// Get geolocation
// Get geolocation
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
try {
List<Address> listAddresses = geocoder.getFromLocation(lat, lng, 1);
List<Address> listAddresses = geocoder.getFromLocation(lat, lng, 1);
if (listAddresses != null && listAddresses.size() > 0) {
if (listAddresses != null && listAddresses.size() > 0) {
Log.i("Place Info", listAddresses.get(0).toString());
Log.i("Place Info", listAddresses.get(0).toString());
String addressHolder = "";
String addressHolder = "";
// Because we don't know the number of address lines
// Because we don't know the number of address lines
for (int i = 0; i < listAddresses.get(0).getMaxAddressLineIndex(); i++) {
for (int i = 0; i < listAddresses.get(0).getMaxAddressLineIndex(); i++) {
addressHolder += listAddresses.get(0).getAddressLine(i) + "\n";
addressHolder += listAddresses.get(0).getAddressLine(i) + "\n";
}
}
addressTV.setText("Address:\n" + addressHolder);
addressTV.setText("Address:\n" + addressHolder);
}
}
} catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
}
}
latTV.setText("Latitude: " + lat.toString());
latTV.setText("Latitude: " + lat.toString());
lngTV.setText("Longitude: " + lng.toString());
lngTV.setText("Longitude: " + lng.toString());
altitudeTV.setText("Altitude: " + alt.toString() + "m");
altitudeTV.setText("Altitude: " + alt.toString() + "m");
Log.i("Latitude", String.valueOf(lat));
Log.i("Latitude", String.valueOf(lat));
Log.i("Longitude", String.valueOf(lng));
Log.i("Longitude", String.valueOf(lng));
Log.i("Altitude", String.valueOf(alt));
Log.i("Altitude", String.valueOf(alt));
}
}
@Override
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
public void onStatusChanged(String s, int i, Bundle bundle) {
}
}
@Override
@Override
public void onProviderEnabled(String s) {
public void onProviderEnabled(String s) {
}
}
@Override
@Override
public void onProviderDisabled(String s) {
public void onProviderDisabled(String s) {
}
}
}
}