gpt4 book ai didi

Android Google Map API 自定义标记将 Google map 数据带到自定义标记上

转载 作者:行者123 更新时间:2023-11-29 18:36:36 25 4
gpt4 key购买 nike

我正在尝试构建一个 Android 应用程序,该应用程序需要使用自定义标记在 map 上显示某些地点。单击标记时,它会显示代码段,但是,对于我的应用程序,我需要显示有关该地点的 Google map 信息,如 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 map 中的兴趣点不可点击。他们的图标在 map 上可见,但不可点击。

  2. 当我的自定义标记被点击时,我想显示 Google map 的信息,而不是我的 fragment 。

有没有办法实现这些?

最佳答案

第 1 页:

首先像this 一样从Google map 中隐藏所有“默认”兴趣点。 Jozef的答案:

You can do it simply by modification of the map style: Adding a Styled Map

  1. Create JSON file src\main\res\raw\map_style.json like this:

[
{
featureType: "poi",
elementType: "labels",
stylers: [
{
visibility: "off"
}
]
}
]
  1. Add map style to your GoogleMap

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>

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

注意!附近的 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 页 ( 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;
}
}

关于Android Google Map API 自定义标记将 Google map 数据带到自定义标记上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54127643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com