无法解析方法getProjection()和getMapCenter()

时间:2016-05-02 11:18:53

标签: android google-maps-android-api-2

我正在实施长按以在mapview上获取latlang, 我提到了this教程。但我收到了错误

这是我的代码:

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();
        }
    }
}

这里我的getProjection()和getMapCenter()无法解决,我想知道为什么会发生这种情况,这种方法是否已被弃用,或者我必须使用哪种方法,

我也想知道为什么 barcode.GeoPoint , 为什么我的程序导入

import com.google.android.gms.vision.barcode.Barcode;     instead of 
import com.google.maps.GeoPoint;

1 个答案:

答案 0 :(得分:1)

您可以实施GoogleMap.OnMapClickListenerGoogleMap.OnMapLongClickListener来实现此目标

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将包含此组件

<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"
        />