我需要在谷歌地图上画一个自由的手形。
我试过这个-LINK但没有找到符合我要求的解决方案。
我要做到这一点-
android - 如何在Android的Google map 上绘制自由手形-LMLPHP
android - 如何在Android的Google map 上绘制自由手形-LMLPHP

最佳答案

这是XML代码-

   <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <FrameLayout
        android:id="@+id/fram_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">
    </FrameLayout>

Java类代码-
 FrameLayout fram_map;
 Boolean Is_MAP_Moveable = false; // to detect map is movable
 private GoogleMap googleMap;
 private boolean screenLeave = false;
 int source = 0;
 int destination = 1;
 private ArrayList<LatLng> val = new ArrayList<LatLng>();

@Nullable
@Override
public View onCreateView(){
   fram_map = (FrameLayout) view.findViewById(R.id.fram_map);


     fram_map.setOnTouchListener(new View.OnTouchListener() {
            @Override

            public boolean onTouch(View v, MotionEvent event) {

                if (Is_MAP_Moveable) {
                    float x = event.getX();
                    float y = event.getY();

                    int x_co = Math.round(x);
                    int y_co = Math.round(y);

//                Projection projection = googleMap.getProjection();
                    Point x_y_points = new Point(x_co, y_co);

                    LatLng latLng = googleMap.getProjection().fromScreenLocation(x_y_points);
                    latitude = latLng.latitude;

                    longitude = latLng.longitude;
                    HomeBean bean = new HomeBean();
                    bean.setPost_lat(String.valueOf(latitude));
                    bean.setPost_long(String.valueOf(longitude));
                    bean.setSource_id("3");

                    mLatLongList.add(bean);


                    System.out.println("LatLng : " + latitude + " : " + longitude);

                    LatLng point = new LatLng(latitude, longitude);

                    int eventaction = event.getAction();
                    switch (eventaction) {
                        case MotionEvent.ACTION_DOWN:
                            // finger touches the screen
                            screenLeave = false;
//                            System.out.println("ACTION_DOWN");

//                            val.add(new LatLng(latitude, longitude));

                        case MotionEvent.ACTION_MOVE:
                            // finger moves on the screen
//                            System.out.println("ACTION_MOVE");
                          /*  if (val.size()==3){
                                val.remove(1);
                            }*/

                            val.add(new LatLng(latitude, longitude));
                            screenLeave = false;
                            Draw_Map();

                        case MotionEvent.ACTION_UP:

//                            System.out.println("ACTION_UP");
                            if (!screenLeave) {
                                screenLeave = true;
                            } else {
                                System.out.println("ACTION_UP ELSE CAse");
                                Is_MAP_Moveable = false; // to detect map is movable
                                btn_draw_State.setImageResource(R.mipmap.erase_icon);
                                source = 0;
                                destination = 1;
                                draw_final_polygon();

                            }

                            // finger leaves the screen
//                            Is_MAP_Moveable = false; // to detect map is movable
//                            Draw_Map();
                            break;
                        default:
                            break;
                    }

                    if (Is_MAP_Moveable) {
                        Log.e("DRAW on MAP : ", "LatLng ArrayList Size : " + mLatLongList.size());
                        return true;

                    } else {
                        return false;
                    }


                } else {
                    return false;
                }


            }
        });
......}

 public void Draw_Map() {


//        specify latitude and longitude of both source and destination Polyline

        if (val.size() > 1) {
            googleMap.addPolyline(new PolylineOptions().add(val.get(source), val.get(destination)).width(8).color(ContextCompat.getColor(getActivity(), R.color.colorForDrawArea)));
            source++;
            destination++;
        }


    }


private void draw_final_polygon() {

    val.add(val.get(0));

    PolygonOptions polygonOptions = new PolygonOptions();
    polygonOptions.addAll(val);
    polygonOptions.strokeColor(ContextCompat.getColor(getActivity(), R.color.colorForDrawArea));
    polygonOptions.strokeWidth(8);
    polygonOptions.fillColor(ContextCompat.getColor(getActivity(), R.color.colorForDrawArea));
    Polygon polygon = googleMap.addPolygon(polygonOptions);
}

关于android - 如何在Android的Google map 上绘制自由手形,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35425896/

10-09 04:25