对于第 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;
}
}