Android Google 地图 API 自定义标记带来 Google 自定义标记上的地图数据

Android Google Map API cumstom marker bring Google map data on custom marker

我正在尝试构建一个 Android 应用程序,该应用程序需要使用自定义标记在地图上显示某些地点。当单击标记时,它会显示片段,但是,对于我的应用程序,我需要显示有关该地点的 Google 地图信息,如 Google 地图应用程序所示。我的XML很简单:

<RelativeLayout 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"
    tools:context=".MainActivity" >

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

我有两个问题:

  1. 来自 Google 地图的兴趣点不可点击。他们的图标在地图上可见,但不可点击。

  2. 当我的自定义标记被点击时,我想显示 Google 地图的信息,而不是我的代码片段。

有没有办法实现这些?

对于第 1 页:

首先从 Google 地图中隐藏所有 "default" 个兴趣点,如 answer of Jozef:

You can do it simply by modification of the map style: Adding a Styled Map

  1. Create JSON file src\main\res\raw\map_style.json like this:

[
  {
    featureType: "poi",
    elementType: "labels",
    stylers: [
      {
        visibility: "off"
      }
    ]
  }
]
  1. Add map style to your GoogleMap

googleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(getContext(), R.raw.map_style));

比使用 Place Search from Google Places API 并通过附近的 URL 请求获取兴趣点列表:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=<LAT_LNG_e_g_ 32.944552,-97.129767>&types=point_of_interest&radius=<RADIUS_IN_METERS>&sensor=false&key=<YOUR_APP_KEY>

解析它并以编程方式在地图上显示所需位置作为您的可点击标记。

注意! Nearby URL request returns only 20 places, load more data you should use string value from next_page_token tag of response and pass it via pagetoken parameter for next request:

 https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=<LAT_LNG_e_g_ 32.944552,-97.129767>&types=point_of_interest&radius=<RADIUS_IN_METERS>&sensor=false&key=<YOUR_APP_KEY>&pagetoken=<TOKEN_FOR_NEXT_PAGE_FROM_next_page_token_TAG_OF_RESPONSE>

并且不要忘记从 Console.

为您的应用程序启用 Places API

对于第 2 页:

使用第 1 页中的信息(Place Search) and suggested by Vadim Eksler in his comment Place Details in Google Maps Android Custom Info Window like in this 示例:

public class CustomInfoWindowGoogleMap implements GoogleMap.InfoWindowAdapter {

    private Context context;

    public CustomInfoWindowGoogleMap(Context ctx){
        context = ctx;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        View view = ((Activity)context).getLayoutInflater()
.inflate(R.layout.map_custom_infowindow, null);

        TextView name_tv = view.findViewById(R.id.name);
        TextView details_tv = view.findViewById(R.id.details);
        ImageView img = view.findViewById(R.id.pic);

        TextView hotel_tv = view.findViewById(R.id.hotels);
        TextView food_tv = view.findViewById(R.id.food);
        TextView transport_tv = view.findViewById(R.id.transport);

        name_tv.setText(marker.getTitle());
        details_tv.setText(marker.getSnippet());

        InfoWindowData infoWindowData = (InfoWindowData) marker.getTag();

        int imageId = context.getResources().getIdentifier(infoWindowData.getImage().toLowerCase(),
                "drawable", context.getPackageName());
        img.setImageResource(imageId);

        hotel_tv.setText(infoWindowData.getHotel());
        food_tv.setText(infoWindowData.getFood());
        transport_tv.setText(infoWindowData.getTransport());

        return view;
    }
}