-1

I'm implementing the autocomplete suggestion code in Google map place api. We are using OnMapReadyCallback as an implement.

The MapView.getMapAsync(this) function was originally used in onCreateView. But now I'm going to use it in setupAutoCompleteFragment. However, in MapView.getMapAsync(this), it is not compiled due to this. What can be used?

public class googlemaptab extends Fragment implements OnMapReadyCallback {
    MapView mapview;
    Button kakaobutton;
    public static googlemaptab newInstance(){
        return new googlemaptab();
    }
    public googlemaptab() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_googlemaptab, container, false);
        kakaobutton = (Button)view.findViewById(R.id.kakaobutton);

        mapview = (MapView)view.findViewById(R.id.google_map_view);
        setupAutoCompleteFragment();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        kakaobutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //클릭하면 카카오maptab으로 이동하겠다.
                ((MainActivity)getActivity()).replaceFragment(kakaomaptab.newInstance());
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        mapview.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapview.onResume();
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if(mapview != null)
        {
            mapview.onCreate(savedInstanceState);
        }

    }

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

    private void setupAutoCompleteFragment() {

        PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)getActivity().
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                mapview.getMapAsync(this);
            }

            @Override
            public void onError(Status status) {
                Log.e("Error", status.getStatusMessage());
            }
        });
    }
}
Hülya
  • 3,353
  • 2
  • 12
  • 19
ᄏ112222
  • 11
  • 1

3 Answers3

0

You can use Classname.this in this case.

For example: if your fragment name is HomeFragment then,

HomeFragment.this

If this still doesn't work then you can override onViewCreated() function and call mapView.getMapAsync(this) inside onViewCreated()

If needed check this link

Saugat Jonchhen
  • 356
  • 5
  • 16
0

You could do it better this way

private void setupAutoCompleteFragment(OnMapReadyCallback instance) {

        PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)getActivity().
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                mapview.getMapAsync(instance);
            }

            @Override
            public void onError(Status status) {
                Log.e("Error", status.getStatusMessage());
            }
        });
    }

And don't forget to update your onCreateView with the new function signature as the following:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_googlemaptab, container, false);
        kakaobutton = (Button)view.findViewById(R.id.kakaobutton);

        mapview = (MapView)view.findViewById(R.id.google_map_view);
        setupAutoCompleteFragment(this);
        return view;
    }
AouledIssa
  • 2,528
  • 2
  • 22
  • 39
0

have you tried with mapview.getMapAsync(getActivity()); instead of mapview.getMapAsync(this);

mili2501
  • 452
  • 5
  • 9