Android的谷歌地图多边形click事件 [英] Android Google Map Polygon click event

查看:418
本文介绍了Android的谷歌地图多边形click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在Android地图应用程序,我使用谷歌地图API的Andr​​oid V2。我从web服务获取该多边形数据,通过XML解析其转换并能显示在地图没有问题上。但是,是不是有什么办法,当用户触摸任意多边形打开像弹出?或者,也许,如果用户想更改选定多边形的坐标。我看到很多的例子,但它们与JavaScript或一些使用不同的第三方完成。难道有人有什么建议吗?先谢谢了。

I am working on a map application on Android and i am using Google Maps Android API V2. I get the polygon data from a web service, convert it by XML parse and can show it on the map without a problem. But isn't there any way to open like pop-up when user touches on any polygon? Or maybe if user wants to change coordinates of selected polygon. I saw many examples, but they are done with javascript or some using different third party. Do someone has any advice? Thanks in advance.

推荐答案

我有同样的问题。当用户点击一个多边形onMapClickListener不叫,当其他覆盖(如多边形)不处理敲击事件这只是调用。多边形不处理它,你可以看到 - 通用移动多边形屏幕中心。而该事件不会传递到onMapClickListener,仅此而已。
要解决它,我截取点击事件处理GM面前,在查看包装MapFragment,所描述的<一个href=\"http://stackoverflow.com/questions/14013002/google-maps-android-api-v2-detect-touch-on-map-solved\"标题=点击这里>这里,项目点击屏幕从​​点坐标映射,然后检查它是否是一个多边形内的地图上所描述的这里(其他答案讲述太)

I had the same problem. onMapClickListener is not called when user taps a polygon, it's only called when other overlays (such as Polygons) do not process the tap event. Polygon does process it, as you can see - GM moves the polygon to center of screen. And the event is not passed to onMapClickListener, that's it. To workaround it, I intercept tap events before GM handles them, in a View wrapping MapFragment, as described here, project clicked point from screen coordinates to map, and then check if it is inside a polygon on the map as described here (other answer tells about it too)

相关code:

public class MySupportMapFragment extends SupportMapFragment {
private View mOriginalContentView;
private TouchableWrapper mTouchView;
private BasicMapActivity mActivity;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mActivity = (BasicMapActivity) getActivity();
}

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

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

class TouchableWrapper extends FrameLayout {

    public TouchableWrapper() {
        super(mActivity);
    }

    @Override
  public boolean dispatchTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
          break;
      case MotionEvent.ACTION_UP: {
          int x = (int) event.getX();
          int y = (int) event.getY();
          mActivity.tapEvent(x,y);
          break;
      }
    }
    return super.dispatchTouchEvent(event);
  }
}

}

BasicMapActivity:

BasicMapActivity:

public void tapEvent(int x, int y) {
    Log.d(TAG,String.format("tap event x=%d y=%d",x,y));
    if(!isEditMode()) {
        Projection pp = mMap.getProjection();
        LatLng point = pp.fromScreenLocation(new Point(x, y));
        for (Shape ss : mPolygons) {
            if(ss.isPointInPolygon(point)) {
                ss.mMarkers.get(0).marker.showInfoWindow();
            }
        }
    }
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
}

布局:

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

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="au.com.datalink.plugins.MySupportMapFragment" />

</RelativeLayout>

这篇关于Android的谷歌地图多边形click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