高德地图Amap常用功能总结

设置缩放比例

1,设置缩放比例的api是

aMap.moveCamera(CameraUpdateFactory.zoomTo(18));

如果你直接设置是没用的,因为此时地图还没加载成功。所以要监听地图加载成功的事件

aMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() {
    @Override
    public void onMapLoaded() {
        aMap.moveCamera(CameraUpdateFactory.zoomTo(18));
    }
});

导航算路成功后点击路线切换

点击回调的polyline id跟导航id是没有任何关联的,只能通过判断整条线路所有点是否包含polyline的点来判断用户点击是哪条线段。这里取得点是polyline的中间点作参考点

map.setOnPolylineClickListener(polyline -> onClickPolyline(polyline));
private void onClickPolyline(Polyline polyline) {
        //突出选择的那条路
        if (routeOverlays != null && routeOverlays.size() == 1) {
            return;
        }
        List<LatLng> polylinePoints = polyline.getPoints();
        LatLng polylinePoint = polylinePoints.get(polylinePoints.size() / 2);
        NaviLatLng naviLatLng = new NaviLatLng(polylinePoint.latitude, polylinePoint.longitude);
        for (int i = 0; i < routeOverlays.size(); i++) {
            RouteOverLay routeOverLay = routeOverlays.valueAt(i);
            List<NaviLatLng> coordList = routeOverLay.getAMapNaviPath().getCoordList();
            if (coordList.contains(naviLatLng)) {
                routeIndex = i;
                routeOverLay.setTransparency(1.0f);
                routeOverLay.setZindex(1);
                aMapNavi.selectRouteId(routeOverlays.keyAt(i));
                updateFlexibleView(aMapNavi.getNaviPath());
                Log.i(TAG, "onPolylineClick: true: " + i);
            } else {
                routeOverLay.setTransparency(0.4f);
                routeOverLay.setZindex(0);
                Log.i(TAG, "onPolylineClick: false: " + i);
            }
        }
    }

将算路的路径在限定区域显示 

  /**
     * 移动镜头到当前的视角。
     *
     * @since V2.1.0
     */
    public void zoomToSpan() {
        if (startPoint != null) {
            if (mAMap == null) {
                return;
            }
            try {
            LatLngBounds bounds = getLatLngBounds();
//            LatLng latLng = new LatLng(((startPoint.latitude + endPoint.latitude) / 2), ((startPoint.longitude + endPoint.longitude) / 2 - 0.2));
            mAMap.animateCamera(CameraUpdateFactory
                    .newLatLngBoundsRect(bounds, Utils.dp2px(550), Utils.dp2px(50), Utils.dp2px(50), Utils.dp2px(50)));
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }

    protected LatLngBounds getLatLngBounds() {
        LatLngBounds.Builder b = LatLngBounds.builder();
        b.include(new LatLng(startPoint.latitude, startPoint.longitude));
        b.include(new LatLng(endPoint.latitude, endPoint.longitude));
        return b.build();
    }

amap切换到后台大概3分钟后在进入显示白屏的情况

分析:多半是因为onResume()后做了其他操作,比如挪动中心点等地图操作。大概原因是此时地图正在绘制,还未加载完毕。此刻挪动中心点就会出现白屏问题

 @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
        if (currentLocation != null) {
            Log.i(TAG, "onResume: currentlocation not null");
            map.clear();
            moveToPosition(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()));
        }
    }

解决方式:在地图加载完成后挪动中心点


map.addOnMapLoadedListener(() -> { moveToPosition(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()));});

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值