Android 百度地图开发(三)

上节我们一起实现了百度地图定位,overlays画点,画线等。今天一起来实现一下再地图上画弧线,画圆,画多边形,画文字以及信息窗和Android8.0以上对后台定位服务的限制以及处理方法(notification前台通知)。

1.画弧线,

用到百度地图api的ArcOptions,设置起点,中间点,终点,弧线宽度,颜色等属性进行绘制

 /**
     * 地图marker画弧线,用ArcOptions
     * @param start    弧线开始经纬度点
     * @param center   弧线中间的经纬度点
     * @param end       弧线结束位置的经纬度点
     * @param width    宽度
     * @param color    颜色
     * @param baiduMap 百度地图对象
     */
    public void DrawMarkerArc(LatLng start,LatLng center,LatLng end,int width,int color,BaiduMap baiduMap){
        if (baiduMap==null){
            return;
        }
        OverlayOptions options=new ArcOptions()
                .color(color)
                .width(width)
                .points(start,center,end);
        baiduMap.addOverlay(options);
    }

2.画圆,

用到CircleOptions,设置中心点坐标,半径(米),圆的颜色,圆的边框颜色,边框宽度等进行绘制


    /**
     * 在地图上画圆  用CircleOptions
     * @param center  中心点的坐标(经纬度)
     * @param radius  圆半径,单位是米
     * @param fillColor 圆的填充颜色
     * @param stokeColor  圆的边框颜色
     * @param stokeWidth  圆的边框宽度
     * @param baiduMap  百度地图对象
     */
    public void DrawMarkerCircle(@NonNull LatLng center, int radius, int fillColor, int stokeColor, int stokeWidth, BaiduMap baiduMap){
        OverlayOptions circleOption=new CircleOptions()
                .center(center)
                .radius(radius)
                .fillColor(fillColor)
                .stroke(new Stroke(stokeWidth,stokeColor));
        if (baiduMap!=null){
            baiduMap.addOverlay(circleOption);
        }
    }

3.画多边形,

用到PolygonOptions,设置多边形各个点的集合,多边形的颜色,多边形的边框宽度,边框颜色等信息进行绘制

 /**
     * 在地图上画多边形 用PolygonOptions
     * @param points  多边形各个点的坐标集合
     * @param fillColor 填充颜色
     * @param stokeColor 边框颜色
     * @param stokeWidth 边框宽度
     * @param baiduMap 百度地图对象
     */
    public void DrawMarkerPolygon(List<LatLng> points,int fillColor,int stokeColor,int stokeWidth,BaiduMap baiduMap){
            PolygonOptions polygonOptions=new PolygonOptions()
                    .points(points)
                    .fillColor(fillColor)
                    .stroke(new Stroke(stokeWidth,stokeColor));
            if (baiduMap != null){
                baiduMap.addOverlay(polygonOptions);
            }

    }

4.画文字信息,

用TextOptions,设置绘制位置,文字颜色,文字大小,背景颜色,文字内容,旋转角度等进行绘制

   /**
     * 在地图上绘制文字 用TextOPtions
     * @param latLng  绘制位置
     * @param bgColor 背景颜色
     * @param textColor 文字颜色
     * @param textSize 文字大小
     * @param rotate 旋转角度
     * @param text 文字内容
     * @param baiduMap 百度地图对象
     */
    public void DrawMarkerText(LatLng latLng,int bgColor,int textColor,int textSize,float rotate,String text,BaiduMap baiduMap){
        TextOptions textOptions=new TextOptions()
                .text(text)
                .bgColor(bgColor)
                .fontColor(textColor)
                .position(latLng)
                .rotate(rotate)
                .fontSize(textSize);
        if (baiduMap != null){
            baiduMap.addOverlay(textOptions);
        }
    }

5.画信息窗,

用InfoWindow,设置上下文信息,信息窗图标资源,信息窗文字信息,显示位置等信息进行绘制


    /**
     * 在地图上绘制信息窗  用InfoWindow
     * @param context  上下文对象
     * @param resourceId 信息窗的图标资源
     * @param text   信息窗的文字信息
     * @param latLng   infoWindow的显示位置
     * @param baiduMap  百度地图对象
     */
    public void DrawInfoWindow(Context context,int resourceId,String text,LatLng latLng,BaiduMap baiduMap){
        Button button=new Button(context);
        button.setBackgroundResource(resourceId);
        button.setText(text);

        InfoWindow infoWindow=new InfoWindow(button,latLng,-150);
        if (baiduMap != null){
            baiduMap.showInfoWindow(infoWindow);
        }

    }

 6.Android8.0定位,

