Android Google Map API 自定义标记在自定义标记上带来谷歌地图数据

Posted

技术标签:

【中文标题】Android Google Map API 自定义标记在自定义标记上带来谷歌地图数据【英文标题】:Android Google Map API cumstom marker bring Google map data on custom marker 【发布时间】:2019-06-05 06:16:27 【问题描述】:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    tools:context=".MainActivity" >

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

我有两个问题:

    Google 地图中的兴趣点不可点击。它们的图标在地图上可见,但不可点击。

    点击我的自定义标记时,我想显示 Google 地图的信息,而不是我的 sn-p。

有没有办法实现这些?

【问题讨论】:

这个api是你需要获取地点数据的吗? developers.google.com/places/android-sdk/place-details @VadimEksler 这可能会有所帮助,但不能只带谷歌地图信息窗口吗?这样,用户就可以看到 cmets 等了。 我认为这不可能 【参考方案1】:

对于第 1 页:

首先从谷歌地图中隐藏所有“默认”兴趣点,例如thisJozef 的答案:

只需修改地图样式即可:Adding a Styled Map

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

[
  
    featureType: "poi",
    elementType: "labels",
    stylers: [
      
        visibility: "off"
      
    ]
  
]
    将地图样式添加到您的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 页:

使用 p.1 (Place Search) 中的信息和 Vadim Eksler 在他的评论中建议的信息 Place Details 在 Google Maps Android 自定义信息窗口中,例如 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;
    

【讨论】:

P1 是一个非常好的解决方法,但在我的情况下它可能会出现问题,有些地方是非常不同的地方。我们决定使用我们的自定义标记。 P2,既然这是唯一的方法,我们就在上面。感谢您的完整回答。 @kelalaka 不客气! P1 如您所愿。地点搜索可以返回非常不同的地点,您可以对其进行过滤。

以上是关于Android Google Map API 自定义标记在自定义标记上带来谷歌地图数据的主要内容,如果未能解决你的问题,请参考以下文章