在android中的地图标记上添加折线/线

时间:2017-07-17 09:00:37

标签: android google-maps google-maps-api-3 google-maps-markers

请。启发我如何在我的for-loop上运行的每个地图标记上添加折线/线。我在添加和连接每个标记时遇到问题。 这是我偶然发现的代码。有人可以加强我的代码吗?

             @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_contact_us, container, false);
/*
        // Instantiating the class PolylineOptions to plot polyline in the map
        final PolylineOptions polylineOptions = new PolylineOptions();

        b = (Button) v.findViewById(R.id.b1);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < list_location.size(); i++) {

                    // Setting the color of the polyline
                    polylineOptions.color(Color.RED);

                    // Setting the width of the polyline
                    polylineOptions.width(3);

                    Double lat = Double.parseDouble(list_location.get(i).getLatitude());
                    Double Longitude = Double.parseDouble(list_location.get(i).getLongitude());

                    // Setting points of polyline
                    polylineOptions.add(new LatLng(lat, Longitude));

                    createMarker(i, list_location.get(i).getLatitude(), list_location.get(i).getLongitude(), list_location.get(i).getLocation());
                }
                // Adding the polyline to the map
                map.addPolyline(polylineOptions);
            }
            });*/
        return v;
    }

    public static void createProximity(String latitude, String longitude) {
        Double lat = Double.parseDouble(latitude);
        Double Longitude = Double.parseDouble(longitude);

        map.addMarker(new MarkerOptions()
                .position(new LatLng(lat, Longitude))
                .anchor(0.5f, 0.5f)
                .title("title")
                .snippet("snippet")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));

        map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, Longitude), 9.0f));
    }

    public static void createMarker(int index, String latitude, String longitude, String snippet) {
        // Adding the taped point to the ArrayList
        BitmapDescriptor image = BitmapDescriptorFactory.fromResource(R.drawable.ic_menu_camera);
        Double lat = Double.parseDouble(latitude);
        Double Longitude = Double.parseDouble(longitude);
/*        float color = 0;
        if (index == 1)
            color = BitmapDescriptorFactory.HUE_AZURE;
        else if (index == list_location.size()-1)
            color = BitmapDescriptorFactory.HUE_VIOLET;*/

        if (index == 1)
            image = BitmapDescriptorFactory.fromResource(R.drawable.ic_menu_gallery);
        else if (index == list_location.size()-1)
            image = BitmapDescriptorFactory.fromResource(R.drawable.ic_menu_slideshow);
        else {
            image = BitmapDescriptorFactory.fromResource(R.drawable.ic_menu_camera);
        }

        map.addMarker(new MarkerOptions()
                .position(new LatLng(lat, Longitude))
                .anchor(0.5f, 0.5f)
                .title(snippet)
                .snippet(list_location.get(index).getLatitude())
                .icon(image));

        map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, Longitude), 12.0f));
        /*map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

            @Override
            public View getInfoWindow(Marker arg0) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {

                LinearLayout info = new LinearLayout(this.getContext());
                info.setOrientation(LinearLayout.VERTICAL);

                TextView title = new TextView(getContext());
                title.setTextColor(Color.BLACK);
                title.setGravity(Gravity.CENTER);
                title.setTypeface(null, Typeface.BOLD);
                title.setText(marker.getTitle());

                TextView snippet = new TextView(getContext());
                snippet.setTextColor(Color.GRAY);
                snippet.setText(marker.getSnippet());

                TextView snippet3 = new TextView(getContext());
                snippet3.setTextColor(Color.GRAY);
                snippet3.setText(marker.getSnippet());


                info.addView(title);
                info.addView(snippet);
                info.addView(snippet3);

                return info;
            }
        });*/

       map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                marker.hideInfoWindow();
            }
        });
/*         map.setInfoWindowAdapter(new MarkerInfoWindowAdapter());

        map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {

                for (int i = 0; i < list_location.size(); i++) {
                    Log.d("TAG", "getLatitude:" + list_location.get(i).getLatitude() + " getLongitude:" + list_location.get(i).getLongitude());
                    if (marker.getSnippet().equals(list_location.get(i).getLocation())) {
                        Log.d("TAG", "Selected Marker");
                    }
                }
            }
        });

        map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker arg0) {
                return null;
            }
            @Override
            public View getInfoContents(Marker marker) {

                LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
                View myContentView = inflater.inflate(R.layout.marker_popup, null);
                TextView loc = ((TextView) myContentView
                        .findViewById(R.id.textView2));
                TextView plate_num = ((TextView) myContentView
                        .findViewById(R.id.textView4));
                for (int i = 0; i < list_location.size(); i++) {
                    Log.d("TAG", "getLatitude:" + list_location.get(i).getLatitude() + " getLongitude:" + list_location.get(i).getLongitude());
                    if (marker.getSnippet().equals(list_location.get(i).getLocation())) {
                        Log.d("TAG", "Selected Marker");
                    }
                    loc.setText(list_location.get(i).getLatitude());
                    plate_num.setText(list_location.get(i).getLongitude());
                }



*//*                Button btnDismiss = (Button) myContentView.findViewById(R.id.dismissbtn);
                btnDismiss.setOnClickListener(new Button.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });*//*
                return myContentView;
            }
        });*/
    }

    public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

        public MarkerInfoWindowAdapter() {
        }

        @Override
        public View getInfoWindow(Marker marker) {
            return null;
        }

        @Override
        public View getInfoContents(final Marker marker) {
            View v = getActivity().getLayoutInflater().inflate(R.layout.marker_popup, null);
/*            for (int i = 0; i < list_location.size(); i++) {
                Log.d("TAG", "getLatitude:" + list_location.get(i).getLatitude() + " getLongitude:" + list_location.get(i).getLongitude());
                if (marker.getSnippet().equals(list_location.get(i).getLocation())) {
                    Log.d("TAG", "Selected Marker");

                }

            }*/
            Button b = (Button) v.findViewById(R.id.button2);
            b.setOnClickListener(new Button.OnClickListener() {

                @Override
                public void onClick(View v) {
                    marker.hideInfoWindow();
                }
            });
                TextView markerLabel = (TextView) v.findViewById(R.id.textView2);
            markerLabel.setText("");

            return v;
        }
    }

1 个答案:

答案 0 :(得分:0)

只需对代码进行一些小改动:

PolylineOptions polylineOptions = new PolylineOptions();
 b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < list_location.size(); i++) {
                    // Instantiating the class PolylineOptions to plot polyline in the map


                    // Setting the color of the polyline
                    polylineOptions.color(Color.RED);

                    // Setting the width of the polyline
                    polylineOptions.width(3);

                    Double lat = Double.parseDouble(list_location.get(i).getLatitude());
                    Double Longitude = Double.parseDouble(list_location.get(i).getLongitude());

                    // Setting points of polyline
                    polylineOptions.add(new LatLng(lat, Longitude));


                    createMarker(i, list_location.get(i).getLatitude(), list_location.get(i).getLongitude(), list_location.get(i).getLocation());
                }
                    // Adding the polyline to the map
                    map.addPolyline(polylineOptions);
            }
            });