有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java地图中未显示新标记

我有一个复选框列表,用户可以在其中选择一些路由,然后从服务器获得响应。我有方法gotoLocation来更新标记的位置,以及在地图中添加一个新标记 使用相同的路由将新路由插入服务器端的表时

我使用HashMap,表id作为键,marker作为值,在我的例子中,键是unique

更新部分工作正常,但我面临添加部分的问题。因此,在用户选择所需路线后,它们作为标记添加到映射中,作为元素添加到HashMap中,但当我向表中插入具有相同路由和不同键(id在表中递增1)的新标记时,会出现问题,然后输入添加部分,识别新添加部分的id,但不会添加新标记在地图上,但到HashMap markerMap

另外,当新的响应再次到达该方法时,我注意到最后添加的带有新id的标记不在HashMap中,它只包含第一个请求的元素,当请求结束时,新添加的元素在请求生命周期内停留在HashMap中!对于这种行为有什么解释吗?我如何修复它

代码:

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 stays in the markerMap 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);
            }

        }

    }

编辑

public class Map extends FragmentActivity {

    GoogleMap map;
    HashMap<Integer, String> markerMap = new HashMap<Integer, String>();
    static String marker_string;

    static Marker marker = null;
    private void gotoLocation(int id, double lat, double lng,
            String route_direct) {
        final float zoom = 11;
        LatLng ll = null;
        if (markerMap.containsKey(id)) {
            // Update the location.
            marker_string = markerMap.get(id);              
            String[] marker_string_split = marker_string.split(",");
            double latit = Double.parseDouble(marker_string_split[0]);
            double longit = Double.parseDouble(marker_string_split[1]);
            String rout_dirc = marker_string_split[2];

            LatLng LL_2 = new LatLng(lat, lng);
            MarkerOptions markerOpt2 = new MarkerOptions().title(route_direct)
                    .position(LL_2);

            // This here doest work.
            marker.remove();

            // Remove from the HashMap tp add the new one.
            markerMap.remove(id);

            ll = new LatLng(lat, lng);
            MarkerOptions markerOpt = new MarkerOptions().title(route_direct)
                    .position(ll);
            marker = map.addMarker(markerOpt);

            String lat1 = Double.toString(lat);
            String lng1 = Double.toString(lng);
            String data = lat1 + "," + lng1 + "," + route_direct;
            markerMap.put(id, data);
            zoom();

        } else {
            // Add a new  marker 
            String lat1 = Double.toString(lat);
            String lng1 = Double.toString(lng);
            String data = lat1 + "," + lng1 + "," + route_direct;
            markerMap.put(id, data);

            ll = new LatLng(lat, lng);
            MarkerOptions markerOpt = new MarkerOptions().title(route_direct)
                    .position(ll);
            marker = map.addMarker(markerOpt);
            zoom();


        }
    }
}

共 (0) 个答案