Create a crosshair in the center of my Google map

I am new to Android development, and I have a problem that I have been working on for hours without success. I want to create a crosshair in the center of my Google Map, which will remain in the center even when the map is tinted. I have a .png image of a crosshair in my directory. In addition, I have a MapView that displays a Google map with multiple markers. What is the best way to solve this problem?

+2
source share
4 answers
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; } } 
+2
source

You will need to do CrosshairOverlay. Did not check it.

 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; } } 
+3
source

This is what I created for a simple CrossHairOverlay (using Fragment and Google maps API2 for Android):

 <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> 

Here is an example screenshot:

enter image description here

+1
source

I took KingAlex1985 answer and made it a little easier. It creates a crosshair on the center screen with a dot, military style.

`` ``

 <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> 

0
source

Source: https://habr.com/ru/post/1468795/


All Articles