Android8.0以上的定位的限与方案制可见百度地图官方文档,后台定位的限制

解决如下:
   //定位
    private void GetLocations() {
        mLocationClient = new LocationClient(this);
        LocationClientOption option = new LocationClientOption();
        //开启gps
        option.setOpenGps(true);
        //设置坐标类型
        option.setCoorType("bd0911");
        //设置刷新时间
        option.setScanSpan(1000);
        //设置是否需要地址信息,默认是false
        option.setIsNeedAddress(true);

        mLocationClient.setLocOption(option);

        //注册位置信息监听
        MylocationListener mylocationListener = new MylocationListener();
        mLocationClient.registerLocationListener(mylocationListener);

        mLocationClient.start();

        //设置绕过后台定位限制
        setLocationBackground();
        //关闭后台定位
//        mLocationClient.disableLocInForeground(true);
        //开启后台定位
        mLocationClient.enableLocInForeground(1, notification);
        isEnableLocInForeground = true;
    }


    private void setLocationBackground() {
        //android8.0及以上使用NotificationUtils
        if (Build.VERSION.SDK_INT >= 26) {
            mNotificationUtils = new NotificationUtils(this);
            Notification.Builder builder2 = mNotificationUtils.getAndroidChannelNotification
                    ("适配android 8限制后台定位功能", "正在后台定位");
            notification = builder2.build();
        } else {
            //获取一个Notification构造器
            Notification.Builder builder = new Notification.Builder(MainActivity.this);
            Intent nfIntent = new Intent(MainActivity.this, MainActivity.class);

            builder.setContentIntent(PendingIntent.
                    getActivity(MainActivity.this, 0, nfIntent, 0)) // 设置PendingIntent
                    .setContentTitle("inspection后台定位功能") // 设置下拉列表里的标题
                    .setSmallIcon(R.mipmap.ic_bjts) // 设置状态栏内的小图标
                    .setContentText("正在后台定位") // 设置上下文内容
                    .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间

            notification = builder.build(); // 获取构建好的Notification
        }
        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
    }

 NotificationUtils代码(摘自百度地图定位demo)

package com.kty.inspection.Utils;
    
    
    import android.annotation.SuppressLint;
    import android.app.Notification;
    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.content.Context;
    import android.content.ContextWrapper;
    import android.graphics.Color;
    
    public class NotificationUtils extends ContextWrapper {
    
        private NotificationManager mManager;
        public static final String ANDROID_CHANNEL_ID = "com.kty.inspection";
        public static final String ANDROID_CHANNEL_NAME = "CHANNEL_INSPECTION";
    
        public NotificationUtils(Context base) {
            super(base);
            createChannels();
        }
        @SuppressLint("NewApi")
        public void createChannels() {
    
            // create android channel
             NotificationChannel androidChannel = new NotificationChannel(ANDROID_CHANNEL_ID,
                    ANDROID_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            // Sets whether notifications posted to this channel should display notification lights
            androidChannel.enableLights(true);
            // Sets whether notification posted to this channel should vibrate.
            androidChannel.enableVibration(true);
            // Sets the notification light color for notifications posted to this channel
            androidChannel.setLightColor(Color.GREEN);
            // Sets whether notifications posted to this channel appear on the lockscreen or not
            androidChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    
            getManager().createNotificationChannel(androidChannel);
    
        }
    
        private NotificationManager getManager() {
            if (mManager == null) {
                mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            }
            return mManager;
        }
    
        @SuppressLint("NewApi")
        public Notification.Builder getAndroidChannelNotification(String title, String body) {
            return new Notification.Builder(getApplicationContext(), ANDROID_CHANNEL_ID)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setSmallIcon(android.R.drawable.stat_notify_more)
                    .setAutoCancel(true);
        }
    }

以上为今天的内容,感谢阅读!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值