package com.ghj.floatingbtnex;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
//GoogleMap 객체
GoogleMap googleMap;
LocationManager locationManager;
RelativeLayout boxMap;
//나의 위도 경도 고도
double mLatitude; //위도
double mLongitude; //경도
FloatingActionButton btnFAB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boxMap = (RelativeLayout)findViewById(R.id.boxMap);
btnFAB = (FloatingActionButton)findViewById(R.id.btnFAB);
btnFAB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Click...", Toast.LENGTH_SHORT).show();
}
});
//LocationManager
locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
mLatitude = 37.5197889;
mLongitude = 126.9403083;
//GPS가 켜져있는지 체크
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
//GPS 설정화면으로 이동
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
finish();
}
//마시멜로 이상이면 권한 요청하기
if(Build.VERSION.SDK_INT >= 23){
//권한이 없는 경우
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION , Manifest.permission.ACCESS_FINE_LOCATION} , 1);
}
//권한이 있는 경우
else{
createMap();
}
}
//마시멜로 아래
else{
createMap();
}
}
//권한 요청후 응답 콜백
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//ACCESS_COARSE_LOCATION 권한
if(requestCode==1){
//권한받음
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
createMap();
}
//권한못받음
else{
Toast.makeText(this, "권한없음", Toast.LENGTH_SHORT).show();
finish();
}
}
}
//맵생성
public void createMap(){
//맵생성
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
//콜백클래스 설정
mapFragment.getMapAsync(MainActivity.this);
}
//구글맵 생성 콜백
@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
//지도타입 - 일반
this.googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//나의 위치 설정
LatLng position = new LatLng(mLatitude , mLongitude);
//화면중앙의 위치와 카메라 줌비율
this.googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15));
}
}