首页 > 解决方案 > 我应该在哪里调用我的更新地图函数?我需要在我的异步任务之后调用它?

问题描述

嗨,我是 Android 编程的新手,我有一个应用程序,当用户点击一个国家/地区时,它会启动另一个活动,显示带有定位首都城市的标记的地图。

但是我需要从我的异步任务方法中获取资金。所以它需要返回国家信息,然后我可以更新我的地图。我在 onPost Execute 中调用了我的更新地图方法,但应用程序只是崩溃了?

在我的创建

 mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync( this);


    new GetCountryDetails().execute(selectedCountry);

然后在我的异步任务中

private class GetCountryDetails extends AsyncTask<String, String, CountryInfo> {

    @Override
    protected CountryInfo doInBackground(String... params) {
       CountryInfo countryInfo = null;

在我的 onPostExecute

protected void onPostExecute(CountryInfo countryInfo) {
    countryCapital=countryInfo.getCapital();

    updateMap(map);//doesn't work here

onMapReady:

public void onMapReady(GoogleMap map) {
    map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    map.getUiSettings().setMapToolbarEnabled(true);

更新地图

         public void updateMap(GoogleMap map){
    LatLng capitalCity = getCityLatitude(this, countryCapital);
       map.moveCamera(CameraUpdateFactory.newLatLng(capitalCity));

        MarkerOptions marker = new MarkerOptions().position(capitalCity).title( countryCapital);
        map.addMarker(marker);

    }

任何帮助将不胜感激谢谢!

一世

标签: javaandroidgoogle-mapsandroid-asynctask

解决方案


Create Map varible as activity field.And define variable in onMapReady function.After that use activity variable as you want. I think it should works.

GoogleMap map;

public void onMapReady(GoogleMap map) {

    this.map = map;
    map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    map.getUiSettings().setMapToolbarEnabled(true);
}

推荐阅读