1

i am implementing long click to get latlang on mapview, i referred this tutorial. but i am getting error

here is my code:

import com.google.android.gms.vision.barcode.Barcode;
public class MyCustomMapView extends MapView {

    public interface OnLongpressListener {
        public void onLongpress(MapView view, Barcode.GeoPoint longpressLocation);
    }
    static final int LONGPRESS_THRESHOLD = 500;
    private Barcode.GeoPoint lastMapCenter; 
    private Timer longpressTimer = new Timer();
    private MyCustomMapView.OnLongpressListener longpressListener;

    public MyCustomMapView(Context context) {
        super(context);
    }

    public void setOnLongpressListener(MyCustomMapView.OnLongpressListener listener) {
        longpressListener = listener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        handleLongpress(event);

        return super.onTouchEvent(event);
    }
    private void handleLongpress(final MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // Finger has touched screen.
            longpressTimer = new Timer();
            longpressTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    Barcode.GeoPoint longpressLocation = getProjection().fromPixels((int)event.getX(),
                            (int)event.getY()); <--here -->

                    longpressListener.onLongpress(MyCustomMapView.this, longpressLocation);
                }

            }, LONGPRESS_THRESHOLD);

            lastMapCenter = **getMapCenter()**;
        }

        if (event.getAction() == MotionEvent.ACTION_MOVE) {

            if (!getMapCenter().equals(lastMapCenter)) {
                longpressTimer.cancel();
            }

            lastMapCenter = getMapCenter();<-- here -->
        }

        if (event.getAction() == MotionEvent.ACTION_UP) {
            longpressTimer.cancel();
        }

        if (event.getPointerCount() > 1) {
            longpressTimer.cancel();
        }
    }
}

here my getProjection() and getMapCenter() cant resolve, i want to know why its happening, is this method deprecated, or what method i have to use instead,

and i also want to know that why its barcode.GeoPoint, why my program importing

import com.google.android.gms.vision.barcode.Barcode;     instead of 
import com.google.maps.GeoPoint;
Sagar Chavada
  • 5,169
  • 7
  • 40
  • 67

1 Answers1

1

You can implement GoogleMap.OnMapClickListener and GoogleMap.OnMapLongClickListener to achieve this

public class CreateFenceActiviy extends AppCompatActivity implements GoogleMap.OnMapClickListener, GoogleMap.OnMapLongClickListener{
    private GoogleMap mGoogleMap;
    private SupportMapFragment mMapFragment;
    private ArrayList<double[]> mLatLongArray =null;
    private Polygon mPolygon;
    private PolygonOptions mPolygonOptions;

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_fence_activity);
        mLatLongArray = new ArrayList<double[]>();
        if (mGoogleMap == null) {
                mMapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_create_fence));
                mGoogleMap = mMapFragment.getMap();
                mGoogleMap.setOnMapClickListener(this);
                mGoogleMap.setOnMapLongClickListener(this);
                mGoogleMap.setOnMarkerClickListener(this);
                isMarkerClicked = false;
            }       

    }
    @Override
    public void onMapClick(LatLng point) {
        if(mGoogleMap != null){
            mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(point));
            isMarkerClicked = false;
        }
    }

    @Override
    public void onMapLongClick(LatLng point) {
        if(mGoogleMap != null) {
            mGoogleMap.addMarker(new MarkerOptions().position(point).title(point.toString()));
            double latitudeNew = point.latitude;
            double longitude = point.longitude;
            mLatLongArray.add(new double[]{latitudeNew, longitude});
            isMarkerClicked = false;
        }
    }
}

XML for Map view will contain this component

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map_create_fence"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
Sreehari
  • 5,621
  • 2
  • 25
  • 59