谷歌地图api V2 InfoWindowAdapter有textview& Imageview事件!,如何处理两个事件?

时间:2013-10-11 11:26:00

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

您好我正在使用自定义弹出窗口进行谷歌地图的演示应用程序。 请建议我如何处理textview&图像视图单击手柄。 我无法得到活动。下面是我的代码。

class PopupAdapter implements InfoWindowAdapter {
    LayoutInflater inflater=null;
        Context context;
        PopupAdapter(LayoutInflater inflater, Context context) {
            this.inflater=inflater;
            this.context = context;
        }

        public View getInfoWindow(Marker marker) {
            return(null);
        }

        public View getInfoContents(Marker marker) {

            MyModel mapItem = (MyModel) MainActivity.markers.get(marker.getId());  
            View popup=inflater.inflate(R.layout.popup, null);

            TextView tv=(TextView)popup.findViewById(R.id.title);
            ImageView im = (ImageView)popup.findViewById(R.id.icon);
            tv.setText(marker.getTitle());
            tv=(TextView)popup.findViewById(R.id.snippet);
            tv.setText(marker.getSnippet());


            im.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        Log.e("POPUP", "Image Click");
        }
        });

            tv.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {

        Log.e("POPUP", "HI");
        Toast.makeText(context, "HI", Toast.LENGTH_SHORT).show();
    }
    });

    return(popup);
    }
}

主要活动

public class MainActivity extends AbstractMapActivity implements
OnNavigationListener, OnInfoWindowClickListener {
private static final String STATE_NAV="nav";
private static final int[] MAP_TYPE_NAMES= { R.string.normal,
    R.string.hybrid, R.string.satellite, R.string.terrain };
  private static final int[] MAP_TYPES= { GoogleMap.MAP_TYPE_NORMAL,
      GoogleMap.MAP_TYPE_HYBRID, GoogleMap.MAP_TYPE_SATELLITE,
      GoogleMap.MAP_TYPE_TERRAIN };
  private GoogleMap map=null;
  public static HashMap<String, MyModel> markers= new HashMap<String, MyModel>();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (readyToGo()) {
      setContentView(R.layout.activity_main);

      SupportMapFragment mapFrag=
          (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

      initListNav();

      map=mapFrag.getMap();

      if (savedInstanceState == null) {
        CameraUpdate center=
            CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
                                                     -73.98180484771729));
        CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

        map.moveCamera(center);
        map.animateCamera(zoom);
      }
        MyModel item = new MyModel();
        item.setId("1");
        item.setName("Bhavesh");
        item.setAdd("Krishnanagar");  
      addMarker(map, 40.748963847316034, -73.96807193756104,R.string.un, R.string.united_nations,item);
      item = new MyModel();
        item.setId("2");
        item.setName("Kunal");
        item.setAdd("Bhavnagar");  
      addMarker(map, 40.76866299974387, -73.98268461227417,R.string.lincoln_center,R.string.lincoln_center_snippet,item);
      item = new MyModel();
        item.setId("3");
        item.setName("Ravi");
        item.setAdd("Ahmedabad");  
      addMarker(map, 40.765136435316755, -73.97989511489868,R.string.carnegie_hall, R.string.practice_x3,item);
      item = new MyModel();
        item.setId("3");
        item.setName("Binitbhai");
        item.setAdd("Shivranjani");  
      addMarker(map, 40.70686417491799, -74.01572942733765,R.string.downtown_club, R.string.heisman_trophy,item);

      map.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater(),MainActivity.this));
      map.setOnInfoWindowClickListener(this);

    }
  }

  @Override
  public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    map.setMapType(MAP_TYPES[itemPosition]);

    return(true);
  }

  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);

    savedInstanceState.putInt(STATE_NAV, getSupportActionBar().getSelectedNavigationIndex());
  }

  @Override
  public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_NAV));
  }

  @Override
  public void onInfoWindowClick(Marker marker) {




//    MyModel mapItem = (MyModel) markers.get(marker.getId()); 

//    Toast.makeText(this,marker.getSnippet()+ marker.getTitle() + " "+mapItem.getName() +" "+mapItem.getAdd() , Toast.LENGTH_LONG).show();
  }

  private void initListNav() {
    ArrayList<String> items=new ArrayList<String>();
    ArrayAdapter<String> nav=null;
    ActionBar bar=getSupportActionBar();

    for (int type : MAP_TYPE_NAMES) {
      items.add(getString(type));
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
      nav=
          new ArrayAdapter<String>(
                                   bar.getThemedContext(),
                                   android.R.layout.simple_spinner_item,
                                   items);
    }
    else {
      nav=
          new ArrayAdapter<String>(
                                   this,
                                   android.R.layout.simple_spinner_item,
                                   items);
    }

    nav.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    bar.setListNavigationCallbacks(nav, this);
  }

  private void addMarker(GoogleMap map, double lat, double lon, int title, int snippet,MyModel item) {
    markers.put(map.addMarker(new MarkerOptions().position(new LatLng(lat, lon)).title(getString(title)).snippet(getString(snippet)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))).getId(), item);
//    map.addMarker(new MarkerOptions().position(new LatLng(lat, lon)).title(getString(title)).snippet(getString(snippet)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));


  }
}

请建议我如何做这个活动。 谢谢..

3 个答案:

答案 0 :(得分:1)

您不能将OnClickListeners放在信息窗口的内容上。信息窗口本身不显示您的布局,而是显示从布局生成的Bitmap。这包含在the documentation

  

如上一节关于信息窗口所述,信息窗口不是实时视图,而是视图在地图上呈现为图像。因此,您在视图上设置的任何侦听器都将被忽略,并且您无法区分视图各个部分上的单击事件。建议您不要在自定义信息窗口中放置交互式组件(如按钮,复选框或文本输入)。

您可以use setOnInfoWindowClickListener()找出点按信息窗口的时间。

答案 1 :(得分:0)

是的,你不能在信息窗口的视图上添加监听器。你可以做的一件事,onInfoWindowClick显示警报菜单,并为用户提供一些选项。

mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker arg0) {
            final CharSequence[] items = { "message", "call"};

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Call");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            if (item == 0) {
                sendMessage();
            } else {
                makeCall();

            }

        }
    });
    AlertDialog alert = builder.create();
    alert.show();
        }
    });

或者你可以试试这样的东西.. 我不确定它是否有效。 https://stackoverflow.com/a/15040761/1792228

答案 2 :(得分:0)

正如其他答案中所解释的那样,Google Maps Android API v2不直接支持您想要实现的目标,但是......

您可以将自己置于MapFragmentMapView前面,处理MotionEvents以使OnClickListener被调用。

请参阅此答案,了解一个不错的(但是hackish)方法:https://stackoverflow.com/a/15040761/2183804