当我们点击谷歌地图android中的标记时,如何在标记的顶部显示图像的底部

时间:2011-12-01 17:47:32

标签: android

我已经写了一个谷歌地图多标记应用程序。当我点击一个标记我已经写了一个代码,在标记的顶部显示九个补丁图像。但在这里,当我点击九个补丁图像不显示在谷歌地图中标记的顶部。它正在标记之后显示,同时九个修补图像的底边触摸地图的表面。如何才能在顶部显示九个修补图像底部点击该标记时的标记。我的代码如下。

公共类MarkerMapActivity扩展了BaseMapActivity {

public static String name;
public static String completeAddress;
public static String address;
private MyItemizedOverlay funPlaces;
public MapView mapView;
public List<Overlay> mapOverlays;
List<Overlay> markersList;
Drawable marker;
public String nameAddressStr;
public ImageView blueArrowIdValue;
public LinearLayout layout;

//Displays Markers on GoogleMap.
public void displayMarkersOnMap(){
      mapView = (MapView)findViewById(R.id.mapView);
      new GoogleMapAsyncTask().execute();   
}

@Override
protected boolean isLocationDisplayed() {
    return false;
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}

public class MyItemizedOverlay extends BalloonItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>();
    @SuppressWarnings("unused")
    private Context c;
    private GeoPoint center = null;

    public MyItemizedOverlay(GeoPoint point, Drawable marker,String hmtostring,String nameaddress,MapView mapView) {
        super(boundCenter(marker), mapView);
        c = mapView.getContext();
        m_overlays.add(new OverlayItem(point,hmtostring,nameaddress));
        populate();
    }

    public GeoPoint getCenterPt() {
        if (center == null) {
            int northEdge = -90000000;
            int southEdge = 90000000;
            int eastEdge = -180000000;
            int westEdge = 180000000;
            Iterator<OverlayItem> iter = m_overlays.iterator();
            while (iter.hasNext()) {
                GeoPoint pt = iter.next().getPoint();
                if (pt.getLatitudeE6() > northEdge)
                    northEdge = pt.getLatitudeE6();
                if (pt.getLatitudeE6() < southEdge)
                    southEdge = pt.getLatitudeE6();
                if (pt.getLongitudeE6() > eastEdge)
                    eastEdge = pt.getLongitudeE6();
                if (pt.getLongitudeE6() < westEdge)
                    westEdge = pt.getLongitudeE6();
            }
            center = new GeoPoint((int) ((northEdge + southEdge) / 2),
                    (int) ((westEdge + eastEdge) / 2));
        }
        return center;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
      if (!shadow) {
        super.draw(canvas, mapView, shadow);
      }
      return false;
    }

    public void addOverlay(OverlayItem overlay) {
        m_overlays.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return m_overlays.get(i);
    }

    @Override
    public int size() {
        return m_overlays.size();
    }

    @Override
    protected boolean onBalloonTap(int index) {
        return true;
    }       
}


public abstract class BalloonItemizedOverlay<Item> extends ItemizedOverlay<OverlayItem>{
    private MapView mapView;
    private BalloonOverlayView balloonView;
    private View clickRegion;
    private int viewOffset;
    final MapController mc;

    /**
     * Create a new BalloonItemizedOverlay
     * 
     * @param defaultMarker - A bounded Drawable to be drawn on the map for each item in the overlay.
     * @param mapView - The view upon which the overlay items are to be drawn.
     */
    public BalloonItemizedOverlay(Drawable defaultMarker, MapView mapView) {
        super(defaultMarker);
        this.mapView = mapView;
        viewOffset = 0;
        mc = mapView.getController();
    }



    /**
     * Set the horizontal distance between the marker and the bottom of the information
     * balloon. The default is 0 which works well for center bounded markers. If your
     * marker is center-bottom bounded,   this before adding overlay items to ensure
     * the balloon hovers exactly above the marker. 
     * 
     * @param pixels - The padding between the center point and the bottom of the
     * information balloon.
     */
    public void setBalloonBottomOffset(int pixels) {
        viewOffset = pixels;
    }

