본문 바로가기

카테고리 없음

안드로이드 google map api를 구현할 때 mapView에서 googleMap객체에 접근하는 방법

안드로이드 앱을 개발하다보면 api는 거의 필수적으로 사용된다. 

그 중에서도 기본적인 지도 api의 사용법을 익히려한다.

 

구글 지도의 api는 activity로 구현할 때는

xml코드 속에 fragment를 넣어 MapFragment 객체로 주로 사용한다.

 

그러나 이번에는 fragment 화면에서 구현을 하고자한다.

그러기 위해서는 mapView를 사용하는 것이 수월하다는 글을 보았고

mapView를 구현하다보니 mapView를 통해 googleMap 객체에 접근하는 법을 찾다가

정리하려고 한다.

 

답은 생각보다 쉬운 곳에 있었다. 

mapview를 구현하기 위해서는 OnMapReadyCallback 인터페이스를 구현해야한다.

그러면 override 함수로 onMapReady 함수를 오버라이딩하게 된다.

그 오버라이딩 함수의 매개변수로 

googleMap 객체가 넘어온다...

 

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
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.MarkerOptions;

public class GoogleMapFragment extends Fragment implements OnMapReadyCallback {
    private MainActivity mainActivity;
    private MapView mapView;
    private MarkerOptions markerOptions;


    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mainActivity = (MainActivity) getActivity();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mainActivity = null;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_googlemap, container, false);

        //ui find
        findViewByidFunction(rootView);

        //mapview 설정
        initMapview(savedInstanceState);

        //이벤트 등록

        return rootView;
    }

    //mapview 설정
    private void initMapview(Bundle savedInstanceState) {
        mapView.onCreate(savedInstanceState);
        mapView.onResume();
        mapView.getMapAsync(this);

    }

    //ui find
    private void findViewByidFunction(View rootView) {
        mapView = rootView.findViewById(R.id.mapView);

    }

    @Override
    public void onMapReady(final GoogleMap googleMap) {
        final LatLng SEOUL = new LatLng(37.56, 126.97);
        markerOptions = new MarkerOptions();
        markerOptions.position(SEOUL);
        markerOptions.title("서울");
        markerOptions.snippet("수도");
        googleMap.addMarker(markerOptions);
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SEOUL, 16));

        googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                //마커 지우고 클릭한 위치로 마커 옴기기위한 문장
                googleMap.clear();
                markerOptions.position(latLng);
                googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17));
                googleMap.addMarker(markerOptions);
            }
        });
    }
}

 

 

반응형