首页 > 编程知识 正文

高德地图比例尺自动缩放,高德地图比例尺设置

时间:2023-05-05 09:09:07 阅读:166185 作者:462

高德地图有附带的比例尺,但不能改变位置。 只能固定在画面的右下角。 做项目的时候,如果有需要必须在某个位置的话,就只能定制了。

虽然我们在gold拥有的比例上进行了测试,但由于同一比例等级、纬度经度的不同比例可能会有所不同,因此无法按照比例等级进行处理。

幸运的是,高德地图提供了相应的api

比例尺数据: AMap. getScalePerPixel ()可以获取与当前地图级别的一个像素点相对应的距离长度(米),并自定义比例尺长度(例如100像素)。

效果图表如下。

完整代码: import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import Android.support.annotation.nullable; import Android.util.attributes et; import android.view.View; import com.amap.api.maps.AMap; import com.autonavi.AMAP.map core.d point; import com.autonavi.AMAP.map core.I point; import com.autonavi.AMAP.map core.virtual earth projection;/* * * @ author gcz * blog https://blog.csdn.net/z 1998 01 15/article/details/78795329 */publicfinalclassmapscaleviewexaiewexaiexaion //比例尺文本private String text=' '; //比例尺宽度private int lineWidth=50; //视图高度private int viewHeight; //比例尺线刷private Paint linePaint; //比例尺文字刷private Paint textPaint; //文本区域private Rect rect; 私有I point point; //1dp为px的值private float dp1=0.0F; privatefinalint [ ] level=new int [ ] { 100000000,5000000,2000000,1000000,500000,50000,300000 } publicmapscaleview (context context,@Nullable AttributeSet attrs ) this ) context,attrs,0 ); } publicmapscaleview (context context,@Nullable AttributeSet attrs,int defStyleAttr ) super ) context,attrs,defstyleatttr } privatevoidinit (上下文上下文) this.rect=new rect (0,0,0,10 ); this.linePaint=new Paint (; this.line paint.set antialias (true; this.line paint.setcolor (color.white ); this.line paint.setstrokewidth (2.0f * a ); this.line paint.set style (paint.style.stroke ); this.textPaint=new Paint (; this.text paint.set antialias (true ); this.text paint.setcolor (color.white ); this.text paint.settext size (20.0 f * a ); this.DP1=(float ) getDP1 ) context; this.point=new

IPoint(); } public void destroy() { this.linePaint = null; this.textPaint = null; this.rect = null; this.text = null; this.point = null; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (this.text != null && !this.text.equals("") && this.lineWidth != 0) { this.textPaint.getTextBounds(this.text, 0, this.text.length(), this.rect); int x = 1; int y = viewHeight - this.rect.height() + 5; canvas.drawText(this.text, (this.lineWidth - this.rect.width()) / 2, y, this.textPaint); y += this.rect.height() - 5; //左竖线 canvas.drawLine(x, y - 3.0F * this.dp1, x, y + a, this.linePaint); //横线 canvas.drawLine(x, y - x, this.lineWidth, y - x, this.linePaint); //右竖线 canvas.drawLine(this.lineWidth, y - 3.0F * this.dp1, this.lineWidth, y + a, this.linePaint); } } public void refreshScaleView(AMap aMap) { if (aMap == null) return; try { float bldty = aMap.getCameraPosition().bldty; //获取地图中心坐标 aMap.getP20MapCenter(this.point); if (this.point != null) { DPoint dPoint = VirtualEarthProjection.PixelsToLatLong((long) this.point.x, (long) this.point.y, 20); float var3 = 1; //屏幕上1像素点对应的地图上距离长度,单位米,等同于aMap.getScalePerPixel(); double scalePerPixel = (double) ((float) (Math.cos(dPoint.y * 3.141592653589793D / 180.0D) * 2.0D * 3.141592653589793D * 6378137.0D / (256.0D * Math.pow(2.0D, (double) bldty)))); //比例尺宽度 int lineWidth = (int) ((double) this.level[(int) bldty] / (scalePerPixel * (double) var3)); String lineText = formatDistance(this.level[(int) bldty]); //如果超过最大宽度,则向下一级转换 while (lineWidth > maxWidth && bldty < 19) { bldty++; lineWidth = (int) ((double) this.level[(int) bldty] / (scalePerPixel * (double) var3)); lineText = formatDistance(this.level[(int) bldty]); } this.lineWidth = lineWidth; this.text = lineText; dPoint.recycle(); this.requestLayout(); this.invalidate(); } } catch (Throwable throwable) { throwable.printStackTrace(); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSize = MeasureSpec.getSize(widthMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int resultWidth; if (widthMode == MeasureSpec.EXACTLY) {//精确模式,写死宽度的情况 resultWidth = widthSize; } else { resultWidth = Math.max(lineWidth, rect.width()) + 2; if (widthMode == MeasureSpec.AT_MOST) {//最大模式,父容器给的最大空间 resultWidth = Math.min(resultWidth, widthSize); } } int heightSize = getHeightSize(heightMeasureSpec); viewHeight = heightSize; setMeasuredDimension(resultWidth, heightSize); } /** * 测量ScaleView的高度 */ private int getHeightSize(int heightMeasureSpec) { int mode = MeasureSpec.getMode(heightMeasureSpec); int height = 0; switch (mode) { case MeasureSpec.AT_MOST: height = (int) (rect.height() + 5 + 3F * this.dp1 + a); break; case MeasureSpec.EXACTLY: { height = MeasureSpec.getSize(heightMeasureSpec); break; } case MeasureSpec.UNSPECIFIED: { height = Math.max((int) (rect.height() + 5 + 3F * this.dp1 + a), MeasureSpec.getSize(heightMeasureSpec)); break; } } return height; } public static String formatDistance(int m) { String distance; if (m < 1000) { distance = m + "m"; } else { distance = m / 1000 + "km"; } return distance; } /** * 1dp转px */ public static int getDp1(Context var0) { float var1 = var0.getResources().getDisplayMetrics().density; return (int) (1.0F * var1 + 0.5F); }} 使用方法: 和正常view一样添加到layout中,注意修改包名 <com.xxx.xxx.MapScaleView android:id="@+id/blc" android:layout_width="wrap_content" android:layout_height="wrap_content" />

在OnCameraChangeListener的onCameraChange中调用比例尺的refreshScaleView()

override fun onCameraChange(p0: CameraPosition?) { if (p0 == null) returnblc.refreshScaleView(mMap)}

参考资料:
如何自定义指南针、缩放按钮、比例尺控件、定位按钮?
ArcGIS for Android 自定义地图比例尺

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。