java - 单击标记时无法调用我的自定义信息窗口

标签 java android infowindow

我正在设置一个新的自定义信息窗口。我有一堆来自 API 的标记,我正在尝试将它们的数据放入 TextView 中,但到目前为止我失败了。

我尝试将 ArrayList 数据放入数组中并调用它。尝试关注互联网上的几乎所有视频或文章,但都失败了!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:id="@+id/tv_stfOrStrName"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:id="@+id/tv_totPriceAll"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:id="@+id/tv_lastOrdTime"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:id="@+id/tv_ordCountView"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:id="@+id/tv_firstOrDatsSinceLast"/>

</LinearLayout>
public class InfoObjectData {

    private String count;
    private String firstOrLastDay;
    private String lastTime;

    public InfoObjectData() {

    }

    public void setCount(String count) {
        this.count = count;
    }

    public void setFirstOrLastDay(String firstOrLastDay) {
        this.firstOrLastDay = firstOrLastDay;
    }

    public void setLastTime(String lastTime) {
        this.lastTime = lastTime;
    }

    public String getCount() {
        return count;
    }

    public String getFirstOrLastDay() {
        return firstOrLastDay;
    }

    public String getLastTime() {
        return lastTime;
    }
}
public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    private Context context;
    InfoObjectData infoObjectData = new InfoObjectData();
    public CustomInfoWindowAdapter(Context ctx) {
        this.context = ctx;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        View view = ((Activity)context).getLayoutInflater()
                .inflate(R.layout.custom_info_window, null);

        TextView name = view.findViewById(R.id.tv_stfOrStrName);
        TextView price= view.findViewById(R.id.tv_totPriceAll);
        TextView count = view.findViewById(R.id.tv_ordCountView);
        TextView lastTime= view.findViewById(R.id.tv_lastOrdTime);
        TextView firstOrLastDay= view.findViewById(R.id.tv_firstOrDatsSinceLast);

        name.setText(marker.getTitle());
        price.setText(marker.getSnippet());

        infoObjectData=(InfoObjectData) marker.getTag();

        return view;
    }
}
public class Mapper extends Fragment implements OnMapReadyCallback, GoogleMap.OnInfoWindowClickListener {

       GoogleMap map;

    public Mapper() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_mapper, container, false);

        return v;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        SupportMapFragment mapFragment= (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map1);
        mapFragment.getMapAsync(this);
        getCords();
    }

    public interface ccService
    {
        @FormUrlEncoded
        @POST("cc.php")
        Call <Orders> getCustomersOrdsSummary(@FieldMap Map<String,String> fields);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

        map= googleMap;
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        map.getUiSettings().setZoomControlsEnabled(true);
        map.getUiSettings().setMyLocationButtonEnabled(true);
        LatLngBounds.Builder builder = new LatLngBounds.Builder();

        LatLng local = new LatLng(30.0503705,31.3393294);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(local,11));
    }

    @Override
    public void onInfoWindowClick(Marker marker) {

    }

    private void getCords()   {
        String base_url = "http://api2.tajjer.com/api/";

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logging);

        Retrofit retrofit= new Retrofit.Builder()
                .baseUrl(base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient.build())
                .build();

        Map<String, String> params= new HashMap<>();
        params.put("accessCode","faf3sfaf_Tajjer_accesscode_ads9wkk");
        params.put("API_HoldingAccount_Secret","Tajjer_f3a8f0w89sfdkfa3k3f3");
        params.put("holdAcc","3");
        params.put("taccId","-1");
        params.put("method","getCustomersOrdsSummary");
       // Toast.makeText(getActivity(), "Here111", Toast.LENGTH_SHORT).show();

        ccService cc= retrofit.create(ccService.class);
         cc.getCustomersOrdsSummary(params).enqueue(new Callback<Orders>() {
             @Override
             public void onResponse(Call<Orders> call, Response<Orders> response) {
                 Orders orders=response.body();

                ArrayList<OrderData> orderData=new ArrayList<>();
                orderData.addAll(orders.getParameter().getOrderData());

                 // Toast.makeText(getActivity(), "Here", Toast.LENGTH_SHORT).show();
                 for(int i =0; i<orderData.size();i++) {
                    if (!orderData.get(i).getCustLat().equals("0") && !orderData.get(i).getCustLng().equals("0")&&!orderData.get(i).getCustLat().equals("undefined")&&!orderData.get(i).getCustLng().equals("undefined")){
                        Log.d("lang",orderData.get(i).getCustLng());
                       // Toast.makeText(getActivity(), "Here222", Toast.LENGTH_SHORT).show();

                        map.addMarker(new MarkerOptions()
                                         .position(new LatLng(Double.parseDouble(orderData.get(i).getCustLat()), Double.parseDouble(orderData.get(i).getCustLng()))
                                         ).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)).title(orderData.get(i).getTotal_price()));

                    }
                 }
             }

             @Override
             public void onFailure(Call<Orders> call, Throwable t) {

             }
         });
    }
}

每个标记都有自己的数据。我希望每当单击标记时都会出现一个包含数据的窗口,就像 XML 布局一样。

最佳答案

在标记中使用 setTag(obj),其中对象与您在适配器中使用的对象相同

关于java - 单击标记时无法调用我的自定义信息窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56406677/

相关文章:

java - Netty 服务器如何向不同的客户端发送数据?

java - 使用分隔符和值分割的字符串数组

android - 在 Android 中的 Bundle 中存储和检索数组列表

android - 自定义 View 的两种方式绑定(bind)

java - android套接字连接更新图形

marker - 如何在地理编码器放置标记后创建信息窗口

java - Goal Seek 在 groovy 中的实现

Javafx 菜单项白色边框

html - React-Google-Map 多个信息窗口打开

ios - 如何使用 Swift 3 自定义 googlemap infoWindow