나는 울부 짖는 코드를 사용자에게 표시하는 목적지까지 남은 거리를 계산 보여주기 위해 노력하고 있어요 :는 대상 프로그램 안드로이드 구글 맵 API v2로 남은 거리를 계산?
public class MyService extends Service {
private LocationManager locManager;
private LocationListener locListener = new myLocationListener();
static final Double EARTH_RADIUS = 6371.00;
private boolean gps_enabled = false;
private boolean network_enabled = false;
private Handler handler = new Handler();
Thread t;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
}
@Override
public void onDestroy() {
}
@Override
public void onStart(Intent intent, int startid) {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getBaseContext(), "Service Started", Toast.LENGTH_SHORT).show();
final Runnable r = new Runnable() {
public void run() {
Log.v("Debug", "Hello");
location();
handler.postDelayed(this, 5000);
}
};
handler.postDelayed(r, 5000);
return START_STICKY;
}
public void location() {
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try {
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
Log.v("Debug", "in on create.. 2");
if (gps_enabled) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), android.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;
}
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
Log.v("Debug", "Enabled..");
}
if (network_enabled) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
Log.v("Debug", "Disabled..");
}
Log.v("Debug", "in on create..3");
}
private class myLocationListener implements LocationListener {
double lat_old = 34.629678;
double lon_old = 50.860293;
double lat_new;
double lon_new;
double time = 10;
double speed = 0.0;
@Override
public void onLocationChanged(Location location) {
Log.v("Debug", "in onLocation changed..");
if (location != null) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locManager.removeUpdates(locListener);
lat_new = location.getLongitude();
lon_new = location.getLatitude();
String longitude = "Longitude: " + location.getLongitude();
String latitude = "Latitude: " + location.getLatitude();
double distance = CalculationByDistance(lat_new, lon_new, lat_old, lon_old);
Toast.makeText(getApplicationContext(), longitude + "\n" + latitude + "\nDistance is: "
+ distance , Toast.LENGTH_SHORT).show();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
public double CalculationByDistance(double lat1, double lon1, double lat2, double lon2) {
double Radius = EARTH_RADIUS;
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.asin(Math.sqrt(a));
return Radius * c;
}
}
문제 :
1 distance
의 값은 변경할 수 없습니다!
2 내가 중지하고 때 나는 5000
당 토스트 참조 : 나는 onLocationChanged
를 사용하고 있기 때문에
Toast.makeText(getApplicationContext(), longitude + "\n" + latitude + "\nDistance is: " + distance , Toast.LENGTH_SHORT).show();
나는 토스트를 볼 수 없습니다를! 나는 중지하고 때
나는 어떤 토스트를 볼 수 없습니다.
당신이 location.distanceTo (locaiton)를 시도 해 봤나
distanceTo
방법은? 위치 클래스에 내장 된 함수입니다. 질문 2 –