Android Google Map API cumstom标记将Google地图数据带到自定义标记上

时间:2019-01-10 11:22:59

标签: android google-maps google-maps-markers google-maps-styled point-of-interest

我正在尝试构建一个需要使用自定义标记在地图上显示某些地方的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. 无法点击Google Maps的兴趣点。他们的图标在地图上可见,但无法点击。

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

有没有实现这些目标的方法?

1 个答案:

答案 0 :(得分:2)

对于第1页:

首先,在Google地图中隐藏所有“默认”兴趣点,例如在thisJozef答案中:

  

您只需修改地图样式即可完成操作:Adding a Styled Map

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

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

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

使用Place Search中的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>

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

NB!附近的网址请求仅返回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}(Place Search中的信息,并由Vadim Eksler在Google Maps Android自定义信息窗口中的评论Place Details中建议,例如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;
    }
}