首页 > 解决方案 > How do I retrieve the JSON response for each click on Google Map Marker?

问题描述

I was able to make multiple marks on the google map on android by getting data from JSON. So NOW each of those markers has their own corresponding bukken_name, latitute and longitude.

Each time I click a specific marker on the map. I want it to display in console their retrieved JSON data.

In my current code, onMarkerClick gets the values and then displays it in System.out.println. But whenever I do so click the marker it only keeps on display the exact same value of the last JSON data.

 StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try{
                    JSONObject jsonObject = new JSONObject(response);
                    JSONObject data = jsonObject.getJSONObject("data");
                    JSONArray bukken_list = data.getJSONArray("bukken_list");

                    for (int i = 0; i < bukken_list.length(); i++) {
                        JSONObject returnBukken =  bukken_list.getJSONObject(i);

                        String latitude = returnBukken.getString("ido_hokui"); //latitude
                        String longitude = returnBukken.getString("keido_tokei"); //longitude
                        final String bukken_name = returnBukken.getString("bukken_name"); //longitude

                        final Double x = Double.parseDouble(latitude);
                        final Double y = Double.parseDouble(longitude);

                        System.out.println("COORDINATES: " + i);
                        System.out.println("LATITUDE: " + latitude);
                        System.out.println("LONGITUDE: " + longitude);
                        System.out.println("BUKKEN NAME: " + longitude);

                        LatLng japan = new LatLng(x, y);
                        MarkerOptions client_marker = new MarkerOptions().position(new LatLng(x, y));
                        client_marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.mapicon_rent));
                        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(japan, 10f));
                        mMap.addMarker(client_marker);
                        mMap.moveCamera(CameraUpdateFactory.newLatLng(japan));
                        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

                            @Override
                            public boolean onMarkerClick(Marker arg0) {

                                //FAULT: does not get value of each marker
                              System.out.println("MARKER " + x + " " + y + " " + bukken_name);

                                return true;
                            }
                        });

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                int x = 1;
                // Anything you want
            }
        });

标签: androidgoogle-maps

解决方案


Set bukken_name as marker's tag:

Marker marker = mMap.addMarker(client_marker);
marker.setTag(bukken_name);

On marker click:

public boolean onMarkerClick(Marker clickedMarker) {
    LatLng location = clickedMarker.getPosition();
    System.out.println("MARKER " + location.latitude + " " + location.longitude + " " + clickedMarker.getTag());
    return true;
}

推荐阅读