我们如何使用Volley设置来自服务器的动态数据的自定义信息窗口

时间:2019-05-06 18:36:46

标签: android json google-maps android-volley google-maps-markers

我的服务器上有json数据,我使用排球库在Android项目中成功获取了数据,然后基于该数据我在google maps上显示了标记。但是问题是每当我单击某个标记时没有填充自定义信息窗口的内容,它总是在信息窗口上显示相同的数据。为此,我正在使用自定义信息窗口适配器。下面是我的主要活动,具有所有内容。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (checkGooglePlayServicesAvailability()){
            Toast.makeText(this,"Connected to google play services",Toast.LENGTH_LONG).show();
            setContentView(R.layout.activity_maps);
            initMap();
        }

    }@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.hideInfoWindow();
                    marker.showInfoWindow();
                    return false;
                }
            });
            mGoogleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(Marker marker) {
                    return null;
                }

                @Override
                public View getInfoContents(Marker marker) {

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

                    TextView name= (TextView) view.findViewById(R.id.name);
                    TextView time= (TextView) view.findViewById(R.id.time);
                    TextView servic= (TextView) view.findViewById(R.id.servic);
                    TextView phone= (TextView) view.findViewById(R.id.contct);
                    Log.e("saloon","Working hours "+workingHours);
                    Log.e("saloon","desc "+desc);
                    Log.e("saloon","pgone "+cntct);
                    LatLng ll=marker.getPosition();
                    name.setText("Name : "+marker.getTitle());
                    InfoWindoData infoWindoData=(InfoWindoData) marker.getTag();
                    time.setText("Timings : "+workingHours);
                    servic.setText("Services  : "+desc);
                    phone.setText("Phone : "+cntct);
                    return view;
                }
            });
        }
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        googleMap.getUiSettings().setZoomControlsEnabled(true);
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        {
            Log.e("JSONResult","Permission not granted");
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
            return;

        }
        else {

            showMarkers();
        }

    }

    public void showMarkers(){
        Log.e("JSONResult","Permission granted");
        requestQueue= Volley.newRequestQueue(getApplicationContext());

        StringRequest stringRequest=new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("JSONResult" , response.toString());
                JSONObject j = null;
                try{
                    j =new JSONObject(response);
                    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");
                        workingHours=jsonObject1.getString("8");
                        Log.e("saloon","Working hours "+workingHours);
                        desc=jsonObject1.getString("9");
                        Log.e("saloon","desc "+desc);
                        cntct=jsonObject1.getString("4");
                        Log.e("saloon","pgone "+cntct);
                        if (ifLocationGiven==""){
                            marker= mGoogleMap.addMarker(new MarkerOptions()
                                    .position(new LatLng(Double.parseDouble(lat_i) , Double.parseDouble(long_i)))
                                    .title(saloonName).position(new LatLng(lat,lng))
                                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE))
                            );

                            mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(31.5151,74.3439), 6.0f));
                        }

                    }

                }catch (NullPointerException e){
                    e.printStackTrace();

                }

                catch (JSONException e){
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                Toast.makeText(Maps.this, error.getMessage(), Toast.LENGTH_LONG).show();
            }
        });


        int socketTimeout = 10000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);
    }

0 个答案:

没有答案