#android #mapview #fragment #mapfragment
Fragment에서 Mapview를 띄웠습니다.
구글맵 API는 불러왔다는 전제하에 시작합니다.
//실행화면
//소스코드
LocationFragment.java
package com.google.fuck; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.ContentView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMapOptions; import com.google.android.gms.maps.MapView; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import com.google.fuck.R; public class LocationFragment extends Fragment implements OnMapReadyCallback, GoogleMap.OnInfoWindowClickListener{ MapView sView = null; public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_stu_location , container, false); //mapview 부름 sView = view.findViewById(R.id.s_map); sView.onCreate(savedInstanceState); sView.getMapAsync(this); return view; } //이 메서드가 없으면 지도가 보이지 않음 @Override public void onStart() { super.onStart(); sView.onStart(); } @Override public void onStop () { super.onStop(); sView.onStop(); } @Override public void onSaveInstanceState (@Nullable Bundle outState){ super.onSaveInstanceState(outState); sView.onSaveInstanceState(outState); } @Override public void onResume() { super.onResume(); sView.onResume(); } @Override public void onDestroy() { super.onDestroy(); sView.onLowMemory(); } //맵뷰 설정 @Override public void onMapReady(GoogleMap googleMap) { //마커찍기(위도,경도) LatLng solnae = new LatLng(35.869253, 127.129006); //마커 옵션 MarkerOptions marker = new MarkerOptions(); marker.position(solnae); //마커 위치 marker.title("솔내청소년수련관"); marker.snippet("전주시 덕진구 송천1동 동부대로 1079"); //맵에 마커표시, 인포윈도우 보여줌 googleMap.addMarker(marker).showInfoWindow(); //인포윈도우 클릭 googleMap.setOnInfoWindowClickListener(this); //맵뷰 카메라위치, 줌 설정 googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(solnae, 13)); } //인포윈도우 클릭 리스너 @Override public void onInfoWindowClick (Marker marker){ } }
fragment_Loaction.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.google.android.gms.maps.MapView android:id="@+id/s_map" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="100" /> </LinearLayout>
'Programming' 카테고리의 다른 글
[Android Studio] 날짜, 시간 지정 후 Notification(푸시 알림) 받기 (0) | 2020.05.27 |
---|---|
[Java Script] 반복 횟수 지정 후 간단한 연산 반복 (0) | 2019.12.18 |