public class MainActivity extends AppCompatActivity {
final int MY_PERMISSION_REQUEST_GPS = 1001;
Button button;
Button button2;
Button button3;
EditText editText;
TextView textView;
TextView textView2;
TextView textView3;
Location myLocation;
Location tarLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tarLocation = new Location("Target location");
myLocation = new Location("My location");
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("info");
builder.setMessage("This app won't work properly unless you grant GPS permission.");
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_REQUEST_GPS);
}
});
AlertDialog dialog = builder.create();
dialog.show();
} else {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_REQUEST_GPS);
}
}
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startLocationService();
}
});
final Geocoder geocoder = new Geocoder(this);
editText = findViewById(R.id.editText);
textView2 = findViewById(R.id.textView2);
button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String search = editText.getText().toString();
List<Address> addressList;
try {
addressList = geocoder.getFromLocationName(search, 10);
String[] split = addressList.get(0).toString().split(",");
String address = split[0].substring(split[0].indexOf("\"") + 1, split[0].length() - 2);
String latitude = split[10].substring(split[10].indexOf("=") + 1); // 위도(수평선)
String longitude = split[12].substring(split[12].indexOf("=") + 1); // 경도(수직선)
String data = "Address: " + address + "\n" + "Latitude: " + latitude + "\n" + "Longitude: " + longitude;
textView2.setText(data);
tarLocation.setLatitude(Double.parseDouble(latitude));
tarLocation.setLongitude(Double.parseDouble(longitude));
} catch (Exception e) {
e.printStackTrace();
}
}
});
textView3 = findViewById(R.id.textView3);
button3 = findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float[] distance = new float[1];
Location.distanceBetween(myLocation.getLatitude(), myLocation.getLongitude(), tarLocation.getLatitude(), tarLocation.getLongitude(), distance);
textView3.setText("Distance: " + distance[0] / 1000.0f + "km");
//textView3.setText("Distance: " + String.valueOf(myLocation.distanceTo(tarLocation) / 1000.0f) + "km");
}
});
}
public void startLocationService() {
LocationManager manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
try {
Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
myLocation.setLatitude(latitude);
myLocation.setLongitude(longitude);
String message = "My last location\nLatitude: " + latitude + "\nLongitude: " + longitude;
Log.d("App", "Last location request");
textView.setText(message);
} else
{
Toast.makeText(this, "location is null", Toast.LENGTH_SHORT).show();
}
GPSListener gpsListener = new GPSListener();
long minTime = 3000;
float minDistance = 0;
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, gpsListener);
Log.d("App", "Current location request");
}catch (SecurityException e) {
e.printStackTrace();
}
}
class GPSListener implements LocationListener {
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
String message = "My current location\nLatitude: " + latitude + "\nLongitude: " + longitude;
textView.setText(message);
myLocation.setLatitude(latitude);
myLocation.setLongitude(longitude);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSION_REQUEST_GPS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this,"Permission granted.",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,"Permission denied.",Toast.LENGTH_SHORT).show();
}
}
}
}
}