手机屏幕四角在百度地图中的坐标点和在屏幕内随机撒点

2017-06-27  本文已影响0人  墨色尘埃

在屏幕内随机撒点

public void initData() {
        list = new ArrayList<>();
        double initLat = 32.030717;//圆点
        double initLng = 118.723488;

        double sanLat = 32.024201;//三角
        double sanLng = 118.721533;
        for (int i = 0; i < 50; i++) {
            Random r = new Random();
            double y = r.nextDouble() * 0.05; //between 0.0 and 1.0
            DecimalFormat df = new DecimalFormat("0.0000");// 保留4位小数
            double intStr = Double.parseDouble(df.format(y));

            initLat -= intStr;
            double jia = initLng + intStr;
            BPointRound bPointRound = new BPointRound();
            if (initLat > 31.960735) {
                bPointRound.setLatitude(initLat + "");  //118.723488,32.030717(绿博园)
                bPointRound.setLongitude(jia + ""); //118.768691,31.962735(银杏山庄)
            } else {
                double initLat1 = 32.026221;
                double initLng1 = 118.724336;
                Random r1 = new Random();
                double y1 = r1.nextDouble() * 0.05; //between 0.0 and 1.0
                DecimalFormat df1 = new DecimalFormat("0.0000");// 保留4位小数
                double intStr1 = Double.parseDouble(df1.format(y1));

                initLat1 -= intStr1;
                double jia1 = initLng1 + intStr;
                if (initLat1 > 31.960735) {
                    bPointRound.setLatitude(initLat1 + "");  //118.723488,32.030717(绿博园)
                    bPointRound.setLongitude(jia1 + ""); //118.768691,31.962735(银杏山庄)
                } else {
                    double lat = initLat1 - (intStr1 * 0.1);
                    double jia2 = initLng1 + intStr;
                    bPointRound.setLatitude(lat + "");  //118.723488,32.030717(绿博园)
                    bPointRound.setLongitude(jia2 + ""); //118.768691,31.962735(银杏山庄)
                }
            }


            sanLat -= intStr;
            double jiaSan = sanLng + intStr;
            BPointTriangle bPoint1 = new BPointTriangle();
            bPoint1.setAz("0,120,240");
            bPoint1.setCompare("弱,强,弱");
            bPoint1.setLapNum("1,2,3");
            bPoint1.setLiveStatus("在网(在网运行),在网(在网运行),在网(在网运行)");
            bPoint1.setSelfWeak("强,弱,弱");
            if (sanLat > 31.960735) {
                bPoint1.setLatitude(sanLat + "");  //118.723488,32.030717(绿博园)
                bPoint1.setLongitude(jiaSan + ""); //118.768691,31.962735(银杏山庄)
            } else {
                double sanLat1 = 32.020649; //118.719521,32.020649
                double sanLng1 = 118.719521;
                Random r1 = new Random();
                double y1 = r1.nextDouble() * 0.05; //between 0.0 and 1.0
                DecimalFormat df1 = new DecimalFormat("0.0000");// 保留4位小数
                double intStr1 = Double.parseDouble(df1.format(y1));

                sanLat1 -= intStr1;
                double jia1 = sanLng1 + intStr;
                if (sanLat1 > 31.960735) {
                    bPoint1.setLatitude(sanLat1 + "");  //118.723488,32.030717(绿博园)
                    bPoint1.setLongitude(jia1 + ""); //118.768691,31.962735(银杏山庄)
                } else {
                    double lat = sanLat1 - (intStr1 * 0.1);
                    double jia2 = sanLng1 + intStr;
                    bPoint1.setLatitude(lat + "");  //118.723488,32.030717(绿博园)
                    bPoint1.setLongitude(jia2 + ""); //118.768691,31.962735(银杏山庄)
                }
            }
            list.add(bPointRound);
            list.add(bPoint1);
        }
    }

手机屏幕四角在百度地图中的坐标点:onCreate方法中

mMapView = (MapView) findViewById(mapView);
mBaiduMap = mMapView.getMap();
mBaiduMap.setOnMapStatusChangeListener(listener); //监听滑动的事件

