Android谷歌地图API cumstom marker将谷歌地图数据带到自定义标记上

Android谷歌地图API cumstom marker将谷歌地图数据带到自定义标记上,android,google-maps,google-maps-markers,google-maps-styled,point-of-interest,Android,Google Maps,Google Maps Markers,Google Maps Styled,Point Of Interest,我正在尝试构建一个Android应用程序,它需要用自定义标记显示地图上的一些地方。当点击标记时,它会显示代码片段,然而,对于我的应用程序,我需要显示谷歌地图关于这个地方的信息,就像谷歌地图应用程序显示的那样。我的XML很简单: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" and

我正在尝试构建一个Android应用程序,它需要用自定义标记显示地图上的一些地方。当点击标记时,它会显示代码片段,然而,对于我的应用程序,我需要显示谷歌地图关于这个地方的信息,就像谷歌地图应用程序显示的那样。我的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页:

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

    只需修改贴图样式即可完成此操作:

  • 创建JSON文件src\main\res\raw\map\u style.JSON如下所示:
  • 将地图样式添加到您的
    GoogleMap
  • 而不是通过附近的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>
    
    并且不要忘记从为您的应用程序启用PlacesAPI

    第2页:

    使用p.1()中的信息,并在Google Maps Android自定义信息窗口的评论中建议,如示例所示:


    您需要这个api来获取有关place的数据吗@瓦迪梅克斯勒:这可能会有所帮助,但难道不可能只在谷歌地图信息窗口中使用吗?因此,用户可以看到评论等。我认为即使是可能的EP1也不是一个很好的解决方法,但在我的情况下,它可能会有问题,有些地方是非常独特的。我们决定使用我们的定制标记。P2,因为这是唯一的方法,我们在上面。谢谢你的完整回答。@kelalaka不客气!如你所愿。地点搜索可以返回非常独特的地点,您可以对其进行筛选。
    googleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(getContext(), R.raw.map_style));
    
    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>
    
     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>
    
    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;
        }
    }