1

我正在尝试构建一个需要使用自定义标记在地图上显示某些地方的 Android 应用程序。但是,当标记单击它会显示片段时,对于我的应用程序,我需要显示有关该地点的 Google 地图信息,如 Google Maps 应用程序所示。我的 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. 谷歌地图的兴趣点不可点击。他们的图标在地图上可见,但不可点击。

  2. 当我的自定义标记点击我想显示谷歌地图的信息,而不是我的片段。

有没有办法实现这些?

4

1 回答 1

2

对于第 1 页:

首先从谷歌地图中隐藏所有“默认”兴趣点,就像在Jozef的这个答案中一样:

您可以简单地通过修改地图样式来做到这一点:添加样式地图

  1. 像这样创建 JSON 文件src\main\res\raw\map_style.json

[
  {
    featureType: "poi",
    elementType: "labels",
    stylers: [
      {
        visibility: "off"
      }
    ]
  }
]
  1. 将地图样式添加到您的GoogleMap

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

比使用Google Places API中的Place Search并通过附近的 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>

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

注意!附近的 URL 请求仅返回 20 个位置,要加载更多数据,您应该使用next_page_token响应标记中的字符串值并通过pagetoken参数将其传递给下一个请求:

 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 页(地点搜索)中的信息,并由Vadim Eksler在他的评论Place Details in Google Maps Android Custom Info Window 中建议,如下例所示:

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;
    }
}
于 2019-01-10T19:08:44.423 回答