安卓百度地图开发(四)在定位图层显示他人位置

在这篇博客中主要做了两个工作

① 添加显示他人位置的marker

② 将他人头像作为图片合并到marker中进行显示


主要代码:

① 添加他人位置

这段代码可以添加到构造函数中,或者从服务器获取到他人位置后的代码中去

//添加他人位置
        Resources r = this.getResources();
        Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.touxiang);
        addOthersLocation(double(对方的经度),double(对方的纬度),bmp(对方的头像bitmap));

主要函数:

public void addOthersLocation(double latitute,double longtitute, Bitmap touxiang) {

        Resources r = this.getResources();
        Bitmap bmp= BitmapFactory.decodeResource(r, R.drawable.icon_geo);//红点

//        //构建Marker图标
//        othersCurrentMarker = BitmapDescriptorFactory
//                .fromResource(R.drawable.icon_geo);

        //构建Marker图标
        othersCurrentMarker = BitmapDescriptorFactory
                .fromBitmap(mergeBitmap(bmp,touxiang));//public static BitmapDescriptor fromBitmap(Bitmap image)  + mergeBitmap()

        //定义Maker坐标点
        LatLng point = new LatLng(latitute, longtitute);
        //构建MarkerOption,用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(othersCurrentMarker);
        //在地图上添加Marker,并显示
        mBaiduMap.addOverlay(option);
    }

② 合并标志点图片和用户头像图像

//将两张图片合并为一张图片 用作头像
    private Bitmap mergeBitmap(Bitmap firstBitmap, Bitmap secondBitmap) {
        int Width = firstBitmap.getWidth();
        int height = firstBitmap.getHeight();
        secondBitmap = zoomImage(secondBitmap,Width,height);
        Bitmap bitmap = Bitmap.createBitmap(Width, height*2,
                firstBitmap.getConfig());
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(secondBitmap, new Matrix(), null);
        canvas.drawBitmap(firstBitmap, 0, height, null);
        return bitmap;
    }

    //缩放头像图片
    public  Bitmap zoomImage(Bitmap bgimage, double newWidth,
                             double newHeight) {
        // 获取这个图片的宽和高
        float width = bgimage.getWidth();
        float height = bgimage.getHeight();
        // 创建操作图片用的matrix对象
        Matrix matrix = new Matrix();
        // 计算宽高缩放率
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 缩放图片动作
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
                (int) height, matrix, true);
        return bitmap;
    }



完整代码:

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值