Add crosshair on google map v2

I am trying to add an overlay to my google map, which is fixed in the center of the screen so that users can use it to accurately select a location.

Currently, the code below draws a map with a centered image of a crosshair, but since the button is below the map, the center of the map changes and the crosshair no longer represents the center point of the map that was moved up the buttonโ€™s height.

I looked at Android - showing a crosshair in the center of my Google Map , but it seems to be suitable only for v1 :(

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/locationpicker_map" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/locationpicker_add_venue" android:clickable="true" android:layout_weight="1" class="com.google.android.gms.maps.SupportMapFragment"/> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/crosshairs" android:layout_gravity="center_vertical" android:layout_centerInParent="true"/> <Button android:id="@+id/locationpicker_add_venue" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text = "Add new venue here" android:layout_alignParentBottom="true"/> </RelativeLayout> 
+4
source share
1 answer

To achieve this effect, I used a relative layout wrapped around a map fragment, centered in the center of the image. An example is given below:

Crosshair centered on android google map

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/locationpicker_map" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:layout_weight="1" class="com.google.android.gms.maps.SupportMapFragment"/> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/crosshairs" android:layout_gravity="center_vertical" android:layout_centerInParent="true"/> <Button android:id="@+id/locationpicker_add_venue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text = "Add new venue here" style="@style/Buttonpurple_style" android:layout_gravity="center" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" /> </RelativeLayout> 
+4
source

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


All Articles