android google map添加标记和TipView

1.new一个mapoverlay并添加到map上。    mapoverlay上要绘制数据点标记和TipView:


protected void adjustCacheInMap(final List<CacheData> caches,

            CacheData selectedCache) {
        // create tip view
        final View mTipView = mInflater.inflate(
                R.layout.mobile_mc_findcache_result_map_tipview, null);

        mMapCaches.removeAllViews();
        mMapCaches.addView(mTipView);
        mMapCaches.post(new Runnable() {

            @Override
            public void run() {
                mMapCaches.getOverlays().clear();
                MobileResultMapOverlay mapOverlay = new MobileResultMapOverlay(
                        MobileFindCacheResultActivity.this, mTipView, null,
                        caches, mVibrator, mMapController);
                mMapCaches.getOverlays().add(mapOverlay);
                mMapCaches.getOverlays().add(mlo);
                mMapCaches.invalidate();
            }
        });
    }

2.MobileResultMapOverlay如下:


package com.mc.yellowbook.android.map;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Point;
import android.os.Vibrator;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
import com.mc.yellowbook.android.R;
import com.mc.yellowbook.android.app.AppConstants;
import com.mc.yellowbook.android.app.AppUtils;
import com.mc.yellowbook.android.findcache.MobileFindViewCacheActivity;
import com.mc.yellowbook.android.model.BusinessData;
import com.mc.yellowbook.android.model.CacheData;

