为什么HashMap中添加的内容仅请求生命

时间:2015-07-12 14:29:43

标签: java android hashmap

我正在实施一个Android应用程序,其中包含用户可以选择路由的复选框列表,然后从服务器收到数据为latitude, longitude, route, direction, id的响应。

当我使用相同的选定路由向我的总线表添加新路由时,在添加部分(else块)中是否有此行为的解释,正在识别新添加的路由ID并且之后正在输入else块标记正在添加到HashMap ,但未在地图上显示。

此外,当新的Response再次到达该方法时,我注意到最后添加的带有新id的标记在HashMap中不是,它只包含第一个请求的元素,当请求是over和新添加的元素保留在HashMap中以获取请求Life!

代码:

public class Map extends FragmentActivity implements OnMapReadyCallback {

    GoogleMap map;
    HashMap<Integer, Marker> markerMap = new HashMap<Integer, Marker>();
    Marker marker = null;

    private void gotoLocation(int id, double lat, double lng,
            String route_direct) {
        // check = false;

        final float zoom = 11;
        LatLng ll = null;

        if (markerMap.containsKey(id)) {
            //Update block.
            // Update the location.

            // To figure out which ones was not updated.
            idList.add(id);

            marker = markerMap.get(id);
            // Remove from the map fragment.
            marker.remove();
            // Remove from the HashMap tp add the new one.
            markerMap.remove(id);

            ll = new LatLng(lat, lng);
            if (lat != 0 && lng != 0 && !route_direct.isEmpty()) {
                MarkerOptions markerOpt = new MarkerOptions()
                        .title(route_direct).position(ll).visible(true);
                marker = map.addMarker(markerOpt);
                markerMap.put(id, marker);

                CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll,
                        zoom);
                map.moveCamera(update);
            }

        } else {
            //Add block.
            // Add the marker on the map in the case it does not exist in the
            // HashMap before.
            ll = new LatLng(lat, lng);
            if (lat != 0 && lng != 0 && !route_direct.isEmpty()) {
                MarkerOptions markerOpt = new MarkerOptions()
                        .title(route_direct).position(ll).visible(true);
                marker = map.addMarker(markerOpt);
                //Here when I add a new record to my table then it is being added to the HashMap but this block is being always entered since the new added one just stays for the request life and is not as the added content of the first request.
                markerMap.put(id, marker);

                CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll,
                        zoom);
                map.moveCamera(update);
            }

        }

    }

0 个答案:

没有答案