我正在Google地图位置api中实现自动完成建议代码。
我们正在使用OnMapReadyCallback
作为工具。
MapView.getMapAsync(this)
函数最初在onCreateView
中使用。但是现在我将在setupAutoCompleteFragment
中使用它。但是,在MapView.getMapAsync(this)
中,由于this
未被编译。可以使用什么?
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());
}
});
}
}
答案 0 :(得分:0)
在这种情况下,您可以使用Classname.this
。
例如:如果您的片段名称为HomeFragment
,则
HomeFragment.this
如果仍然无法执行,则可以覆盖onViewCreated()
函数并在mapView.getMapAsync(this)
内部调用onViewCreated()
如果需要,请检查此link
答案 1 :(得分:0)
您可以通过这种方式做得更好
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());
}
});
}
请不要忘记使用以下新功能签名更新onCreateView
:
@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;
}
答案 2 :(得分:0)
您是否尝试过使用mapview.getMapAsync(getActivity());而不是mapview.getMapAsync(this);