BaiduMap.OnMapStatusChangeListener listener = new BaiduMap.OnMapStatusChangeListener() {
        /**
         * 手势操作地图,设置地图状态等操作导致地图状态开始改变。
         * @param status 地图状态改变开始时的地图状态
         */
        public void onMapStatusChangeStart(MapStatus status) {
        }

        /**
         * 地图状态变化中
         * @param status 当前地图状态
         */
        public void onMapStatusChange(MapStatus status) {
        }

        /**
         * 地图状态改变结束
         */
        @Override
        public void onMapStatusChangeFinish(MapStatus mapStatus) {
            //左上角经纬度
            Point pt = new Point();
            pt.x = 0;
            pt.y = 0;
            LatLng ll = mBaiduMap.getProjection().fromScreenLocation(pt);
            //右下角经纬度
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            Point pty = new Point();
            pty.x = dm.widthPixels;
            pty.y = dm.heightPixels;
            LatLng lly = mBaiduMap.getProjection().fromScreenLocation(pty);

            //这里能正确显示坐标
            LatLng[] pts = MapUtils.getMapLTandRB(mBaiduMap);
//            pts[0] = MapUtils.fromBaiduToGPS(pts[0]); //gps坐标
//            pts[1] = MapUtils.fromBaiduToGPS(pts[1]); //gps坐标
            Log.e("MainActivity", "左上角坐标:" + pts[0]+"\n右下角坐标:"+pts[1]);

            pointByWindow = DrawPointByWindow.getInstance(BaiduMapUse.getInstance(MainActivity.this
                    , mBaiduMap), mBaiduMap);
            pointByWindow.getPoint(pts);

            //如果把marker的点击事件放在屏幕滑动的监听中,会出现滑动一次就多执行一次setOnMarkerClickListener事件
            //可能是因为onMapStatusChangeFinish滑动一次setOnMarkerClickListener也要执行一次,虽然暂时没有点击
            //但是被记录下来,等到点击marker的时候会执行完所有的次数
//            mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
//                @Override
//                public boolean onMarkerClick(Marker marker) {
//
//                    long startTime = System.currentTimeMillis();
//                    Log.e("MainActivity", "startTime:" + startTime);
//
//                    Bundle b = MapUtils.getDataWithOverlay(marker);
//                    if (null != b) {
//                        Serializable data = b.getSerializable("data");
//                        BasePointInfo basePointInfo = (BasePointInfo) data;
//                        String layerId = basePointInfo.getLayerId();
//                        //查询已勾选图层下对应坐标的基站
//                        List<BasePointInfo> pois = new ArrayList<>();
//                        pois.add(basePointInfo);
//                        pois.add(basePointInfo);
//
//                        PixelInfoDialog dialog = new PixelInfoDialog(mContext, pois, mDialogUtil,mBaiduMap);
//                        dialog.show();
//
//                        long endTime = System.currentTimeMillis();
//                        Log.e("MainActivity", "endTime:" + endTime);
//                        long xiangjian = endTime - startTime;
//                        Log.e("MainActivity", "xiangjian:" + xiangjian);
//
//
////                        if (layerId.equals("layerId")) {  //不同图层的点(每个图层的点信息结构应该相同)
////                            map = new HashMap<>();
////                            map.put("第一", new ElementaryFragment());
////                            map.put("第二", new MapFragment());
////                            map.put("第三", new ZipFragment());
////                            DialogFragmentWindow_Modity dialog = new DialogFragmentWindow_Modity(map,pois,basePointInfo);
////                            dialog.show(getSupportFragmentManager(), "");
////
////                        }else{                           //另一个图层的点
////                            map = new HashMap<>();
////                            map.put("第一", new MapFragment());
////                            DialogFragmentWindow_Modity dialog = new DialogFragmentWindow_Modity(map,pois,basePointInfo);
////                            dialog.show(getSupportFragmentManager(), ""); //Activity继承FragmentActivity才可以有getSupportFragmentManager
////                        }
//
//                    }
//                    return true;
//                }
//            });

        }
    };

MapUtils

