对于第 1 页:
首先从谷歌地图中隐藏所有“默认”兴趣点,就像在Jozef的这个答案中一样:
您可以简单地通过修改地图样式来做到这一点:添加样式地图
- 像这样创建 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 页:
使用第 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;
}
}