第一次单击标记时,getInfoContents返回null

时间:2019-05-08 10:43:47

标签: android json google-maps android-volley

我正在尝试单击谷歌地图上显示的标记,并从服务器获取有关该标记的所有数据。问题是第一次单击标记返回null,而第二次单击另一个标记则显示第一个数据标记。出于某种原因,我的变量首先被设置,然后凌空请求队列运行。请看一下我的代码

@Override
public void onMapReady(GoogleMap googleMap) {
    mGoogleMap=googleMap;
    if (mGoogleMap!=null){
        mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Log.e("marker","marker clicked"+marker.getPosition());
                marker.showInfoWindow();
                return false;
            }
        });
        mGoogleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                return null;
            }

            @Override
            public View getInfoContents(final Marker marker) {

                View view= getLayoutInflater().inflate(R.layout.window,null);

                final TextView name= (TextView) view.findViewById(R.id.name);
                final TextView time= (TextView) view.findViewById(R.id.time);
                final TextView servic= (TextView) view.findViewById(R.id.servic);
                final TextView phone= (TextView) view.findViewById(R.id.contct);
                LatLng latLng=marker.getPosition();
                double lat=latLng.latitude;
                double lng=latLng.longitude;
                strlat=Double.toString(lat);
                strLng=Double.toString(lng);
                RequestQueue requestQueue=Volley.newRequestQueue(getApplicationContext());
                StringRequest stringRequest=new StringRequest(Request.Method.POST, CLICK, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        JSONObject j = null;
                        try{
                            j =new JSONObject(response);
                            Log.e("workingg","i am inside request");
                            result = j.getJSONArray("FL");
                            for(int i=0;i<result.length();i++){
                                JSONObject jsonObject1=result.getJSONObject(i);
                                Double lat=jsonObject1.getDouble("10");
                                Double lng=jsonObject1.getDouble("11");
                                String lat_i = jsonObject1.getString("10");
                                String long_i = jsonObject1.getString("11");
                                String saloonName=jsonObject1.getString("1");
                                workingHoursclicked=jsonObject1.getString("8");
                                Log.e("workingg","Working hours "+workingHoursclicked);
                                descClicked=jsonObject1.getString("9");
                                cntctClicked=jsonObject1.getString("4");

                            }
                        }catch (NullPointerException e){
                            e.printStackTrace();
                        }
                        catch (JSONException e){
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    Toast.makeText(Maps.this,""+error,Toast.LENGTH_LONG).show();
                    }
                }){
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String,String>map=new HashMap<>();
                        map.put("s_latitude",strlat);
                        map.put("s_longitude",strLng);
                        return map;
                    }
                };
                requestQueue.add(stringRequest);

                name.setText("Name : "+marker.getTitle());
                time.setText("Timings : "+workingHoursclicked);
                servic.setText("Services  : "+descClicked);
                phone.setText("Phone : "+cntctClicked);
                Log.e("workingg","Working hours are "+workingHoursclicked);
                return view;
            }
        });
    }

0 个答案:

没有答案