我是新手Android开发者。我正在开发一个我正在使用GoogleMaps的应用程序。我必须在GoogleMaps中实现双击缩放。当我双击地图时,它应该放大。请尽可能提供示例代码 我写了以下代码,但我不知道要添加更多内容。
public class Maps extends MapActivity implements OnGestureListener, OnDoubleTapListener {
private GestureDetector detector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// MapViewer mapViewer = new MapViewer(null, null);
MapView mapView = (MapView) findViewById(R.id.mapview);
detector = new GestureDetector(this, this);
mapView.setBuiltInZoomControls(true);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public boolean onDoubleTap(MotionEvent e) {
mapView.getController().zoomIn();
return false;
}
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
public boolean onDown(MotionEvent e) {
return false;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
public void onLongPress(MotionEvent e) {
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
public void onShowPress(MotionEvent e) {
}
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
}
答案 0 :(得分:4)
根据此处提供的信息和double click/tap map zoom (Android)我得到的最佳解决方案是:
myLocationOverlay = new MyLocationOverlay(this, mapView);
MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay() {
private long systemTime = System.currentTimeMillis();
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if ((System.currentTimeMillis() - systemTime) < ViewConfiguration.getDoubleTapTimeout()) {
mapController.zoomInFixing((int) event.getX(), (int) event.getY());
}
break;
case MotionEvent.ACTION_UP:
systemTime = System.currentTimeMillis();
break;
}
return false;
}
};
由于https://developer.android.com/reference/android/view/ViewConfiguration.html#getDoubleTapTimeout()表示case MotionEvent.ACTION_UP
的计算方式,我们添加了 getDoubleTapTimeout()
。此外,我更改了缩放功能,因为这个功能最少,功能与地图应用程序完全相同。
答案 1 :(得分:2)
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if ((System.currentTimeMillis() - systemTime) < 200) {
mapView.getController().zoomIn();
}
systemTime = System.currentTimeMillis();
break;
}
return false;
}
这将模拟放大。在MapOverlay中实现(ItemizedOverlay)
答案 2 :(得分:0)
要将其放大到点按的位置,只需添加:
getController().animateTo(getProjection().fromPixels((int) ev.getX(), (int) ev.getY()));
答案 3 :(得分:0)
我一直在寻找答案/示例,但发现无处可用的代码。
最后,这是适合我的代码:
<强> MyMapActivity.java 强>
public class MyMapActivity extends MapActivity implements OnGestureListener,
OnDoubleTapListener{
private MapView mapView;
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.mapView);
}
@Override
public boolean onDoubleTap(MotionEvent e) {
int x = (int)e.getX(), y = (int)e.getY();;
Projection p = mapView.getProjection();
mapView.getController().animateTo(p.fromPixels(x, y));
mapView.getController().zoomIn();
return true;
}
//Here will be some autogenerated methods too
<强> 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 < 250) {
// Double tap
this.getController().zoomInFixing((int) ev.getX(), (int) ev.getY());
lastTouchTime = -1;
} else {
// Too slow
lastTouchTime = thisTime;
}
}
return super.onInterceptTouchEvent(ev);
}
}
<强> main.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>
不要忘记在此处用android:apiKey
替换apiKey
值。