在Async任务中,在api调用中添加两次标记

时间:2017-09-22 20:18:23

标签: java android google-maps android-asynctask google-maps-markers

我正在实施谷歌地图应用程序,我正在对我的服务器进行api调用并接收当前位置的少数人或对象或任何你可以认为它的东西。在每次api调用时,每个对象的响应位置都会发生变化。收到回复后,我会根据收到的位置和ID添加标记。我能够添加标记,但我在标记的添加过程中我删除了在同一过程中添加的所有先前标记但不幸的是它正在工作但是为相同的id维护了两个标记实例。

这是api调用的线程调用

public class GetNearbyVehicle extends AsyncTask<String, Void, ArrayList<ActiveVehiclePosition>> {
static GoogleMap mMap;

static ArrayList<ActiveVehiclePosition> activeVehiclePositionArrayList;
static HashMap<ActiveVehiclePosition, Marker> activeVehiclePositionHashMap;
public static HashMap<ActiveVehiclePosition, Marker> getNearbyVehicle(GoogleMap mMap1, HashMap<ActiveVehiclePosition, Marker> oldVehiclePositionHashMap){
    String url= "some url";
    mMap= mMap1;
    if(oldVehiclePositionHashMap != null){
        for (Map.Entry<ActiveVehiclePosition, Marker> entry : oldVehiclePositionHashMap.entrySet()) {
            Marker m= entry.getValue();
            m.remove();
        }
    }
    new GetNearbyVehicle().execute(url);
    return activeVehiclePositionHashMap;
}
@Override
protected ArrayList<ActiveVehiclePosition> doInBackground(String... params) {
    String url= params[0];
    try {
        ArrayList<ActiveVehiclePosition> ar= new ArrayList<>();
        ar= JsonConverter.getNearByVehicle(url);
        if(ar != null){
            activeVehiclePositionArrayList= ar;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return activeVehiclePositionArrayList;
}

@Override
protected void onPostExecute(ArrayList<ActiveVehiclePosition> activeVehiclePositions) {
    super.onPostExecute(activeVehiclePositions);

    activeVehiclePositionHashMap= new HashMap<>();
    if(activeVehiclePositions != null){
        for(int i=0; i<activeVehiclePositions.size(); i++){
            MarkerOptions markerOptions= new MarkerOptions();
            markerOptions.position(activeVehiclePositions.get(i).getLatLng());
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
            markerOptions.title(activeVehiclePositions.get(i).getDriver().getName());
            activeVehiclePositionHashMap.put(activeVehiclePositions.get(i), mMap.addMarker(markerOptions));
        }
    }

}

}

这是我调用线程的位置。

@Override
public void onLocationChanged(Location location) {
    currLatLng = new LatLng(location.getLatitude(), location.getLongitude());
    if(sourceMarker != null && LOOKING_FOR == CURRENT_LOCATION_POINTER) sourceMarker.remove();
    else if(destinationMarker != null && LOOKING_FOR == FINAL_LOCATION_POINTER) destinationMarker.remove();
    if(LOOKING_FOR == CURRENT_LOCATION_POINTER){
        setCurrentLocationMarker(currLatLng, sourceMarker);
        activeVehiclePositionArrayList= GetNearbyVehicle.getNearbyVehicle(mMap, activeVehiclePositionArrayList);
    }
    else if(LOOKING_FOR == FINAL_LOCATION_POINTER){
        setCurrentLocationMarker(currLatLng, destinationMarker);
    }
}

enter image description here

我从api调用添加的标记是洋红色。 我无法理解它有什么问题。任何人都可以帮助我吗?

注意:实时发生的事情是,在两个标记中,一个消失并占据新的位置,这将一个接一个地交替继续。

但理想情况下,应该发生的事情应该同时消失并同时采取新的立场。

1 个答案:

答案 0 :(得分:0)

虽然我设法让异步任务成为内部类,但我仍然想知道我是否将异步任务保持为独立类,那我该怎么做呢?

private static class GetNearbyVehicle extends AsyncTask<String, Void, ArrayList<ActiveVehiclePosition>> {
    ArrayList<ActiveVehiclePosition> activeVehiclePositionArrayList;
    public static void getNearbyVehicle(){
        String url= "some url";
        if(oldVehiclePositionArrayList != null){
            for (Map.Entry<ActiveVehiclePosition, Marker> entry : oldVehiclePositionArrayList.entrySet()) {
                Marker m= entry.getValue();
                m.remove();
            }
        }
        new GetNearbyVehicle().execute(url);
    }
    @Override
    protected ArrayList<ActiveVehiclePosition> doInBackground(String... params) {
        String url= params[0];
        try {
            ArrayList<ActiveVehiclePosition> ar= new ArrayList<>();
            activeVehiclePositionArrayList= JsonConverter.getNearByVehicle(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return activeVehiclePositionArrayList;
    }

    @Override
    protected void onPostExecute(ArrayList<ActiveVehiclePosition> activeVehiclePositions) {
        super.onPostExecute(activeVehiclePositions);

        oldVehiclePositionArrayList= new HashMap<>();
        if(activeVehiclePositions != null){
            for(int i=0; i<activeVehiclePositions.size(); i++){
                MarkerOptions markerOptions= new MarkerOptions();
                markerOptions.position(activeVehiclePositions.get(i).getLatLng());
                markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                markerOptions.title(activeVehiclePositions.get(i).getDriver().getName());
                oldVehiclePositionArrayList.put(activeVehiclePositions.get(i), mMap.addMarker(markerOptions));
            }
        }


    }
}