GPS를 이용한 거리 계산 거리 android


GPS를 이용한 거리 계산 거리 android package com.ebookfrenzy.gps;

import android.Manifest; import android.annotation.SuppressLint; import android.content.pm.PackageManager; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; import android.os.Bundle; import android.content.Context; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Handler; import android.os.IBinder; import android.app.Service;

@SuppressLint("Registered") public class GpsService extends Service {

// @Override
// protected void onCreate(Bundle savedInstanceState) {
//     super.onCreate( savedInstanceState );
//setContentView( R.layout.activity_gps_service );
// }
public static final String BROADCAST_ACTION = "com.ebookfrenzy.gps.updateUI";
public LocationManager locationManager;
public MyLocationListener listener;




static Double lat1 = null;
static Double lon1 = null;
static Double lat2 = null;

static Double lon2 = null; static Double distance = 0.0; static int status = 0; Intent intent; private final Handler UIhandler = new Handler();

@Override
public void onCreate() {
    super.onCreate();
    intent = new Intent( BROADCAST_ACTION );
}

@Override
public void onStart(Intent intent, int startId) {

    UIhandler.removeCallbacks( sendUpdatesToUI );
    UIhandler.postDelayed( sendUpdatesToUI, 0 );
    locationManager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
    listener = new MyLocationListener();
    if (ActivityCompat.checkSelfPermission( this, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( this, 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;
    }
    locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 10000, 10, listener );
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, listener);
}
private Runnable sendUpdatesToUI = new Runnable() {
    public void run() {
        intent.putExtra("distance", distance+"");
        sendBroadcast(intent);
        UIhandler.postDelayed(this, 0);
    }
};


@Nullable
// @Overridereturn null;
public IBinder onBind(Intent intent) {
    return null;
}
@Override
public void onDestroy() {
    UIhandler.removeCallbacks(sendUpdatesToUI);
    locationManager.removeUpdates(listener);
    super.onDestroy();
}
public class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        if (status == 0) {
            lat1 = location.getLatitude();
            lon1 = location.getLongitude();
        } else if ((status % 2) != 0) {
            lat2 = location.getLatitude();
            lon2 = location.getLongitude();
            distance += distanceBetweenTwoPoint(lat1, lon1, lat2, lon2);
        } else if ((status % 2) == 0) {
            lat1 = location.getLatitude();
            lon1 = location.getLongitude();
            distance += distanceBetweenTwoPoint(lat2, lon2, lat1, lon1);
        }
        status++;
        UIhandler.postDelayed(sendUpdatesToUI, 0);

    }

double distanceBetweenTwoPoint(double lat1, double lon1, double lat2, double lon2) { double earthRadius = 6371; // Radius in Kilometers double dLat = Math.toRadians(lat2 - lat1); double dLng = 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(dLng / 2) * Math.sin(dLng / 2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); double dist = earthRadius * c;

double meterConversion = 1609;

return (int) (dist * meterConversion);

}

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}

}

  • 2018년 04월 20일에 수정됨
  • 알 수 없는 사용자

조회수 43


Hashcode banner summercoding

로그인이 필요한 기능입니다.

Hashcode는 개발자들을 위한 무료 QnA사이트 입니다. 작성한 답변에 다른 개발자들이 댓글을 작성하거나 좋아요/싫어요를 할 수 있기 때문에 계정을 필요로 합니다.
► 로그인
► 계정만들기
Close