on
Android App Study_24 (googleMapApplication (현재 위치, marker 새로...
Android App Study_24 (googleMapApplication (현재 위치, marker 새로...
package com.example.googlemapapplication;
import ...
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback { //implements 중요
private FragmentManager fragmentManager; //googleMap을 띄우기 위한 것
private MapFragment mapFragment;
private Thread thread;
private LocationManager locationManager; //현위치를 가져오기 위한 것
Location location;
double lattitude;
double longitude;
private Boolean isFine = false ;
private RelativeLayout rootLayout; //여러 view들을 연결해주기 위한 것
private LinearLayout linearLayout;
private Button btn_toMyLocation;
private EditText et_name;
private Button btnForApply;
private boolean isForApply = true ; //btnForApply를 위한 boolean
private LatLng latLngForMarker; //marker을 만들 때 사용할 것
GoogleMap map; //googleMap에 대한 것들을 저장할 것
private boolean onMarker = true ; //현위치에 시점을 고정하는 것에 대한 boolean
Marker marker;
Marker markerForRetouch;
private final static String TAG = "MainActivity.java" ;
private final static int UPDATELOCATION = 0 ;
private int mpHeight; //linearLayout을 움직이게 하기 위한 변수
private int lastY, curY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TedPermission.with( this ) //권한을 얻기 위한 코드이다.
.setPermissionListener(permission)
.setRationaleMessage( "위치 확인를 위하여 권한을 허용해주세요." )
.setDeniedMessage( "권한이 거부되었습니다. 설정 > 권한에서 허용할 수 있습니다." )
.setPermissions(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)
.check();
fragmentManager = getFragmentManager();
mapFragment = (MapFragment) fragmentManager.findFragmentById(R.id.googleMap);
mapFragment.getMapAsync( this ); //googleMap을 사용하기 위한 것
rootLayout = (RelativeLayout)findViewById(R.id.rootLayout);
linearLayout = (LinearLayout)findViewById(R.id.linearLayout);
et_name = (EditText)findViewById(R.id.et_name);
btnForApply = (Button)findViewById(R.id.btnForApply);
btnForApply.setOnClickListener(v - > {
if (isForApply) {
if (et_name.getText(). toString () = = "" ) { //입력된 것이 없을 때의 부분
Toast.makeText(getApplicationContext(), "이름을 입력해주세요." , Toast.LENGTH_SHORT).show();
} else {
MarkerOptions markerOptions = new MarkerOptions(); //marker를 등록하기 위한 부분
markerOptions.title(et_name.getText(). toString ());
markerOptions.position(latLngForMarker);
map.addMarker(markerOptions);
isForApply = false ;
btnForApply.setText( "수정" );
}
} else { //marker을 수정하기 위한 부분
markerForRetouch.setTitle(et_name.getText(). toString ());
}
});
btn_toMyLocation = (Button)findViewById(R.id.btn_toMyLocation);
btn_toMyLocation.setOnClickListener(v - > { //버튼으로 현위치로 시점을 움직이기 위한 부분
onMarker = true ; //시점을 현위치에 고정
btn_toMyLocation.setVisibility(View.INVISIBLE); //버튼을 보이지 않게
Log.d(TAG, ( "onClickListener : onMarker = " + onMarker));
LatLng location = new LatLng(lattitude, longitude);
map.moveCamera(CameraUpdateFactory.newLatLng(location));
});
thread = new Thread() { //계속 위치를 받아오기 위한 thread
@SuppressLint( "MissingPermission" )
@Override
public void run() {
while ( true ) {
try {
sleep( 1000 );
} catch (InterruptedException e) {
e.printStackTrace();
}
if (isFine) { //관련 허가를 받았는지 확인해주는 부분
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
lattitude = location.getLatitude(); //gps로 얻은 위치로 좌표를 받아와서 저장한다.
longitude = location.getLongitude();
}
handler.sendEmptyMessage(UPDATELOCATION); // mainThread에서 실행해야할 부분을 실행
Log.d(TAG, "run : threadIsRunning" );
}
}
};
thread.start();
linearLayout.setOnTouchListener( new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) { // linearLayout을 드레그로 움직일 수 있게 한다.
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) linearLayout.getLayoutParams();
int action = event.getAction();
if (action = = MotionEvent.ACTION_DOWN){
lastY = ( int )event.getY() + layoutParams.topMargin;
} else if (action = = MotionEvent.ACTION_MOVE & & Math.abs(lastY - layoutParams.topMargin) < 100 & & lastY < (mpHeight - 200 ) & & lastY > 100 ) {
curY = ( int )event.getY() + layoutParams.topMargin;
layoutParams.setMargins( 0 , (layoutParams.topMargin + curY - lastY), 0 , 0 );
linearLayout.setLayoutParams(layoutParams);
lastY = curY;
}
return true ;
}
});
}
@Override
public void onMapReady(@NonNull @org.jetbrains.annotations.NotNull GoogleMap googleMap) {
LatLng location = new LatLng( 0 , 0 ); //지도에 marker을 만들기 위한 좌표
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.title( "현위치" );
markerOptions.draggable( true );
markerOptions.position(location);
marker = googleMap.addMarker(markerOptions); //위에서 설정한 marker을 적용
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 16 )); //marker 위치로 camera를 이동하고 줌함
googleMap.setOnCameraMoveListener( new GoogleMap.OnCameraMoveListener() {
@Override
public void onCameraMove() {
Log.d(TAG, "onCameraMove : TouchLinstenerIsRunning" );
if (Math.abs(map.getCameraPosition().target.latitude - lattitude) < 0. 0005 & & Math.abs(map.getCameraPosition().target.longitude - longitude) < 0. 0005 ) {
onMarker = true ; //시점이 현위치에 주변에 가면 고정한다.
btn_toMyLocation.setVisibility(View.INVISIBLE);
Log.d(TAG, ( "onCameraMove : onMarker = " + onMarker));
} else {
onMarker = false ; //시점이 현위치를 벗어나면 고정을 푼다.
btn_toMyLocation.setVisibility(View.VISIBLE);
Log.d(TAG, ( "onCameraMove : onMarker = " + onMarker));
}
}
});
googleMap.setOnMarkerClickListener( new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
if (marker.getTitle(). toString () ! = "현위치" ){ //marker을 클릭했을 때 그 marker을 수정할 수 있게 준비한다.
markerForRetouch = marker;
et_name.setText(marker.getTitle());
} else {
onMarker = true ;
btn_toMyLocation.setVisibility(View.INVISIBLE);
}
googleMap.moveCamera(CameraUpdateFactory.newLatLng( new LatLng(marker.getPosition().latitude, marker.getPosition().longitude)));
return true ;
}
});
googleMap.setOnMapLongClickListener( new GoogleMap.OnMapLongClickListener() { //오래 터치했을 때 marker을 생성할 준비를 한다.
@Override
public void onMapLongClick(LatLng latLng) {
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) linearLayout.getLayoutParams();
isForApply = true ;
btnForApply.setText( "생성" );
layoutParams.setMargins( 0 , mpHeight / 2 + 100 , 0 , 0 ); //정보를 입력받기 위한 linearLayout을 적당한 높이로 띄운다.
linearLayout.setLayoutParams(layoutParams);
btn_toMyLocation.setVisibility(View.VISIBLE);
onMarker = false ;
latLngForMarker = latLng;
}
});
map = googleMap;
}
PermissionListener permission = new PermissionListener() {
@Override
public void onPermissionGranted() {
Toast.makeText(MainActivity. this , "권한 허가" , Toast.LENGTH_SHORT);
locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
isFine = true ; //허가를 받았을 때 그 정보를 저장
}
@Override
public void onPermissionDenied(ArrayList < String > deniedPermissions) {
Toast.makeText(MainActivity. this , "권한 거부" , Toast.LENGTH_SHORT);
}
};
private Handler handler = new Handler() { //thread에서 사용될 부분을 정의
@Override
public void handleMessage(@NonNull Message msg) {
if (msg.what = = UPDATELOCATION){
LatLng location = new LatLng(lattitude, longitude);
marker.setPosition(location);
Log.d(TAG, ( "handleMessage: onMarker = " + onMarker));
if (onMarker) map.moveCamera(CameraUpdateFactory.newLatLng(location)); //현위치에 시점이 고정되어있을 때 시점을 marker로 이동
}
}
};
@Override
public void onWindowFocusChanged( boolean hasFocus) {
super .onWindowFocusChanged(hasFocus);
mpHeight = rootLayout.getHeight(); //현재 화면의 세로 방향의 px를 저장함
}
}
from http://yunseong.tistory.com/47 by ccl(A) rewrite - 2021-09-24 01:27:42