MapView inside ScrollView (fragment)
프래그멘트 안에 있는 ScrollView 에 맵뷰 달기
fragment_maps.xml 맵뷰의 레이아웃을 정의 합니다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MapInFragment.java뷰의 클레스를 정의합니다.
public class MapInFragment extends Fragment {
MapView mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_maps, container, false);
mMap = (MapView) v.findViewById(R.id.mapView);
mMap.onCreate(savedInstanceState);
mMap.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
boolean permission = hasAllPermissionsGranted();
if (permission) {
Log.e("test","aaaa");
}
return;
}
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.setMyLocationEnabled(true);
try {
MapsInitializer.initialize(getActivity());
} catch (Exception e) {
}
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.488, 126.891), 15));
}
});
return v;
}
@Override
public void onResume() {
mMap.onResume();
super.onResume();
}
@Override
public void onPause() {
super.onPause();
mMap.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMap.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMap.onLowMemory();
}
//권한 추가
private static final int REQUEST_PERMISSIONS = 1;
private static final String[] MY_PERMISSIONS = {
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
};
private boolean hasAllPermissionsGranted() {
for (String permission : MY_PERMISSIONS) {
if (ContextCompat.checkSelfPermission(getActivity(), permission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), MY_PERMISSIONS, REQUEST_PERMISSIONS);
return false;
}
}
return true;
}
}
코드 적용 (7.0 버전으로 만들어서 시스템 permission 권한 코드 포함)
Activity layout 에서 ScrollView 안에 정의합니다.
<ScrollView
...
<FrameLayout android:id="@+id/map_container"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp"/>
...
</ScrollView>
Fragment 내에서의 처리
public class AddMemoFragment extends Fragment {
FrameLayout map_container;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_add, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
map_container = (FrameLayout) view.findViewById(R.id.map_container) ;
MapInFragment map = new MapInFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.map_container, map).commit();
}
}
결과 화면