package com.example.hasee.baidutest.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.SnapshotReadyCallback;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.Circle;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.Gradient;
import com.baidu.mapapi.map.HeatMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polygon;
import com.baidu.mapapi.map.PolygonOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.map.WeightedLatLng;
import com.baidu.mapapi.map.offline.MKOLSearchRecord;
import com.baidu.mapapi.map.offline.MKOLUpdateElement;
import com.baidu.mapapi.map.offline.MKOfflineMap;
import com.baidu.mapapi.map.offline.MKOfflineMapListener;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;
import com.baidu.mapapi.utils.CoordinateConverter;
import com.baidu.mapapi.utils.CoordinateConverter.CoordType;
import com.baidu.mapapi.utils.DistanceUtil;
import com.baidu.mapapi.utils.SpatialRelationUtil;
import com.example.hasee.baidutest.widget.CircleDrawable;
import com.example.hasee.baidutest.widget.PerformanceIndexDrawable;
import com.example.hasee.baidutest.widget.PerformanceIndexDrawable.PerformanceIndex;
import com.example.hasee.baidutest.widget.PerformanceIndexDrawable2;
import com.example.hasee.baidutest.widget.TriangleDrawable;

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

/**
 * 百度地图工具类
 *
 * @author Liao_Yang
 * @Description
 * @data2015年12月28日 下午4:26:13
 */
public class MapUtils {
    /**
     * 开始定位
     */
    public static void initLocation(BaiduMap baiduMap, LocationClient locClient, BDLocationListener listener) {
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f); //缩放等级
        baiduMap.setMyLocationEnabled(true);// 开启定位图层
        // 普通地图
        baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        baiduMap.setMapStatus(msu);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);// 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setAddrType("all");
        option.setScanSpan(1000);
        locClient.setLocOption(option);
        locClient.registerLocationListener(listener);
        locClient.start();
    }

    /**
     * 切换地图俯仰角
     */
    public static void Map3D(BaiduMap baiduMap, int overlook) {
        MapStatus status = new MapStatus.Builder().overlook(overlook).build();
        baiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(status));
    }

    /**
     * 关闭定位
     */
    public static void stopLocation(BaiduMap baiduMap, LocationClient locClient) {
        if (null != locClient && null != baiduMap) {
            locClient.stop();
            baiduMap.setMyLocationEnabled(false);
        }
    }

    /**
     * 添加点位至定位图层
     */
    public static void addMyLocation(BaiduMap baiduMap, BDLocation location) {
        MyLocationData locData = new MyLocationData.Builder().accuracy(location.getRadius())
                .direction(location.getDirection()).latitude(location.getLatitude()).longitude(location.getLongitude())
                .build();
        baiduMap.setMyLocationData(locData);
    }

    /**
     * 移动地图至指定点位
     */
    public static void moveMapToNewLatLng(BaiduMap baiduMap, double lat, double lng) {
        LatLng ll = new LatLng(lat, lng);
        MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
        baiduMap.animateMapStatus(u);
    }

    /**
     * 改变地图缩放等级 处理缩放 sdk 缩放级别范围: [3.0,19.0]
     */
    public static void setZoomLevel(BaiduMap baiduMap, float zoomLevel) {
        MapStatusUpdate u = MapStatusUpdateFactory.zoomTo(zoomLevel);
        baiduMap.animateMapStatus(u);
    }

    /**
     * 获取地图缩放等级
     */
    public static int getZoomLevel(BaiduMap baiduMap) {
        int zoom = Math.round(baiduMap.getMapStatus().zoom);
        return zoom;
    }

    public static void mapZoomIn(BaiduMap baiduMap) {
        int zoomLevel = getZoomLevel(baiduMap) + 1;
        if (zoomLevel <= 20) {
            setZoomLevel(baiduMap, zoomLevel);
        } else {
            setZoomLevel(baiduMap, 20);
        }
    }

    public static void mapZoomOut(BaiduMap baiduMap) {
        int zoomLevel = getZoomLevel(baiduMap) - 1;
        if (zoomLevel >= 3) {
            setZoomLevel(baiduMap, zoomLevel);
        } else {
            setZoomLevel(baiduMap, 3);
        }
    }

    /**
     * 获取屏幕截图 rect=null 为全屏
     */
    public static void getScreenshot(BaiduMap baiduMap, Rect rect, SnapshotReadyCallback callback) {
        baiduMap.snapshotScope(rect, callback);
    }

    /**
     * 改变地图状态为普通
     */
    public static void changeMapTypeNormall(BaiduMap baiduMap) {
        baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
    }

    /**
     * 改变地图状态为卫星
     */
    public static void changeMapTypeSatellite(BaiduMap baiduMap) {
        baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
    }

    /**
     * 获取路线规划实例
     */
    public static RoutePlanSearch initRoutePlan(OnGetRoutePlanResultListener listener) {
        RoutePlanSearch search = RoutePlanSearch.newInstance();
        search.setOnGetRoutePlanResultListener(listener);
        return search;
    }

    /**
     * 线路规划
     */
    public static void routePlan(RoutePlanSearch search, LatLng stLL, LatLng enLL) {
        PlanNode stNode = PlanNode.withLocation(stLL);
        PlanNode enNode = PlanNode.withLocation(enLL);
        search.drivingSearch(new DrivingRoutePlanOption().from(stNode).to(enNode));
    }


    /**
     * 获取离线地图服务实例
     */
    public static MKOfflineMap initOffline(MKOfflineMapListener listener) {
        MKOfflineMap offline = new MKOfflineMap();
        offline.init(listener);
        return offline;
    }

    /**
     * 获取所有已下载地图列表
     */
    public static ArrayList<MKOLUpdateElement> getDownloadMapList(MKOfflineMap offline) {
        ArrayList<MKOLUpdateElement> array = null;
        array = offline.getAllUpdateInfo();
        return array;
    }

    /**
     * 获取所有可下载地图列表
     */
    public static ArrayList<MKOLSearchRecord> getAllCityList(MKOfflineMap offline) {
        ArrayList<MKOLSearchRecord> array = null;
        array = offline.getOfflineCityList();
        return array;
    }

    /**
     * 获取建议搜索服务实例
     */
    public static SuggestionSearch initSuggestionSearch(OnGetSuggestionResultListener listener) {
        SuggestionSearch suggesionSearch = SuggestionSearch.newInstance();
        suggesionSearch.setOnGetSuggestionResultListener(listener);
        return suggesionSearch;
    }

    /**
     * 使用建议搜索服务获取建议列表
     */
    public static void requestSuggestion(SuggestionSearch suggesionSearch, String city, String keyword) {
        suggesionSearch.requestSuggestion(new SuggestionSearchOption().city(city).keyword(keyword));
    }

    /**
     * 获取poi搜索服务实例
     */
    public static PoiSearch initPoiSearch(OnGetPoiSearchResultListener listener) {
        PoiSearch poiSearch = PoiSearch.newInstance();
        poiSearch.setOnGetPoiSearchResultListener(listener);
        return poiSearch;
    }

    /**
     * poi搜索
     */
    public static void requestPoiSearch(PoiSearch poiSearch, String city, String keyword, int capacity, int pageNum) {
        poiSearch.searchInCity(
                new PoiCitySearchOption().city(city).keyword(keyword).pageCapacity(capacity).pageNum(pageNum));
    }

