How do I put an icon in the center of the screen in Google Map on Android?

Asked 1 years ago, Updated 1 years ago, 113 views

We are currently producing a mobile community application.

I'd like to mark the center on the map.

I don't want to mark it with a marker, but I want to classify it separately like an image icon.

I want to create an image that points only to the center center point even if I drag the map like the screen in the picture.

What should I do?

I think it is impossible to put imageView in Google map because it is a fragment.

Help me!

googlemap android

2022-09-22 21:27

3 Answers

You can add fragments through XML. For more information, see the Add a Fragment to an Activity using XML at the link below.

The above method can be used to ensure that the marker is always located in the center of the screen. If you make it into a code, it would be the following way.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ImageView
        android:id="@+id/marker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/marker"/>
</RelativeLayout>

Activity Code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.debug_map);

    SupportMapFragment mapFragment =
        (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


2022-09-22 21:27

The part that centers the marker is the android:layout_centerInParent="true" property in ImageView in the XML file. This will overlay markers on the map, allowing you to place markers in the middle of the screen regardless of the map's movement.

In fact, if you look at the UI layout boundaries on the following map, you can see that the markers are displayed overlayively on the map. If you look around the marker, you can see the red border. This layout boundary (red border) overlays a separate layout, not a marker drawn on the map.It's something that can be inferred.

If you look at a typical marker, you can see that it doesn't have a border, as shown in the figure below, and these markers mean markers drawn directly on the map.


2022-09-22 21:27

You can customize the marker. For more information, please use Google Search.

If you customize the marker itself, you can change it to the shape you want, not the basic marker shape.


2022-09-22 21:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.