繁体   English   中英

在 Android Studio [Java] 中加载 mapbox map 后的绘图标记

[英]Drawing markers after mapbox map loads in Android Studio [Java]

这是我在 Android Studio 中的第一个项目,基本上我正在尝试使用 Mapbox 开发具有多个标记的 map。 所以,我的问题是,当在 map 上加载标记时,加载大约需要 3-5 秒,并且应用程序冻结,直到我从 API 调用中获得 json。 这是我对 API 的改造 2 调用:

private void getNearbyStations() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("***")//my API, not relevant
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
        Utilizator utilizator = Utilizator.getUtilizatorInstance();
        Call<ResponseNearbyStations> call = jsonPlaceHolderApi.getNearbyStations(utilizator.getAuthentificationKey(), 47.1744354, 27.5746688);//Static Lat and Long for test, in future will use current location
        try {
            ResponseNearbyStations body = call.execute().body();
            JsonObject jsonObject = body.getData();
                    JsonArray ja_data = jsonObject.getAsJsonArray("stationAround");
                    Station[] statiiPrimite = gson.fromJson(ja_data, Station[].class);
                    stationList = new ArrayList<>(Arrays.asList(statiiPrimite));
        } catch (IOException e) {
            e.printStackTrace();
        }
}

我将所有站保存在名为 stationList 的 ArrayList 中。 在车站 class 除了其他信息外,我还有纬度和经度坐标。

这是我的 addMarkers function:

    private void addMarkers(@NonNull Style loadedMapStyle) {
        List<Feature> features = new ArrayList<>();

        for(Station statie:stationList){    
 features.add(Feature.fromGeometry(Point.fromLngLat(Double.valueOf(statie.getCoordinates().getLongitude()),
Double.valueOf(statie.getCoordinates().getLatitude()))));
        }

        loadedMapStyle.addSource(new GeoJsonSource(MARKER_SOURCE, FeatureCollection.fromFeatures(features)));

        loadedMapStyle.addLayer(new SymbolLayer(MARKER_STYLE_LAYER, MARKER_SOURCE)
                .withProperties(
                        PropertyFactory.iconAllowOverlap(true),
                        PropertyFactory.iconIgnorePlacement(true),
                        PropertyFactory.iconImage(MARKER_IMAGE),
                        PropertyFactory.iconOffset(new Float[]{0f, -52f})
                ));
    }

所以经过几次搜索后,我发现这里的“问题”是我在getNearbyStations() 中使用 call.execute()不是异步的,所以主线程正在等待 Stations 加载。 我尝试使用call.enqueue但之后我遇到了另一个问题,在我的 function addMarkers中我得到 NullPointerException 因为 stationList 没有足够的时间加载

for(Station statie:stationList){    
 features.add(Feature.fromGeometry(Point.fromLngLat(Double.valueOf(statie.getCoordinates().getLongitude()),
Double.valueOf(statie.getCoordinates().getLatitude()))));
        }

我猜我必须使用某种线程来解决这个问题,但我是在 Android Studio 中使用线程的初学者,我想不通。 我认为解决方案是:

1.显示map为空

2.加载后添加标记。

以这种方式,用户不会经历任何冻结。 欢迎任何想法如何解决这个问题。

由于问题是您希望应用程序不要等待同步 function,因此我建议为此使用异步任务。 然后,您可以在调用异步任务的onPostExecute回调后执行addMarkers function。 但请确保仅在您的addMarkers中设置样式后运行onMapReady

请参阅此文档以了解如何使用异步任务: https://developer.android.com/reference/android/os/AsyncTask

使用异步任务获得的好处是 Android 将在不同的线程中执行它,从而减轻主线程的负载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM