package com.example.cw.hikerswatch;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
LocationListener locationListener;
TextView latitudeTextView;
TextView longitudeTextView;
TextView accuracyTextView;
TextView addressTextView;
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
startListening();
}
}
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitudeTextView = (TextView)findViewById(R.id.latitudeTextView);
longitudeTextView = (TextView)findViewById(R.id.longitudeTextView);
accuracyTextView = (TextView)findViewById(R.id.accuracyTextView);
addressTextView = (TextView)findViewById(R.id.addressTextView);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override public void onLocationChanged(Location location) {
updateLocation(location);
}
@Override public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override public void onProviderEnabled(String provider) {
}
@Override public void onProviderDisabled(String provider) {
}
};
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
}else{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER );
if(lastKnownLocation!=null){
updateLocation(lastKnownLocation);
}
}
}
public void startListening(){
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
}
}
public void updateLocation(Location location)
{
latitudeTextView.setText("Latitude : "+location.getLatitude());
longitudeTextView.setText("Longitude : "+location.getLongitude());
accuracyTextView.setText("Accuracy : "+location.getAccuracy());
String address = "Could not find address";
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try{
List<Address> listAddresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
if(listAddresses!=null && listAddresses.size()>0){
String result = "";
Log.i("address : ",listAddresses.get(0).toString());
if(listAddresses.get(0).getFeatureName()!=null)
{
result += listAddresses.get(0).getFeatureName();
result += "\r\n";
}
if(listAddresses.get(0).getAdminArea()!=null)
{
result += listAddresses.get(0).getAdminArea();
result += "\r\n";
}
if(listAddresses.get(0).getPostalCode()!=null)
{
result += listAddresses.get(0).getPostalCode();
result += "\r\n";
}
if(listAddresses.get(0).getCountryName()!=null)
{
result += listAddresses.get(0).getCountryName();
result += "\r\n";
}
address = result;
}
addressTextView.setText(address);
}catch (Exception e){
e.printStackTrace();
addressTextView.setText(address);
}
}
}
If You Enjoyed This, Take 5 Seconds To Share It
0 comments:
Post a Comment