//    /**
//     * 添加poi结果至地图
//     */
//    public static PoiOverlay addPoiResultOnMap(BaiduMap baiduMap, PoiResult result) {
//        PoiOverlay overlay = new PoiOverlay(baiduMap);
//        overlay.setData(result);
//        overlay.addToMap();
//        return overlay;
//    }

    /**
     * 添加覆盖物至地图
     */
    public static Marker addOverlayToMap(BaiduMap baiduMap, LatLng ll, BitmapDescriptor bitmap) {
        OverlayOptions options = new MarkerOptions().position(ll).icon(bitmap);
        Marker marker = (Marker) baiduMap.addOverlay(options);
        return marker;
    }

    public static Marker addOverlayToMap(BaiduMap baiduMap, LatLng ll, BitmapDescriptor bitmap, float rotate,
                                         int index) {
        OverlayOptions options = new MarkerOptions().position(ll).icon(bitmap).rotate(rotate).zIndex(index);
        Marker marker = (Marker) baiduMap.addOverlay(options);
        return marker;
    }

    public static Marker addOverlayToMap(BaiduMap baiduMap, LatLng ll, BitmapDescriptor bitmap, float rotate, int index,
                                         float anchorX) {
        OverlayOptions options = new MarkerOptions().position(ll).icon(bitmap).rotate(rotate).zIndex(index)
                .anchor(anchorX, 1f);
        Marker marker = (Marker) baiduMap.addOverlay(options);
        return marker;
    }

    public static Marker addOverlayToMap(BaiduMap baiduMap, LatLng ll, BitmapDescriptor bitmap, float rotate, int index,
                                         float anchorX, float anchorY) {
        OverlayOptions options = new MarkerOptions().position(ll).icon(bitmap).rotate(rotate).zIndex(index)
                .anchor(anchorX, anchorY);
        Marker marker = (Marker) baiduMap.addOverlay(options);
        return marker;
    }

    /**
     * 更新mark坐标
     */
    public static void updateMarkerLatLng(Marker marker, LatLng ll) {
        marker.setPosition(ll);
    }

    /**
     * 绘制折线
     */
    public static Polyline addPolylineToMap(BaiduMap baiduMap, List<LatLng> points, int width, int color) {
        OverlayOptions ooPolyline = new PolylineOptions().points(points).width(width).color(color);
        Polyline polyline = (Polyline) baiduMap.addOverlay(ooPolyline);
        return polyline;
    }

    /**
     * 更新折线数据
     */
    public static void setPolylinePoint(Polyline polyline, List<LatLng> points) {
        polyline.setPoints(points);
    }

    /**
     * 绘制多边形
     */
    public static Polygon addPolygonToMap(BaiduMap baiduMap, List<LatLng> points, int width, int color,
                                          int innerColor) {
        OverlayOptions ooPolygon = new PolygonOptions().points(points).stroke(new Stroke(width, color))
                .fillColor(innerColor);
        Polygon polygon = (Polygon) baiduMap.addOverlay(ooPolygon);
        return polygon;
    }

    /**
     * 绘制多边形
     */
    public static Polygon addPolygonToMap(BaiduMap baiduMap, List<LatLng> points, int width, int color, int innerColor,
                                          int index) {
        OverlayOptions ooPolygon = new PolygonOptions().points(points).stroke(new Stroke(width, color))
                .fillColor(innerColor).zIndex(index);
        Polygon polygon = (Polygon) baiduMap.addOverlay(ooPolygon);
        return polygon;
    }

    public static Circle addCircleToMap(BaiduMap baiduMap, LatLng center, int radius, int fillColor, int strokeColor) {
        Stroke stroke = new Stroke(1, strokeColor);
        CircleOptions circleOptions = new CircleOptions().center(center).radius(radius).fillColor(fillColor)
                .stroke(stroke);
        Circle circle = (Circle) baiduMap.addOverlay(circleOptions);
        return circle;
    }

    /**
     * 更新多边形数据
     */
    public static void setPolygonPoint(Polygon polygon, List<LatLng> points) {
        polygon.setPoints(points);
    }

    /**
     * 删除覆盖物
     */
    public static void removeOverlayFromMap(Overlay overlay) {
        if (null != overlay) {
            overlay.remove();
        }
    }

    /**
     * 保存信息至覆盖物
     */
    public static void saveDataWithOverlay(Overlay overlay, Bundle b) {
        Bundle extraInfo = overlay.getExtraInfo();
        if (extraInfo == null) {
            overlay.setExtraInfo(b);
        }
    }

    /**
     * 获取覆盖物保存信息
     */
    public static Bundle getDataWithOverlay(Overlay overlay) {
        return overlay.getExtraInfo();
    }

    /**
     * 获取两点间距离 单位:米
     */
    public static double getDistance(LatLng stLL, LatLng enLL) {
        return DistanceUtil.getDistance(stLL, enLL);
    }

    /**
     * 清空地图
     */
    public static void clearMap(BaiduMap baiduMap) {
        baiduMap.clear();
    }

    /**
     * 将屏幕坐标转换成经纬度
     */
    public static LatLng fromPointToLatLng(BaiduMap baiduMap, Point p) {
        LatLng ll = baiduMap.getProjection().fromScreenLocation(p);
        return ll;
    }

    /**
     * 获取地理编码查询实例
     */
    public static GeoCoder initGeoCoder(OnGetGeoCoderResultListener listener) {
        GeoCoder search = GeoCoder.newInstance();
        search.setOnGetGeoCodeResultListener(listener);
        return search;
    }

    /**
     * 反地理编码查询
     */
    public static void reverseGeoCode(GeoCoder search, LatLng ll) {
        search.reverseGeoCode(new ReverseGeoCodeOption().location(ll));
    }

    /**
     * 地理编码查询
     */
    public static void geocode(GeoCoder search, String address, String city) {
        search.geocode(new GeoCodeOption().address(address).city(city));
    }

    /**
     * GPS坐标转换百度坐标
     */
    public static LatLng fromGPSToBaidu(LatLng ll) {
        if (null != ll) {
            CoordinateConverter converter = new CoordinateConverter();
            converter.from(CoordType.GPS);
            converter.coord(ll);
            return converter.convert();
        }
        return null;
    }

    /**
     * 百度坐标转gps
     */
    public static LatLng fromBaiduToGPS(LatLng ll) {
        if (null != ll) {
            LatLng convertBaidu = fromGPSToBaidu(ll);
            double resultLat = 2 * ll.latitude - convertBaidu.latitude;
            double resultLong = 2 * ll.longitude - convertBaidu.longitude;
            return new LatLng(resultLat, resultLong);
        }
        return null;
    }

    /**
     * 获取地图信息
     */
    public static LatLng[] getMapBounds(BaiduMap baiduMap) {
        LatLng rt = baiduMap.getMapStatus().bound.northeast;
        LatLng lb = baiduMap.getMapStatus().bound.southwest;
        return new LatLng[]{rt, lb};
    }

    /**
     * 获取地图左上角和右下角的经纬度
     */
    public static LatLng[] getMapLTandRB(BaiduMap baiduMap) {
        LatLng rt = baiduMap.getMapStatus().bound.northeast;
        LatLng lb = baiduMap.getMapStatus().bound.southwest;
        return new LatLng[]{new LatLng(rt.latitude, lb.longitude), new LatLng(lb.latitude, rt.longitude)};
    }

    /**
     * 获取三角型覆盖物
     */
    public static BitmapDescriptor getTriangleDrawable(int width, int height, int color, Style style) {
        Drawable drawable = new TriangleDrawable(color, style);
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        return BitmapDescriptorFactory.fromBitmap(bitmap);
    }

    public static BitmapDescriptor getRoadDrawable(int color) {
        Drawable drawable = new CircleDrawable(color);
        Bitmap bitmap = Bitmap.createBitmap(40, 40, Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, 40, 40);
        drawable.draw(canvas);
        return BitmapDescriptorFactory.fromBitmap(bitmap);
    }

    public static BitmapDescriptor getBigRoadDrawable(int color) {
        Drawable drawable = new CircleDrawable(color, 13);
        Bitmap bitmap = Bitmap.createBitmap(40, 40, Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, 40, 40);
        drawable.draw(canvas);
        return BitmapDescriptorFactory.fromBitmap(bitmap);
    }

    /**
     * * 获取待性能指标覆盖物
     *
     * @param color       扇区颜色
     * @param style       扇区样式:Fill/Stroke
     * @param sWidth      扇区宽
     * @param sHeight     扇区高
     * @param angle       扇区角度
     * @param selfCompare 自身弱覆盖
     * @param over        越区覆盖
     * @param lap         重叠覆盖
     * @param compare     比较
     * @return
     */
    public static BitmapDescriptor getTriangleDrawable(int color, Style style, int sWidth, int sHeight, float angle,
                                                       int offsetLevel, String selfCompare, String over, String lap,
                                                       String compare) {
        PerformanceIndexDrawable drawable = new PerformanceIndexDrawable(color, style, sWidth, sHeight, angle);
        if ("弱".equals(selfCompare)) {
            drawable.addRect(PerformanceIndex.TYPE_RUO, selfCompare, Color.parseColor("#43D012"));
        }
        if (!Utils.isEmpty(over) && !"0".equals(over)) {
            int overColor = 0;
            try {
                int overCount = Integer.parseInt(over);
                if (overCount <= 5) {
                    overColor = Color.parseColor("#999999");
                } else if (overCount <= 10) {
                    overColor = Color.parseColor("#F742B9");
                } else {
                    overColor = Color.parseColor("#ff0000");
                }
            } catch (Exception e) {
            }
            drawable.addRect(PerformanceIndex.TYPE_GUO, over, overColor);
        }
        if (!Utils.isEmpty(lap) && !"0".equals(lap)) {
            int lapColor = 0;
            try {
                int lapCount = Integer.parseInt(lap);
                if (lapCount <= 5) {
                    lapColor = Color.parseColor("#999999");
                } else if (lapCount <= 10) {
                    lapColor = Color.parseColor("#F742B9");
                } else {
                    lapColor = Color.parseColor("#ff0000");
                }
            } catch (Exception e) {
            }
            drawable.addRect(PerformanceIndex.TYPE_CHONG, lap, lapColor);
        }
        if (!"弱".equals(selfCompare) && "弱".equals(compare)) {
            drawable.addRect(PerformanceIndex.TYPE_WEAK, "弱", Color.parseColor("#999999"));
        } else if ("弱".equals(selfCompare) && "弱".equals(compare)) {
            drawable.addRect(PerformanceIndex.TYPE_WEAK, "弱", Color.parseColor("#FF0000"));
        } else if ("弱".equals(selfCompare) && "强".equals(compare)) {
            drawable.addRect(PerformanceIndex.TYPE_WEAK, "强", Color.parseColor("#7F007F"));
        }
//        Bitmap bp = Utils.drawable2bitmap(drawable);
        Bitmap bp = Utils.drawable2bitmap(drawable,angle);
        return BitmapDescriptorFactory.fromBitmap(bp);
    }

    public static BitmapDescriptor getTriangleDrawable(int leftOrRight, int color, Style style, int sWidth, int sHeight,
                                                       float angle, int offsetLevel, String selfCompare, String over,
                                                       String lap, String compare) {
        /**
         * 初始化构造函数,还未调用对象里的方法
         */
        // if (MapFragment.ab % 2 == 0){
//        PerformanceIndexDrawable drawable = new PerformanceIndexDrawable(color, style, sWidth, sHeight, angle);
        PerformanceIndexDrawable2 drawable = new PerformanceIndexDrawable2(color, style, sWidth, sHeight, leftOrRight);
//        PerformanceIndexDrawable2_copy drawable = new PerformanceIndexDrawable2_copy(color, style, sWidth, sHeight,
//                leftOrRight,angle);
        if (!Utils.isEmpty(over) && !"0".equals(over)) {
            int overColor = 0;
            try {
                int overCount = Integer.parseInt(over);
                if (overCount <= 5) {
                    overColor = Color.parseColor("#999999");
                } else if (overCount <= 10) {
                    overColor = Color.parseColor("#F742B9");
                } else {
                    overColor = Color.parseColor("#ff0000");
                }
            } catch (Exception e) {
            }
            drawable.addRect(PerformanceIndex.TYPE_GUO, over, overColor);
        }
        if (!Utils.isEmpty(lap) && !"0".equals(lap)) {
            int lapColor = 0;
            try {
                int lapCount = Integer.parseInt(lap);
                if (lapCount <= 5) {
                    lapColor = Color.parseColor("#999999");
                } else if (lapCount <= 10) {
                    lapColor = Color.parseColor("#F742B9");
                } else {
                    lapColor = Color.parseColor("#ff0000");
                }
            } catch (Exception e) {
            }
            drawable.addRect(PerformanceIndex.TYPE_CHONG, lap, lapColor);
        }
        if ("弱".equals(selfCompare) && "弱".equals(compare)) {
            drawable.addRect(PerformanceIndex.TYPE_WEAK, "弱", Color.parseColor("#FF0000"));
        } else if ("弱".equals(selfCompare) && "强".equals(compare)) {
            drawable.addRect(PerformanceIndex.TYPE_WEAK, "弱", Color.parseColor("#999999"));
        }
//        Bitmap bp = Utils.drawable2bitmap(drawable);
        Bitmap bp = Utils.drawable2bitmap(drawable,angle); //旋转角度
        return BitmapDescriptorFactory.fromBitmap(bp);
        // }else{
        // PerformanceIndexDrawable3 drawable = new
        // PerformanceIndexDrawable3(color, style, sWidth, sHeight,leftOrRight);
        // if (!Utils.isEmpty(over) && !"0".equals(over)) {
        // int overColor = 0;
        // try {
        // int overCount = Integer.parseInt(over);
        // if (overCount <= 5) {
        // overColor = Color.parseColor("#999999");
        // } else if (overCount <= 10) {
        // overColor = Color.parseColor("#F742B9");
        // } else {
        // overColor = Color.parseColor("#ff0000");
        // }
        // } catch (Exception e) {
        // }
        // drawable.addRect(PerformanceIndex.TYPE_GUO, over, overColor);
        // }
        // if (!Utils.isEmpty(lap) && !"0".equals(lap)) {
        // int lapColor = 0;
        // try {
        // int lapCount = Integer.parseInt(lap);
        // if (lapCount <= 5) {
        // lapColor = Color.parseColor("#999999");
        // } else if (lapCount <= 10) {
        // lapColor = Color.parseColor("#F742B9");
        // } else {
        // lapColor = Color.parseColor("#ff0000");
        // }
        // } catch (Exception e) {
        // }
        // drawable.addRect(PerformanceIndex.TYPE_CHONG, lap, lapColor);
        // }
        // if ("弱".equals(selfCompare) && "弱".equals(compare)) {
        // drawable.addRect(PerformanceIndex.TYPE_WEAK, "弱",
        // Color.parseColor("#FF0000"));
        // } else if ("弱".equals(selfCompare) && "强".equals(compare)) {
        // drawable.addRect(PerformanceIndex.TYPE_WEAK, "弱",
        // Color.parseColor("#999999"));
        // }
        // Bitmap bp = Utils.drawable2bitmap(drawable);
        // return BitmapDescriptorFactory.fromBitmap(bp);
        // }
    }

    /**
     * 获取两点间角度
     */
    public static double getAngle(List<LatLng> points) {
        double angle = 0;

        LatLng stLL = points.get(0);
        LatLng enLL = points.get(1);

        double x = enLL.longitude - stLL.longitude;
        double y = enLL.latitude - stLL.latitude;

        if (y == 0 && x > 0) {
            angle = 90;
        } else if (y == 0 && x < 0) {
            angle = 270;
        } else if (x == 0 && y > 0) {
            angle = 0;
        } else if (x == 0 && y < 0) {
            angle = 180;
        } else if (x > 0 && y > 0) {
            angle = Math.atan((x) / (y)) * 180 / Math.PI;
        } else if (x > 0 && y < 0) {
            angle = 180 + Math.atan((x) / (y)) * 180 / Math.PI;
        } else if (x < 0 && y < 0) {
            angle = 180 + Math.atan((x) / (y)) * 180 / Math.PI;
        } else if (x < 0 && y > 0) {
            angle = 360 + Math.atan((x) / (y)) * 180 / Math.PI;
        }

        return angle;
    }

    /**
     * 添加热力图
     */
    public static HeatMap addHeatMap(BaiduMap baiduMap, List<WeightedLatLng> lls) {
        HeatMap heatmap = null;
        if (null != baiduMap) {
            int[] DEFAULT_GRADIENT_COLORS = {Color.rgb(102, 225, 0), Color.rgb(255, 0, 0)};
            float[] DEFAULT_GRADIENT_START_POINTS = {0.2f, 1f};
            Gradient gradient = new Gradient(DEFAULT_GRADIENT_COLORS, DEFAULT_GRADIENT_START_POINTS);
            heatmap = new HeatMap.Builder().weightedData(lls).gradient(gradient).radius(20).build();
            baiduMap.addHeatMap(heatmap);
        }
        return heatmap;
    }

    /**
     * 移除热力图
     */
    public static void removeheatMap(HeatMap heatMap) {
        heatMap.removeHeatMap();
    }

    public static boolean isPolygonContainsPoint(List<LatLng> points, LatLng point) {
        boolean isInFlag = SpatialRelationUtil.isPolygonContainsPoint(points, point);
        return isInFlag;
    }
}

上一篇下一篇

猜你喜欢

热点阅读