【问题标题】:How to call Map methods in onPostExecute?如何在 onPostExecute 中调用 Map 方法?
【发布时间】:2020-12-25 09:26:18
【问题描述】:

我有以下情况,我有课:

public class MapFragment extends SupportMapFragment implements OnMapReadyCallback

其中包括以下方法:

private void createMarkers(LatLng latLng) {

    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);

    map.addMarker(markerOptions);
}

private LatLng getLatLngFromAddress(String address) {

    Geocoder geocoder = new Geocoder(getActivity());
    List<Address> addresses = new ArrayList<>();
    LatLng latLng = null;

    try {
        addresses = geocoder.getFromLocationName(address, 1);

        if(addresses == null) {
            return null;
        }

        Address location = addresses.get(0);
        latLng = new LatLng(location.getLatitude(), location.getLongitude());

    } catch (IOException e) {
        e.printStackTrace();
    }

    return latLng;
}

此时我有一个内部类,我在其中从 SQLite 下载 doInBackground 方法中的数据,并且在 onPostExecute 中我想在地图上放置标记。如何在 onPostExecute 中调用这两种方法(createMarkers 和 getLatLngFromAddress)?

private static class getDataTask extends AsyncTask<Void, Void, List<String>> {

    private String currentDateForRequest = "";
    private IRoutesStore database;
    private List<String> allAddresses = new ArrayList<>();

    public getDataTask(String currentDateForRequest, IRoutesStore database) {
        this.currentDateForRequest = currentDateForRequest;
        this.database = database;
    }

    @Override
    protected List<String> doInBackground(Void... voids) {
        
        //getData from database
    }

    @Override
    protected void onPostExecute(List<String> addresses) {

        for(String address: addresses) {
            createMarkers(getLatLngFromAddress(address)); //I can"t call this methods
        }
    }
}

【问题讨论】:

  • 您可以将MapFragment 作为参数传递给getDataTask 的构造函数,然后调用MapFragment 对象上的方法

标签: java android dictionary asynchronous inner-classes


【解决方案1】:

在全球范围内制作您的地图 并使用 this.map

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-16
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多