【问题标题】:Creating a Crosshair in the Center of My Google Map在我的 Google 地图中心创建十字准线
【发布时间】:2010-06-30 16:15:13
【问题描述】:

我是 Android 开发的新手,我遇到了一个问题,我已经研究了几个小时但没有成功。我想在我的谷歌地图的中心创建一个十字准线,即使地图被平移,它也会保持在中心。我的可绘制目录中有一个十字准线的 .png 图像。此外,我有一个 MapView 显示带有多个标记的 Google 地图。解决这个问题的最佳方法是什么?

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    您需要制作一个 CrosshairOverlay。没有测试过。

    public class CrosshairOverlay extends Overlay {
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            Projection projection = mapView.getProjection();
            Point center = projection.toPixels(mapView.getMapCenter(), null);
    
            // Customize appearance, should be a fields.
            Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
            p.setColor(0xFF000000);
            p.setStyle(Style.STROKE);
            p.setStrokeWidth(2.0f);
            int innerRadius = 10;
            int outerRadius = 20;
    
            canvas.drawCircle(center.x, center.y, innerRadius, p);
            canvas.drawCircle(center.x, center.y, outerRadius, p);
            return true;
        }
    }
    

    【讨论】:

    • @Robby Pond 如何使用这个类在googleMap上绘制十字准线?
    【解决方案2】:
    public class CrossHairsOverlay extends Overlay {
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            super.draw(canvas, mapView, shadow);
            GeoPoint centerGp = mapView.getMapCenter();
            Projection projection = mapView.getProjection();
            Point centerPoint = projection.toPixels(centerGp, null);
            Paint p = new Paint();
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.crosshairs_dial);
            canvas.drawBitmap(bmp, centerPoint.x, centerPoint.y, p);
            return true;
        }
    }
    

    【讨论】:

    • 您可能还需要将位图居中: //如果您希望图像居中 canvas.drawBitmap(bmp, centerPoint.x - bmp.getWidth() / 2, centerPoint.y - bmp.getHeight()/2, p);
      OR

      //如果您希望图像底部居中canvas.drawBitmap(bmp, centerPoint.x - bmp.getWidth() / 2, centerPoint.y - bmp.getHeight(), p);
    【解决方案3】:

    这就是我为简单的 CrossHairOverlay 构建的(使用 Fragment 和适用于 Android 的 Google 地图 API2):

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
    
        <TextView
            android:id="@+id/crosshair_horizontal_line"
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:padding="1dp"
            android:background="@color/black"/>
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center">
    
        <TextView
            android:id="@+id/crosshair_vertical_line"
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:padding="2dp"
            android:background="@color/black"/>
    </LinearLayout>
    

    这是一个示例截图:

    【讨论】:

      【解决方案4】:

      我接受了 KingAlex1985 的回答,并使它更简单一些。它在屏幕中心产生一个带有点的十字准线,军事风格

      ````

      <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_centerInParent="true"
          android:foregroundGravity="center"
          android:gravity="center"
      
          android:orientation="horizontal">
      
          <View
              android:layout_width="15dp"
              android:layout_height="1dp"
              android:background="@android:color/black"
              />
          <View
              android:layout_width="5dp"
              android:layout_height="1dp"
              android:background="@android:color/transparent"
              />
          <View
              android:layout_width="2dp"
              android:layout_height="2dp"
              android:background="@android:color/black"
              />
          <View
              android:layout_width="5dp"
              android:layout_height="1dp"
              android:background="@android:color/transparent"
              />
          <View
              android:layout_width="15dp"
              android:layout_height="1dp"
              android:background="@android:color/black"
              />
      </LinearLayout>
      
      <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_centerInParent="true"
          android:foregroundGravity="center"
          android:gravity="center"
          android:orientation="vertical">
      
          <View
              android:layout_width="1dp"
              android:layout_height="15dp"
              android:background="@android:color/black"
              />
          <View
              android:layout_width="1dp"
              android:layout_height="11dp"
              android:background="@android:color/transparent"
              />
      
          <View
              android:layout_width="1dp"
              android:layout_height="15dp"
              android:background="@android:color/black"
              />
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-03
        • 1970-01-01
        • 2014-05-15
        • 2020-08-25
        相关资源
        最近更新 更多