    /**
     * Override this method to handle a "tap" on a balloon. By default, does nothing 
     * and returns false.
     * 
     * @param index - The index of the item whose balloon is tapped.
     * @return true if you handled the tap, otherwise false.
     */
    protected boolean onBalloonTap(int index) {
        return false;
    }

    /* (non-Javadoc)
     * @see com.google.android.maps.ItemizedOverlay#onTap(int)
     */ 

    @Override
    protected final boolean onTap(int index) {          
        boolean isRecycled;
        final int thisIndex;
        GeoPoint point;         
        thisIndex = index;
        point = createItem(index).getPoint();           
        if (balloonView == null) {
            balloonView = new BalloonOverlayView(mapView.getContext(), viewOffset);
            clickRegion = (View) balloonView.findViewById(R.id.balloon_inner_layout);
            isRecycled = false;
        } else {
            isRecycled = true;
        }       
        balloonView.setVisibility(View.GONE);           
        List<Overlay> mapOverlays = mapView.getOverlays();
        if (mapOverlays.size() > 1) {
            hideOtherBalloons(mapOverlays);
        }
        balloonView.setData(createItem(index));         
        MapView.LayoutParams params = new MapView.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, point,
                MapView.LayoutParams.BOTTOM_CENTER);
        params.mode = MapView.LayoutParams.MODE_MAP;            
        setBalloonTouchListener(thisIndex);         
        balloonView.setVisibility(View.VISIBLE);
        if (isRecycled) {
            balloonView.setLayoutParams(params);
        } else {
            mapView.addView(balloonView, params);
        }       
        mc.animateTo(point);
        //balloonView.setOnClickListener(this);     
        return true;
    }

    /*public void onClick(View v){
        switch(v.getId()){
        case R.id.balloon_inner_layout:
            balloonView.setVisibility(View.GONE);
            break;
        }
    }*/


    /**
     * Sets the visibility of this overlay's balloon view to GONE. 
     */
    private void hideBalloon() {
        if (balloonView != null) {
            balloonView.setVisibility(View.GONE);
        }
    }

    /**
     * Hides the balloon view for any other BalloonItemizedOverlay instances
     * that might be present on the MapView.
     * 
     * @param overlays - list of overlays (including this) on the MapView.
     */
    private void hideOtherBalloons(List<Overlay> overlays) {            
        for (Overlay overlay : overlays) {
            if (overlay instanceof BalloonItemizedOverlay<?> && overlay != this) {
                ((BalloonItemizedOverlay<?>) overlay).hideBalloon();
            }
        }       
    }

    /**
     * Sets the onTouchListener for the balloon being displayed, calling the
     * overridden onBalloonTap if implemented.
     * 
     * @param thisIndex - The index of the item whose balloon is tapped.
     */
    private void setBalloonTouchListener(final int thisIndex) {         
        try {
            @SuppressWarnings("unused")
            Method m = this.getClass().getDeclaredMethod("onBalloonTap", int.class);                
            clickRegion.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {                     
                    View l =  ((View) v.getParent()).findViewById(R.id.balloon_main_layout);
                    Drawable d = l.getBackground();                 
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        int[] states = {android.R.attr.state_pressed};
                        if (d.setState(states)) {
                            d.invalidateSelf();
                        }
                        return true;
                    } else if (event.getAction() == MotionEvent.ACTION_UP) {
                        int newStates[] = {};
                        if (d.setState(newStates)) {
                            d.invalidateSelf();
                        }
                        // call overridden method
                        onBalloonTap(thisIndex);
                        return true;
                    } else {
                        return false;
                    }                       
                }
            });

        } catch (SecurityException e) {
            Log.e("BalloonItemizedOverlay", "setBalloonTouchListener reflection SecurityException");
            return;
        } catch (NoSuchMethodException e) {
            // method not overridden - do nothing
            return;
        }
    }       
}

