Draw a path on maps following the finger motion path on the device screen

后端 未结 1 620
小蘑菇
小蘑菇 2020-12-15 01:18

1)I want to draw a motion path on google map as per the follow of my finger touch points on the area of map. 2)and get the selected area from the map which lies inside the p

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 02:07

    I have implemented the same for one of my Application hope that will help you, I have done this with the help of poly line and polygon.

    activity_main.xml
    
     
    
        
    
            
    
            
    
            

    The below class will cover the full code to draw custom region in Google map v2

    MainActivity.java
    
        import java.util.ArrayList;
        import android.graphics.Color;
        import android.graphics.Point;
        import android.os.Bundle;
        import android.support.v4.app.FragmentActivity;
        import android.util.Log;
        import android.view.GestureDetector;
        import android.view.GestureDetector.SimpleOnGestureListener;
        import android.view.MotionEvent;
        import android.view.View;
        import android.view.View.OnTouchListener;
        import android.widget.Toast;
    
        import com.google.android.gms.common.ConnectionResult;
        import com.google.android.gms.common.GooglePlayServicesUtil;
        import com.google.android.gms.maps.GoogleMap;
        import com.google.android.gms.maps.SupportMapFragment;
        import com.google.android.gms.maps.model.LatLng;
        import com.google.android.gms.maps.model.PolygonOptions;
        import com.google.android.gms.maps.model.PolylineOptions;
    
        public class MainActivity extends FragmentActivity implements OnTouchListener {
    
            private static final String TAG = "polygon";
            private GoogleMap mGoogleMap;
            private View mMapShelterView;
            private GestureDetector mGestureDetector;
            private ArrayList mLatlngs = new ArrayList();
            private PolylineOptions mPolylineOptions;
            private PolygonOptions mPolygonOptions;
            // flag to differentiate whether user is touching to draw or not
            private boolean mDrawFinished = false;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                mMapShelterView = (View) findViewById(R.id.drawer_view);
                mGestureDetector = new GestureDetector(this, new GestureListener());
                mMapShelterView.setOnTouchListener(this);
                initilizeMap();
            }
    
            private final class GestureListener extends SimpleOnGestureListener {
                @Override
                public boolean onDown(MotionEvent e) {
                    return true;
                }
    
                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                        float velocityY) {
                    return false;
                }
            }
    
            /**
             * Ontouch event will draw poly line along the touch points
             * 
             */
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int X1 = (int) event.getX();
                int Y1 = (int) event.getY();
                Point point = new Point();
                point.x = X1;
                point.y = Y1;
                LatLng firstGeoPoint = mGoogleMap.getProjection().fromScreenLocation(
                        point);
                switch (event.getAction()) {
    
                case MotionEvent.ACTION_DOWN:
                    break;
    
                case MotionEvent.ACTION_MOVE:
                    if (mDrawFinished) {
                        X1 = (int) event.getX();
                        Y1 = (int) event.getY();
                        point = new Point();
                        point.x = X1;
                        point.y = Y1;
                        LatLng geoPoint = mGoogleMap.getProjection()
                                .fromScreenLocation(point);
                        mLatlngs.add(geoPoint);
                        mPolylineOptions = new PolylineOptions();
                        mPolylineOptions.color(Color.RED);
                        mPolylineOptions.width(3);
                        mPolylineOptions.addAll(mLatlngs);
                        mGoogleMap.addPolyline(mPolylineOptions);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    Log.d(TAG, "Poinnts array size " + mLatlngs.size());
                    mLatlngs.add(firstGeoPoint);
                    mGoogleMap.clear();
                    mPolylineOptions = null;
                    mMapShelterView.setVisibility(View.GONE);
                    mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);
                    mGoogleMap.getUiSettings().setAllGesturesEnabled(true);
                    mPolygonOptions = new PolygonOptions();
                    mPolygonOptions.fillColor(Color.GRAY);
                    mPolygonOptions.strokeColor(Color.RED);
                    mPolygonOptions.strokeWidth(5);
                    mPolygonOptions.addAll(mLatlngs);
                    mGoogleMap.addPolygon(mPolygonOptions);
                    mDrawFinished = false;
                    break;
                }
                return mGestureDetector.onTouchEvent(event);
            }
    
            /**
             * Setting up map
             * 
             */
    
            private void initilizeMap() {
                int status = GooglePlayServicesUtil
                        .isGooglePlayServicesAvailable(getApplicationContext());
                if (status == ConnectionResult.SUCCESS) {
                    if (mGoogleMap == null) {
                        mGoogleMap = ((SupportMapFragment) getSupportFragmentManager()
                                .findFragmentById(R.id.map)).getMap();
                        mGoogleMap.setMyLocationEnabled(true);
    
                    }
    
                } else if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
                    // showErrorDialog(status);
                } else {
                    Toast.makeText(this, "No Support for Google Play Service",
                            Toast.LENGTH_LONG).show();
                }
            }
    
            /**
             * Method gets called on tap of draw button, It prepares the screen to draw
             * the polygon
             * 
             * @param view
             */
    
            public void drawZone(View view) {
                mGoogleMap.clear();
                mLatlngs.clear();
                mPolylineOptions = null;
                mPolygonOptions = null;
                mDrawFinished = true;
                mMapShelterView.setVisibility(View.VISIBLE);
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(false);
            }
    
        }
    

    Hopefully the above code is pretty enough to draw custom region in map v2. For detecting whether the particular point is inside the custom region or not we can use the below snippet,

    public synchronized boolean Contains(Location location) {
            boolean isInside = false;
            if (mLatlngs.size() > 0) {
                LatLng lastPoint = mLatlngs.get(mLatlngs.size() - 1);
    
                double x = location.getLongitude();
    
                for (LatLng point : mLatlngs) {
                    double x1 = lastPoint.longitude;
                    double x2 = point.longitude;
                    double dx = x2 - x1;
    
                    if (Math.abs(dx) > 180.0) {
                        if (x > 0) {
                            while (x1 < 0)
                                x1 += 360;
                            while (x2 < 0)
                                x2 += 360;
                        } else {
                            while (x1 > 0)
                                x1 -= 360;
                            while (x2 > 0)
                                x2 -= 360;
                        }
                        dx = x2 - x1;
                    }
    
                    if ((x1 <= x && x2 > x) || (x1 >= x && x2 < x)) {
                        double grad = (point.latitude - lastPoint.latitude) / dx;
                        double intersectAtLat = lastPoint.latitude
                                + ((x - x1) * grad);
    
                        if (intersectAtLat > location.getLatitude())
                            isInside = !isInside;
                    }
                    lastPoint = point;
                }
            }
    
            return isInside;
        }
    

    You can pass the location parameter to the method, it will return that whether the location is inside the custom region or not.

    Manifest.xml

    
    
    
        
    
        
    
        
        
        
        
        
        
    
        
            
                
                    
    
                    
                
            
    
            
            
        
    
        
    
    
    

    0 讨论(0)
提交回复
热议问题