简体   繁体   中英

Android: creating a Custom view as Marker in Google Maps API

Strangely enough, I haven't seen a proper answer anywhere else on here. It might not be possible, but I'm still taking a shot.

In the latest Google Maps API version for Android, I want to create a custom View that would be placed over the marker like so once you click on it :

Map Preview

在此输入图像描述

Of course, I want to be able to interact with its intern components, ie the listview. The menu would still show up and remain attached to the marker even if you move around on the map. From what I've seen you can only render a Bitmap on the map. Is there really no workaround ?

Thanks in advance

Use Info window to display custom layout on google maps.

This is the sample code from the document .

public class MarkerDemoActivity extends AppCompatActivity implements
        OnInfoWindowClickListener,
        OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    public void onMapReady(GoogleMap map) {
        mMap = map;
        // Add markers to the map and do other map setup.
        ...
        // Set a listener for info window events.
        mMap.setOnInfoWindowClickListener(this);
    }

    @Override
    public void onInfoWindowClick(Marker marker) {
        Toast.makeText(this, "Info window clicked",
                Toast.LENGTH_SHORT).show();
    }
}

However, Info window is not a live View, rather the view is rendered as an image onto the map. As a result, any listeners you set on the view are disregarded and you cannot distinguish between click events on various parts of the view. You are advised not to place interactive components — such as buttons, checkboxes, or text inputs — within your custom info window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM