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

时间:2019-06-01 12:18:59

标签: java android infowindow

我正在设置一个新的“自定义信息”窗口。我有很多来自API的标记,我正在尝试将其数据放入TextViews,但到目前为止,我失败了。

我试图将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布局

1 个答案:

答案 0 :(得分:0)

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