Search code examples
javaandroidinfowindow

Can't call my custom info window when clicking a marker


I'm setting a new custom info window. I have a bunch of markers from an API and I'm trying to put their data in TextViews, but so far I've failed.

I tried putting the ArrayList data into an array and calling it. Tried following almost every single video or article on the internet and failed!

<?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) {

             }
         });
    }
}

Every marker has its own data. I expect whenever a marker is clicked a window will appear with the data, just like the XML layout.


Solution

  • in marker use setTag(obj), where object same object you used in your adapter