0

我有一个使用 BalloonItemizedOverlay 的地图视图。一切正常,但我希望能够在地图标记被删除时调用网络服务。Web 服务调用在我的 MapActivity 中。

如何从 BalloonItemizedOverlay 调用 Web 服务,这是我的拖放代码所在的位置?

谢谢你的帮助。

4

2 回答 2

2

我通过使用广播接收器解决了此类问题。

在您的 BalloonItemizedOverlay 类中声明此代码:

Intent intent = new Intent();
intent.setAction("BalloonItemizedOverlay");
con.sendBroadcast(intent);

并在 MapActivity 中执行此操作。onCreate()::

{
IntentFilter filter = new IntentFilter("BalloonItemizedOverlay");
registerReceiver(myReceiver, filter);
}

private BroadcastReceiver myReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) {
//call your service method from here...
}
};
于 2013-02-04T05:07:43.820 回答
0

这是在 tap.hop 上拖放和弹出的代码,它对您很有用。

@Override
            protected boolean onTap(final int index) {
                  if ( isPinch ){
                        return false;
                  }else{
    //          getMapView().setOnTouchListener(new OnTouchListener() {
    //              @Override
    //              public boolean onTouch(View arg0, MotionEvent arg1) {
    //                  if (!items.isEmpty()) {
    //                      if (view != null) {
    //                          view.setVisibility(View.GONE);
    //                          getMapView().invalidate();
    //                      }
    //                  }
    //                  getMapView().invalidate();
    //                  return true;
    //              }
    //          });
                if (view != null) {
                    view.setVisibility(View.GONE);
                    getMapView().removeView(view);
                    getMapView().invalidate();
                    flag = false;
                    view = null;
                }
                view = getLayoutInflater().inflate(R.layout.balloon_overlay, null);
                LinearLayout layout = (LinearLayout) view
                        .findViewById(R.id.balloon_main_layout);
                layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
                view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
                view.setBackgroundResource(R.drawable.balloon_overlay_bg_selector);
                ImageView image = (ImageView) view
                        .findViewById(R.id.balloon_disclosure);
                TextView text = (TextView) view
                        .findViewById(R.id.balloon_item_title);
                text.setText(items.get(index).getTitle());

                if (items.get(index).getTitle() != null
                        && items.get(index).getTitle().equals("Me") == false) {
                    image.setImageResource(R.drawable.mekr);
                }

                Projection projection = getMapView().getProjection();
                Point point = new Point();
                projection.toPixels(items.get(index).getPoint(), point);

                int x = (int) (view.getWidth() / 2f);
                int y = -bitMap.getHeight() - 3;

                MapView.LayoutParams lp = new MapView.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT, items.get(index)
                                .getPoint(), x, y,
                        MapView.LayoutParams.BOTTOM_CENTER);
                        getMapView().removeView(view);
                        getMapView().invalidate();
                        getMapView().addView(view, lp);
                        getMapView().invalidate();

                    view.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (!items.isEmpty()) {
                                if (view != null) {
                                    view.setVisibility(View.GONE);
                                    getMapView().invalidate();
                                }
                            }
                            getMapView().invalidate();
                        }
                    });
                        selectedIndex = index;
                        return true;           
              }
            }

            @Override
            public boolean onTouchEvent(MotionEvent event, MapView mapView) {
                final int action = event.getAction();
                final int x = (int) event.getX();
                final int y = (int) event.getY();
                boolean result = false;

                if (action == MotionEvent.ACTION_DOWN) {
                    for (OverlayItem item : items) {
                        Point p = new Point(0, 0);

                        map.getProjection().toPixels(item.getPoint(), p);

                        if (hitTest(item, marker, x - p.x, y - p.y)) {
                            result = true;
                            inDrag = item;
                            items.remove(inDrag);
                            populate();

                            xDragTouchOffset = 0;
                            yDragTouchOffset = 0;

                            setDragImagePosition(p.x, p.y);
                            dragImage.setVisibility(View.VISIBLE);

                            xDragTouchOffset = x - p.x;
                            yDragTouchOffset = y - p.y;
                            isPinch=false;
                            break;
                        }
                    }
                } else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {

                    if (view != null) {
                        if (view.getVisibility() != 0) {
                            Log.e("touch", " out move");
                            setDragImagePosition(x, y);
                            result = true;
                            isPinch=true;
                        }else{
                            setDragImagePosition(x, y);
                            isPinch=false;
                        }
                    }else{
                        setDragImagePosition(x, y);
                        result = true;
                        isPinch=true;
                    }
                } else if (action == MotionEvent.ACTION_UP && inDrag != null) {
                    dragImage.setVisibility(View.GONE);

                    GeoPoint pt = map.getProjection().fromPixels(
                            x - xDragTouchOffset, y - yDragTouchOffset);
                    OverlayItem toDrop = new OverlayItem(pt, inDrag.getTitle(),
                            inDrag.getSnippet());

                    Log.e("touch", " out last" + pt.getLatitudeE6());
                    items.add(toDrop);
                    populate();
                    inDrag = null;
                    result = true;
                    isPinch = false;
                }
                    return (isPinch || super.onTouchEvent(event,mapView));
            }

            private void setDragImagePosition(int x, int y) {
                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) dragImage
                        .getLayoutParams();

                lp.setMargins(x - xDragImageOffset - xDragTouchOffset, y
                        - yDragImageOffset - yDragTouchOffset, 0, 0);
                dragImage.setLayoutParams(lp);
            }
        }

在 ontouch 事件中,您可以在标记被删除时触发 Web 服务。

于 2013-02-04T05:18:09.493 回答