MapView没有';t在Android mvvm数据绑定中显示谷歌地图[已解决]

MapView没有';t在Android mvvm数据绑定中显示谷歌地图[已解决],android,mvvm,google-maps-android-api-2,android-mapview,android-databinding,Android,Mvvm,Google Maps Android Api 2,Android Mapview,Android Databinding,我正在尝试使用带有数据绑定的mapView设置google地图,并在地图上设置一些标记。当我硬编码“latlngs”时,我的地图工作正常,但当我尝试设置从服务器获取的“latlngs”时,地图不会显示。标记已显示,数据和设置标记没有问题,只有地图不起作用。谷歌的标志就在那里,以防你们怀疑我的API密钥是否有效。这是我的密码 这是碎片 private MapView google_map; @Override public void onCreate(Bundle savedInstanceSt

我正在尝试使用带有数据绑定的mapView设置google地图,并在地图上设置一些标记。当我硬编码“latlngs”时,我的地图工作正常,但当我尝试设置从服务器获取的“latlngs”时,地图不会显示。标记已显示,数据和设置标记没有问题,只有地图不起作用。谷歌的标志就在那里,以防你们怀疑我的API密钥是否有效。这是我的密码

这是碎片

private MapView google_map;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    FragmentMapBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_map, container, false);
    MapFragmentViewModel viewModel = new ViewModelProvider(this, new MapFragmentViewModelFactory()).get(MapFragmentViewModel.class);

    binding.setLifecycleOwner(this);
    binding.setViewModel(viewModel);
    
    google_map = binding.googleMap;

    viewModel.getLocations();
    viewModel.locationsResponse().observe(getViewLifecycleOwner(), apiResponseObject -> {
        switch (apiResponseObject.status) {
            case SUCCESS:
                ViewUtils.hideProgressDialog();
                break;
            case LOADING:
                ViewUtils.showProgressDialog(requireContext());
                break;
            case ERROR:
                ViewUtils.hideProgressDialog();
                ViewUtils.showToastMessage(requireContext(), apiResponseObject.error);
        }
    });

    setTypeFace(binding.getRoot(), requireContext());
    languageSetup(requireContext());
    return binding.getRoot();
}

@Override
public void onResume() {
    super.onResume();
    google_map.onResume();
}

@Override
public void onPause() {
    super.onPause();
    google_map.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    google_map.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    google_map.onLowMemory();
}

@Override
public void onStop() {
    super.onStop();
    google_map.onStop();
}
这是我的viewModel:

private MutableLiveData<ApiResponseObject> responseLiveData = new MutableLiveData<>();
private CompositeDisposable disposables;
private ApiCallsRepository apiCallsRepository;

public MapFragmentViewModel() {
    apiCallsRepository = new ApiCallsRepository();
    disposables = new CompositeDisposable();

}

void getLocations() {

    disposables.add(apiCallsRepository.getLocations(SharedPrefUtils.getUserId())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe(disposable -> responseLiveData.setValue(ApiResponseObject.loading()))
            .subscribe(this::setLocations, this::handleError));

}

LiveData<ApiResponseObject> locationsResponse() {
    return responseLiveData;
}

public MutableLiveData<List<LatLng>> locations = new MutableLiveData<>();

@BindingAdapter("initMap")
public static void initMap(final MapView mapView, final List<LatLng> locations) {

    if (mapView != null) {
        mapView.onCreate(new Bundle());
        mapView.getMapAsync(googleMap -> {
            if (locations != null) {
                for (LatLng latLng : locations)
                    googleMap.addMarker(new MarkerOptions().position(latLng));
            }
        });
    }
}

public void setLocations(List<LocationsResponse> mLocationsResponses) {

    List<LatLng> latLngs = new ArrayList<>();

    for (LocationsResponse locationsResponse : mLocationsResponses)
            latLngs.add(new LatLng(Double.parseDouble(locationsResponse.getLongitude()), 
                    Double.parseDouble(locationsResponse.getLatitude())));

    locations.setValue(latLngs);
    responseLiveData.setValue(ApiResponseObject.success(null));
}

private void handleError(Throwable throwable) {
    if (throwable instanceof HttpException) {
        HttpException httpException = (HttpException) throwable;
        Response<?> response = httpException.response();
        responseLiveData.setValue(ApiResponseObject.error(getErrorMessage(response)));
    } else if (throwable instanceof IOException) {
        responseLiveData.setValue(ApiResponseObject.error(UNEXPECTED_ERROR));
    } else {
        responseLiveData.setValue(ApiResponseObject.error(throwable.getMessage()));
    }
}
我找到了解决方案

问题是在响应调用时再次设置了“位置”mutableLiveData,这会再次调用getMapAsync,我需要在该方法中添加mapView.onResume

@BindingAdapter("initMap")
public static void initMap(final MapView mapView, final List<LatLng> locations) {

    if (mapView != null) {
        mapView.onCreate(new Bundle());
        mapView.getMapAsync(googleMap -> {
            if (locations != null) {
                mapView.onResume();
                for (LatLng latLng : locations)
                    googleMap.addMarker(new MarkerOptions().position(latLng));
            }
        });
    }
}
@BindingAdapter(“initMap”)
公共静态void initMap(最终地图视图地图视图,最终列表位置){
如果(地图视图!=null){
onCreate(newbundle());
getMapAsync(谷歌地图->{
如果(位置!=null){
onResume();
用于(板条板条:位置)
addMarker(新的MarkerOptions().position(latLng));
}
});
}
}

您需要在片段中加载贴图,而不是在ViewModel中。将此代码移动到片段的onViewCreated

mapView.getMapAsync(googleMap -> {
        if (locations != null) {
            for (LatLng latLng : locations)
                googleMap.addMarker(new MarkerOptions().position(latLng));
        }
    });

您需要在片段中加载贴图,而不是在ViewModel中。将此代码移动到片段的onViewCreated

mapView.getMapAsync(googleMap -> {
        if (locations != null) {
            for (LatLng latLng : locations)
                googleMap.addMarker(new MarkerOptions().position(latLng));
        }
    });

Tnx的答复。这不是我加载地图的问题,我已经找到了解决方案。我编辑了答案,所以如果你感兴趣,请检查一下。@VladaMomirovic Good。但我认为getMapAsync回调应该是getLocations调用的起点,这对于回答来说更清楚。这不是我加载地图的问题,我已经找到了解决方案。我编辑了答案,所以如果你感兴趣,请检查一下。@VladaMomirovic Good。但我认为getMapAsync回调应该是getLocations调用的起点,这是一种更清晰的方法
mapView.getMapAsync(googleMap -> {
        if (locations != null) {
            for (LatLng latLng : locations)
                googleMap.addMarker(new MarkerOptions().position(latLng));
        }
    });