public class BalloonOverlayView extends FrameLayout {
    //private LinearLayout layout;
    private TextView pinAddressIdValue;
    @SuppressWarnings("unused")
    private String nameAddress;
    @SuppressWarnings("unused")
    private LinearLayout mainLinLayout;

    /**
     * Create a new BalloonOverlayView.
     * 
     * @param context - The activity context.
     * @param balloonBottomOffset - The bottom padding (in pixels) to be applied
     * when rendering this view.
     */
    public BalloonOverlayView(Context context, int balloonBottomOffset) {
        super(context);
        setPadding(10, 0, 10, balloonBottomOffset);
        layout = new LinearLayout(context);
        layout.setVisibility(VISIBLE);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.pinclick, layout);
        pinAddressIdValue = (TextView) v.findViewById(R.id.pinAddressId);
        mainLinLayout=(LinearLayout)v.findViewById(R.id.balloon_main_layout);
        blueArrowIdValue = (ImageView) v.findViewById(R.id.blueArrowId);
        ImageView blueArrowIdValue = (ImageView) v.findViewById(R.id.blueArrowId);
        blueArrowIdValue.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                layout.setVisibility(GONE);
                DetailsPinScreen.nameAddress=nameAddressStr;
                TabsScreen parentDetailsPin = (TabsScreen)MarkerMapActivity.this.getParent();
                parentDetailsPin.detailsPinScreenEmployees(true);
                DetailsPinScreen.dpses.setValues();
                DetailsPinScreen.dpses.loadStaticGoogleMap();
                DetailsPinScreen.dpses.currenLocLatLong();
            }
        }); 
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.NO_GRAVITY;
        addView(layout, params);
    }

    /**
     * Sets the view data from a given overlay item.
     * 
     * @param item - The overlay item containing the relevant view data 
     * (title and snippet). 
     */
    public void setData(OverlayItem item) {     
        layout.setVisibility(VISIBLE);
        if (item.getTitle() != null) {
            nameAddress=item.getTitle();
        }else {
         }
        if (item.getSnippet() != null) {
            pinAddressIdValue.setVisibility(VISIBLE);
            String nameaddress=item.getSnippet();
            pinAddressIdValue.setText(nameaddress);
         }else {
            pinAddressIdValue.setVisibility(GONE);
          }     
       }
    }   

private class GoogleMapAsyncTask extends AsyncTask<Void,Void,Void>{ 
    public Void doInBackground(Void...voids ){
         Drawable marker;
         try{
         StringBuffer strBuffer=new StringBuffer();
         strBuffer.append("name=");
         strBuffer.append(name);
         strBuffer.append("\n");
         strBuffer.append("address=");
         strBuffer.append(completeAddress);
         nameAddressStr=strBuffer.toString();
         marker = getResources().getDrawable(R.drawable.pin_green);
         marker.setBounds((int) (-marker.getIntrinsicWidth() / 2),-marker.getIntrinsicHeight(),(int) (marker.getIntrinsicWidth() / 2), 0);
         Geocoder geoCoder = new Geocoder(MarkerMapActivity.this, Locale.getDefault());
         List<Address> addresses = geoCoder.getFromLocationName(completeAddress,5);
         if (addresses.size() > 0) {
               GeoPoint point =new GeoPoint((int) (addresses.get(0).getLatitude() * 1E6),(int) (addresses.get(0).getLongitude() * 1E6));
               funPlaces = new MyItemizedOverlay(point,marker,nameAddressStr,completeAddress,mapView);
               markersList=mapView.getOverlays();
               markersList.clear();
               markersList.add(funPlaces);       
           }
         }catch (IOException e) {
             e.printStackTrace();
          }     
         return null;   
    }

     protected void onPostExecute(Void result) {
         for(int i=0;i<mapView.getOverlays().size();i++){
                GeoPoint pt = funPlaces.getCenterPt();
                MapController mc = mapView.getController();
                   mc.setCenter(pt);
            }
         }
      }
   }

enter image description here

实际上地图正在显示,但这里我没有显示。

0 个答案:

没有答案