Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

에이치의 모바일 앱 개발

내 위치 정보 가져오기<기본> 본문

Android/Android 개발 소스

내 위치 정보 가져오기<기본>

로이누리 2017. 12. 23. 01:14
 

/**
* Created by lsh on 2016-08-12.
* version 1.0.1
*/
public class BaseLocation extends Service implements LocationListener {
// 단위 second
private static final int LOCATION_UPDATE_CYCLE_MIN_TIME = 1000 * 60;
// 단위 meter
private static final int LOCATION_UPDATE_MIN_DISTANCE = 10;

private double lat, lon;
private boolean isGPSEnabled, isNetworkEnabled, isLocationResult;
private Context context;
private Location location;
private LocationManager locationManager;

public BaseLocation(Context context) {
this.context = context;
getLocation();
}

public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (!isGPSEnabled && !isNetworkEnabled) {
this.isLocationResult = false;
} else {
this.isLocationResult = true;

if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_UPDATE_CYCLE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
setLatLon(location.getLatitude(), location.getLongitude());
}
}
}

if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_UPDATE_CYCLE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
setLatLon(location.getLatitude(), location.getLongitude());
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}

return location;
}

public void setLatLon(double lat, double lon) {
Log.e("Test", "lat : " + String.valueOf(lat) + ", lon : " + String.valueOf(lon));
this.lat = lat;
this.lon = lon;
}

public double getLatitude() {
if (location != null) {
lat = location.getLatitude();
}
return lat;
}

public double getLongitude() {
if (location != null) {
lon = location.getLongitude();
}
return lon;
}

public boolean isLocationResult() {
return isLocationResult;
}

/**

* GPS 정보 획득 실패 시 설정창 이동 확인 창

*/

public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);

alertDialog.setTitle("GPS 사용유무셋팅");
alertDialog.setMessage("GPS 셋팅을 위해 설정창으로 가시겠습니까?");

alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});

alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {

}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

Comments