public class MobileResultMapOverlay extends Overlay implements
        android.view.View.OnClickListener {
    // offset for tip view
    private static int TIP_POSITION_X_OFFERSET = 12;
    private static int TIP_POSITION_Y_OFFERSET = 7;
    
    private Bitmap geoPnt = null;
    private CacheData selected = null;
    private List<CacheData> coll = null;
    private List<GeoPoint> points = null;
    private Activity activity;
    private Vibrator vibrator;
    private MapController mapController;

    private View mTipView;
    private TextView mTextMapTipCacheName, mTextMapTipBusinessName,
            mTextMapTipDistance;
    private ImageView mImgMapTipViewCache, mImgMapTipCacheDifficulty,
            mImgMapTipCacheSize;

    public MobileResultMapOverlay(Activity activity, View mTipView,
            CacheData selectedCache, List<CacheData> caches, Vibrator vibrator, MapController mapController) {
        super();
        this.mapController = mapController;
        this.vibrator = vibrator;
        this.activity = activity;
        this.mTipView = mTipView;
        coll = caches;

        if (selectedCache == null && caches != null && caches.size() != 0) {
            // set the first one selected by default
            selected = caches.get(0);
        }

        points = new ArrayList<GeoPoint>();
        final List<CacheData> pointsList = new ArrayList<CacheData>();
        for (CacheData data : coll) {
            BusinessData p = data.getBusiness();
            if (p == null) {
                continue;
            }
            if (p.getLatE6() == 0 && p.getLonE6() == 0) {
                continue;
            }

            GeoPoint pnt = new GeoPoint(p.getLatE6(), p.getLonE6());
            points.add(pnt);
            pointsList.add(data);
        }

        if (mTipView != null) {
            // get View by id
            mTextMapTipBusinessName = (TextView) mTipView
                    .findViewById(R.id.textResultMapTipBusinessName);
            mTextMapTipCacheName = (TextView) mTipView
                    .findViewById(R.id.textResultMapTipCacheName);
            mTextMapTipDistance = (TextView) mTipView
                    .findViewById(R.id.textResultMapTipDistance);
            mImgMapTipViewCache = (ImageView) mTipView
                    .findViewById(R.id.imgResultMapTipViewCache);
            mImgMapTipCacheDifficulty = (ImageView) mTipView
                    .findViewById(R.id.imgResultMapTipDiff);
            mImgMapTipCacheSize = (ImageView) mTipView
                    .findViewById(R.id.imgResultMapTipSize);

            // set click listener
            mImgMapTipViewCache.setOnClickListener(this);
        }

        // get bitmap for point show on map
        geoPnt = BitmapFactory.decodeResource(activity.getResources(),
                R.drawable.mc_cache_flag);
    }

    @Override
    public boolean onTap(GeoPoint click, MapView mapView) {

        Projection projection = mapView.getProjection();

        Point tapPoint = projection.toPixels(click, null);

        Point myPoint = null;
        int idx = 0;
        double min_dis = Math.pow(25.0, 2);
        CacheData cache = null;
        for (CacheData data : coll) {
            BusinessData p = data.getBusiness();
            if (p == null || (p.getLatE6() == 0 && p.getLonE6() == 0)) {
                continue;
            }

            myPoint = projection.toPixels(points.get(idx), null);
            double dis = Math.pow(tapPoint.x - myPoint.x, 2.0)
                    + Math.pow(tapPoint.y - myPoint.y, 2.0);
            if (dis < min_dis) {
                min_dis = dis;
                cache = data;
            }
            idx++;
        }

//        if (cache != null) {
            if (cache != selected) {
                selected = cache;
//                mTipView.setVisibility(View.VISIBLE);
            }
//        }

        if (selected != null) {
            // move selected cache to map center
            BusinessData selectedBusiness = selected.getBusiness();
            mapController.animateTo(new GeoPoint(selectedBusiness.getLatE6(),
                    selectedBusiness.getLonE6()));
        }

        return false;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {
        Projection projection = mapView.getProjection();
        Matrix matrix = new Matrix();

        float scale = 0.7f;
        matrix.postScale(scale, scale);
        int width = geoPnt.getWidth();
        int height = geoPnt.getHeight();
        Bitmap newGeoPnt = null;
        newGeoPnt = Bitmap.createBitmap(geoPnt, 0, 0, width, height, matrix,
                true);
        width = newGeoPnt.getWidth();
        height = newGeoPnt.getHeight();

        // draw all points
        int idx = 0;
        for (CacheData data : coll) {
            BusinessData p = data.getBusiness();
            if (p == null || (p.getLatE6() == 0 && p.getLonE6() == 0)) {
                continue;
            }

            Point point = new Point();
            projection.toPixels(points.get(idx), point);
            canvas.drawBitmap(newGeoPnt, point.x - width, point.y - height,
                    null);
            idx++;
        }

        // show tip information
        if (selected != null) {
            Point point = new Point();
            projection.toPixels(new GeoPoint(selected.getBusiness().getLatE6(),
                    selected.getBusiness().getLonE6()), point);

            // show the information in the tip.
            MapView.LayoutParams pos = (MapView.LayoutParams) mTipView
                    .getLayoutParams();
            GeoPoint geop = new GeoPoint(selected.getBusiness().getLatE6(),
                    selected.getBusiness().getLonE6());
            pos.point = geop;
            pos.x = -width / 2 + TIP_POSITION_X_OFFERSET;
            pos.y = -height - mTipView.getHeight() / 2 + TIP_POSITION_Y_OFFERSET;
            mTipView.setLayoutParams(pos);

            // set the text
            mTextMapTipBusinessName.setText(selected.getBusiness().getName());
            mTextMapTipCacheName.setText(selected.getTitle());
            mTextMapTipDistance.setText(selected.getBusiness().getOneDecimalDistance());
            mImgMapTipCacheDifficulty.setImageLevel(selected.getDifficulty());
            mImgMapTipCacheSize.setImageLevel(selected.getSize());

            mTipView.setVisibility(View.VISIBLE);
            mTipView.bringToFront();
        } else {
            mTipView.setVisibility(View.GONE);
        }

        return super.draw(canvas, mapView, shadow, when);
    }

    @Override
    public void onClick(View v) {
        AppUtils.vibrate(vibrator);

        if(v == mImgMapTipViewCache){
            // go to view cache page
            Intent i = new Intent(activity, MobileFindViewCacheActivity.class);
            i.putExtra(AppConstants.INTENT_EXTRA_CACHE_DETAIL,
                    selected);
            activity.startActivity(i);
        }
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值