Created
June 19, 2020 01:06
-
-
Save godjooyoung/011ba07dfc3b7fa933de42f43aa3fb3d to your computer and use it in GitHub Desktop.
좌표값 가져오기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.location; | |
import android.annotation.SuppressLint; | |
import android.content.Context; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import com.pedro.library.AutoPermissions; | |
import com.pedro.library.AutoPermissionsListener; | |
public class MainActivity extends AppCompatActivity implements AutoPermissionsListener { | |
//0.전역변수 선언 | |
Button btn; | |
TextView textView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
AutoPermissions.Companion.loadAllPermissions(this,101); | |
//1.변수 초기화 | |
btn = findViewById(R.id.btn1); | |
textView = findViewById(R.id.text1); | |
//2.btn에 클릭이벤트를 걸었다. | |
btn.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
//클릭하면 해당 메소드가 실행된다. | |
startLocationService(); | |
} | |
}); | |
}//end on create | |
@SuppressLint("MissingPermission") | |
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(); | |
String message = "최근 위치: " + latitude + ", " + longitude; | |
textView.setText(message); | |
} | |
} catch(Exception e){ | |
e.printStackTrace(); | |
} | |
//5.지속적인 좌표업데이트 추가 | |
GPSListener gpsListner = new GPSListener(); | |
long minTime = 10000; //10초를 뜻함. | |
float minDistance = 0; //거리 상관없이 무조건 10초마다. 여기에 값을 추가하면 일정 만큼 거리가 병경이 일어나면 하란 소리임. | |
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, gpsListner); | |
Toast.makeText(getApplicationContext(), "내 위치 확인 요청함", Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
} | |
@Override | |
public void onDenied(int i, String[] strings) { | |
} | |
@Override | |
public void onGranted(int i, String[] strings) { | |
} | |
//4.지속적으로 위치 받아오기 | |
class GPSListener implements LocationListener { | |
@Override | |
public void onLocationChanged(Location location) { | |
double latitude = location.getLatitude(); | |
double longitude = location.getLongitude(); | |
String message = "최근 위치: " + latitude + ", " + longitude; | |
textView.setText(textView.getText() +"\n" + message) ; | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
} | |
} | |
}//class end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment