簡體   English   中英

如何通過雙擊在地圖上添加標記並獲取坐標 [android]

[英]How to add marker on map by double tap and get the coordinates [android]

我想通過雙擊在谷歌地圖上添加標記。 我知道googlemap.OnMapClickListener但它會在單擊時添加一個標記。 我在谷歌地圖中找到了一個關於處理雙擊的帖子,但我無法從另一個類添加標記。

MapFragment.java:

        @Override
        public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_map, container, false);

            mMapView = (MapView) view.findViewById(R.id.mapView);
            mMapView.onCreate(savedInstanceState);

            mMapView.onResume(); // needed to get the map to display immediately

            try {
                MapsInitializer.initialize(getActivity().getApplicationContext());
            } catch (Exception e) {
                e.printStackTrace();
            }

            mMapView.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap mMap) {
                    googleMap = mMap;

                    if (latitude != null && longitude != null) {
                        // For showing a move to my location button
                        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
                        {
                            return;
                        }
                        googleMap.setMyLocationEnabled(true);

                        // For dropping a marker at a point on the Map
                        final LatLng coordinates = new LatLng(latitude, longitude);

                        // For zooming automatically to the location of the marker
                        cameraPosition = new CameraPosition.Builder().target(coordinates).zoom(15).build();
                        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

                        googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                            @Override
                            public void onMapClick(final LatLng coordinates){

                            **//adds marker on single tap & get coordinates**//

                            }
                        });

                    }

                }

            });

            return view;
        }

@Override
public boolean onDoubleTap(MotionEvent e) {

    return true; 
}

// Here will be some autogenerated methods too

地圖片段.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <azizbekyan.andranik.map.OnDoubleTap
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="YOUR_API_KEY" />        
</LinearLayout>

OnDoubleTap.java:

    public class OnDoubleTap extends MapView {

  private long lastTouchTime = -1;

  public OnDoubleTap(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
      long thisTime = System.currentTimeMillis();
      if (thisTime - lastTouchTime < ViewConfiguration.getDoubleTapTimeout()) {
        //** Double tap control here **//

        lastTouchTime = -1;
      } else {
        // Too slow 
        lastTouchTime = thisTime;
      }
    }
    return super.onInterceptTouchEvent(ev);
  }
}

現在如何通過雙擊在谷歌地圖上添加標記並從另一個類獲取坐標? 任何幫助,將不勝感激。

您可以使用googleMap.setonMapClickListener

在片段內聲明全局變量

private long lastTouchTime = -1;

將片段內的偵聽器更新為

googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    @Override
    public void onMapClick(final LatLng coordinates){
        long thisTime = System.currentTimeMillis();
        if (thisTime - lastTouchTime < ViewConfiguration.getDoubleTapTimeout()) {
            //** Double tapped , write logic to add Map **//
            lastTouchTime = -1;
        } else {
            lastTouchTime = thisTime;
        }
    }
});

嘗試實現 rajan ks 答案並將此行添加到您的“onMapReady”方法中

googleMap.getUiSettings().setZoomControlsEnabled(false);

這將禁用 Google 地圖雙擊手勢以放大地圖

為此,您可以使用方法,基於社區維基的這個答案:您需要攔截自定義TouchableWrapper視圖中的TouchableWrapper並管理縮放手勢啟用/禁用:

public class TouchableWrapper extends FrameLayout {

    private GoogleMap mGoogleMap = null;
    private long mLastTouchTime = -1;

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

    public void setGoogleMap(GoogleMap googleMap) {
        mGoogleMap = googleMap;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN:
                mGoogleMap.getUiSettings().setZoomGesturesEnabled(false);
                long thisTime = System.currentTimeMillis();
                if (thisTime - mLastTouchTime < ViewConfiguration.getDoubleTapTimeout()) {

                    if (mGoogleMap != null) {
                        mGoogleMap.addMarker(new MarkerOptions()
                                .position(mGoogleMap.getProjection().fromScreenLocation(new Point((int)event.getX(), (int)event.getY())))
                                .title("DblTapped"));
                    }
                    mLastTouchTime = -1;
                } else {
                    mLastTouchTime = thisTime;
                    mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);
                }
                break;
        }
        return super.dispatchTouchEvent(event);
    }
}

而不是創建自定義 MapFragment,它交換原始視圖和可觸摸視圖:

public class MultiTouchMapFragment extends MapFragment {
    public View mOriginalContentView;
    public TouchableWrapper mTouchView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        mOriginalContentView = super.onCreateView(inflater, parent, savedInstanceState);
        mTouchView = new TouchableWrapper(getActivity());
        mTouchView.addView(mOriginalContentView);
        return mTouchView;
    }

    @Override
    public View getView() {
        return mOriginalContentView;
    }
}

並在MainActivity使用它:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mGoogleMap;
    private MultiTouchMapFragment mMapFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mMapFragment = (MultiTouchMapFragment) getFragmentManager()
                .findFragmentById(R.id.map_fragment);
        mMapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
        mMapFragment.mTouchView.setGoogleMap(mGoogleMap);
    }

}

其中activity_mail.xml是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="<your_package_name>.MainActivity">

    <fragment
        android:id="@+id/map_fragment"
        android:name="<your_package_name>.MultiTouchMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

就這樣成功了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM