在地图视图中距离当前位置约500米处放置一个圆圈

时间:2013-01-16 09:57:41

标签: android map android-mapview

我搜索了半天,我无法找到解决方案。请给我一些建议。

这就是我想在MapView中显示的方式,

this is how i want to show in MapView

这就是我的方式,

this is how i get

因此,该圆圈将以500米半径绘制,当前位置为中心。

if(GPSTracker == true)
{
    cl_drawable = getResources().getDrawable(R.drawable.blue);
    cl_itemizedOverlay = new MyItemizedOverlay(cl_drawable, mapView, shadow);
    mapView.getOverlays().clear();

    cur_loc = new GeoPoint((int)(latPt*1E6),(int)(lngPt*1E6));
    Log.e("Current place ", " is "+cur_loc);
    cl_overlayItem = new OverlayItem(cur_loc, "current location", "");
    cl_itemizedOverlay.addOverlay(cl_overlayItem);


     if(xxxx != 0)
     {
         drawable = getResources().getDrawable(R.drawable.yellow_pin);
         itemizedOverlay = new MyItemizedOverlay(drawable, mapView, shadow);

         for(int la=0;la<GoConstants.drink_venue_id.size();la++)
         {
             latpt = Double.valueOf(latd.get(la));
             lngpt = Double.valueOf(lond.get(la));

             p = new GeoPoint((int)(latpt*1E6),(int)(lngpt*1E6));
             overlayItem = new OverlayItem(p, GoConstants.xxxxx.get(la), "");
             itemizedOverlay.addOverlay(overlayItem);
         }

         mapOverlays.add(cl_itemizedOverlay);
         mapOverlays.add(itemizedOverlay);
         mapController = mapView.getController();
         mapController.animateTo(cur_loc);
         mapController.setZoom(10);
         mapView.getController().setCenter(cur_loc);

这是我的代码的一部分。我应该在何处以及在何处放置代码以获取上述mapView。

1 个答案:

答案 0 :(得分:0)

使用此课程:

import java.util.ArrayList;

import org.mabna.order.utils.Farsi;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MapItemizedOverlay extends ItemizedOverlay<OverlayItem> {
    // member variables
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private Context mContext;

    private int markerHeight;
    private int mTextSize;
    private int mTitleMargin;

    private Typeface tf;
    private boolean mDrawCircle;
    private int mRadiusInMeter = 0;

    public MapItemizedOverlay(Drawable defaultMarker, Context context,
            int textSize, int titleMargin, boolean drawCircle) {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
        mTextSize = textSize;           
        markerHeight = ((BitmapDrawable) defaultMarker).getBitmap().getHeight();
        mTitleMargin = titleMargin;
        mDrawCircle = drawCircle;
    }

    // In order for the populate() method to read each OverlayItem, it will make
    // a request to createItem(int)
    // define this method to properly read from our ArrayList
    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

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

    @Override
    protected boolean onTap(int index) {
        OverlayItem item = mOverlays.get(index);

        // Do stuff here when you tap, i.e. :
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();

        // return true to indicate we've taken care of it
        return true;
    }



    @Override
    public void draw(android.graphics.Canvas canvas, MapView mapView,
            boolean shadow) {
        super.draw(canvas, mapView, shadow);

        // go through all OverlayItems and draw title for each of them
        for (OverlayItem item : mOverlays) {
            /*
             * Converts latitude & longitude of this overlay item to coordinates
             * on screen. As we have called boundCenterBottom() in constructor,
             * so these coordinates will be of the bottom center position of the
             * displayed marker.
             */
            GeoPoint point = item.getPoint();
            Point markerBottomCenterCoords = new Point();
            mapView.getProjection().toPixels(point, markerBottomCenterCoords);

            /* Find the width and height of the title */
            TextPaint paintText = new TextPaint();
            Paint paintRect = new Paint();

            Rect rectText = new Rect();
            paintText.setTextSize(mTextSize);
            paintText.getTextBounds(item.getTitle(), 0, item.getTitle()
                    .length(), rectText);

            rectText.inset(-mTitleMargin, -mTitleMargin);
            rectText.offsetTo(
                    markerBottomCenterCoords.x - rectText.width() / 2,
                    markerBottomCenterCoords.y + 5);

            paintText.setTextAlign(Paint.Align.CENTER);
            paintText.setTextSize(mTextSize);
            paintText.setARGB(255, 255, 255, 255);
            // paintText.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);

            paintRect.setARGB(100, 0, 0, 0);

            if (mDrawCircle && mapView != null) {
                int radius = metersToRadius(mRadiusInMeter, mapView);
                int arcWidth = (int) ((float) radius * 0.01);
                arcWidth = arcWidth == 0 ? 1 : arcWidth;

                Paint paintCircle = new Paint();
                paintCircle.setARGB(10, 0, 0, 255);

                Paint paintArc = new Paint();
                // paintArc.setColor(Color.BLUE);
                paintArc.setARGB(80, 0, 0, 255);
                paintArc.setStrokeWidth(arcWidth);
                paintArc.setAntiAlias(true);
                paintArc.setStrokeCap(Paint.Cap.ROUND);
                paintArc.setStyle(Paint.Style.STROKE);

                RectF rectArc = new RectF();
                rectArc.set(markerBottomCenterCoords.x - radius,
                        markerBottomCenterCoords.y - radius,
                        markerBottomCenterCoords.x + radius,
                        markerBottomCenterCoords.y + radius);

                canvas.drawCircle(markerBottomCenterCoords.x,
                        markerBottomCenterCoords.y, radius, paintCircle);
                canvas.drawArc(rectArc, 0, 360, false, paintArc);
            }

            if (item.getTitle().length() > 0) {
                canvas.drawRoundRect(new RectF(rectText), 2, 2, paintRect);
                canvas.drawText(item.getTitle(),
                        rectText.left + rectText.width() / 2,
                        rectText.bottom - mTitleMargin, paintText);
            }
        }
    }

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

    public void removeOverlay(OverlayItem overlay) {
        mOverlays.remove(overlay);
        populate();
    }

    public void clear() {
        mOverlays.clear();
        populate();
    }

    public static int metersToRadius(float meters, MapView map) {
        return (int) (map.getProjection().metersToEquatorPixels(meters));
    }

    public void setRadius(int radiusInMeter) {
        mRadiusInMeter = radiusInMeter;
    }
}