高德地图实用技能

2019-07-23  本文已影响0人  cyq7on

最近一个项目中用到了高德地图Android SDK,做了一些略微复杂一点的效果和功能,现在将其抽离出来,整理为一篇关于高德地图的实用技能。

前言

1、基础jar包选择

最基础的地图功能有两个jar包可以实现,一个是3d的,一个是2d。2d的只需要一个jar包,3d的还要引入so文件,apk体积会增大。以我的经验,但凡复杂一点的功能,还是优先使用3d的,以下内容均是基于3d包。

2、jar包分类

设置缩放按钮是否可见

高德sdk自带缩放按钮功能,但是一般都不满足设计师的要求。
uiSettings.setZoomControlsEnabled(false);

设置旋转手势是否可用

uiSettings.setRotateGesturesEnabled(false);

设置旋转手势可用,实现指南针效果

aMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
    @Override
    public void onCameraChange(CameraPosition cameraPosition) {

        startIvCompass(cameraPosition.bearing);
    }

    @Override
    public void onCameraChangeFinish(CameraPosition cameraPosition) {
    }
});

// 指南针旋转
private void rotateCompass(float bearing) {
    bearing = 360 - bearing;
    RotateAnimation rotateAnimation = new RotateAnimation(lastBearing, bearing, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotateAnimation.setFillAfter(true);
    ivCompass.startAnimation(rotateAnimation);
    lastBearing = bearing;
}

设置倾斜手势是否可用

uiSettings.setTiltGesturesEnabled(false);

缩放移动地图,保证所有自定义marker在可视范围中

代码基本参考高德地图官方demo

//缩放移动地图,保证所有自定义marker在可视范围中。
    public void zoomToSpan(LatLng centerPoint) {
        List<LatLng> pointList = new ArrayList<>();

        // add latlan of your markers to poinList
         ...

        if (pointList.isEmpty()) {
            if(centerPoint == null){
                return;
            }else {
                aMap.moveCamera(CameraUpdateFactory.changeLatLng(centerPoint));
            }
        }

        LatLngBounds bounds;
        if (centerPoint == null) {
            bounds = getLatLngBounds(pointList);
        }else {
            bounds = getLatLngBounds(centerPoint, pointList);
        }
        aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 150));
    }

    //根据中心点和自定义内容获取缩放bounds
    private LatLngBounds getLatLngBounds(LatLng centerPoint, List<LatLng> pointList) {
        LatLngBounds.Builder b = LatLngBounds.builder();
        if (centerPoint != null){
            for (int i = 0; i < pointList.size(); i++) {
                LatLng p = pointList.get(i);
                LatLng p1 = new LatLng((centerPoint.latitude * 2) - p.latitude, (centerPoint.longitude * 2) - p.longitude);
                b.include(p);
                b.include(p1);
            }
        }
        return b.build();
    }

    //根据自定义内容获取缩放bounds
    private LatLngBounds getLatLngBounds(List<LatLng> pointList) {
        LatLngBounds.Builder b = LatLngBounds.builder();
        for (int i = 0; i < pointList.size(); i++) {
            LatLng p = pointList.get(i);
            b.include(p);
        }
        return b.build();
    }

添加marker

LatLng latLng = new LatLng(markerInfo.lat, markerInfo.lon);
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.start);
MarkerOptions markerOptions = new MarkerOptions().setFlat(true)
        .anchor(0.5f, 0.5f)
        .infoWindowEnable(false)
        .position(latLng)
        .icon(bitmapDescriptor);
aMap.addMarker(markerOptions);
LatLng latLng = new LatLng(markerInfo.lat, markerInfo.lon);
View v = getLayoutInflater().inflate(R.layout.info_window, null);
TextView tvInfo = v.findViewById(R.id.tv_info);
tvInfo.setText(info);
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromView(v);
MarkerOptions markerOptions = new MarkerOptions().setFlat(true)
        .anchor(0.5f, 0.5f)
        .title(info)
        .infoWindowEnable(false)
        .position(latLng)
        .icon(bitmapDescriptor);
aMap.addMarker(markerOptions);

其中这三个api值得注意:


api

使用布局文件的场景是:marker不再是单一图片;或者说所有marker需要同时显示文本信息,而使用sdk原生的弹出InfoWindow的方式,最终只有最后一个调用该方法的marker会弹出。

添加折线,实现轨迹效果

    /**
     * 添加折线
     * @param latLng 经纬度
     * @param dotted 是否虚线
     * @param color 颜色
     * @param width 宽度
     * @return 折线
     */
    private Polyline addPolyline(LatLng latLng, boolean dotted, int color, int width) {
        return aMap.addPolyline((new PolylineOptions())
                .add(latLng)
                .width(width)
                .setDottedLine(dotted)
                .color(color));
    }

gif文件太大,无法上传,上传几张图片,源码和动图在文末。


旋转手势
轨迹、点击marker

源码和动图

上一篇 下一篇

猜你喜欢

热点阅读