我想通过双击在谷歌地图上添加标记。我知道 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
映射Fragment.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);
}
}
现在如何通过双击在谷歌地图上添加标记并从另一个类获取坐标?任何帮助将不胜感激。
答案 0 :(得分:0)
您可以使用
中的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;
}
}
});
答案 1 :(得分:0)
尝试实现rajan ks的答案并将此行添加到您的&#34; onMapReady&#34;方法
googleMap.getUiSettings().setZoomControlsEnabled(false);
这将禁用Google地图双击手势以放大地图
答案 2 :(得分:0)
为此,您可以使用基于社区维基的this answer的方法:您需要拦截自定义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>
这就是诀窍。