在标记信息窗口中获取并显示JSON数据,提取当前位置的位置类型

时间:2016-07-19 11:09:05

标签: android json google-maps

我有两个自动完成找到我完成的路线。现在我有两个地点的路线。我有原点和目的地标记。在那里我设置信息窗口显示纬度,经度,地点,子地点,管理区域。我已成功显示lat和lan。现在我获取Json数据并显示在该信息窗口中。我已解析数据但不知道如何在信息窗口中显示它。发布一些解决方案..

    googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

        // Use default InfoWindow frame

        public View getInfoWindow(Marker arg0) {
            return null;
        }

        // Defines the contents of the InfoWindow

        public View getInfoContents(Marker arg0) {

            // Getting view from the layout file info_window_layout
            View v = getLayoutInflater().inflate(R.layout.info_window, null);

            // Getting the position from the marker
            LatLng latLng = arg0.getPosition();


            // Getting reference to the TextView to set latitude
            TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);

            // Getting reference to the TextView to set longitude
            TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
            TextView type=(TextView)v.findViewById(R.id.type);

            // Setting the latitude
            tvLat.setText("Latitude:" + latLng.latitude);

            // Setting the longitude
            tvLng.setText("Longitude:" + latLng.longitude);
            type.setText("locality"+"");


            // Returning the view containing InfoWindow contents
            return v;

        }
    });

 public void onDirectionFinderStart() {
    progressDialog = ProgressDialog.show(this, "Please wait.",
            "Finding direction..!", true);

    if (originMarkers != null) {
        for (Marker marker : originMarkers) {
            marker.remove();
            marker.showInfoWindow();

        }
    }

    if (destinationMarkers != null) {
        for (Marker marker : destinationMarkers) {
            marker.remove();
            marker.showInfoWindow();

        }
    }

    if (polylinePaths != null) {
        for (Polyline polyline : polylinePaths) {
            polyline.remove();
        }
    }
}


private class GetLocationTask extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                JSONArray addrComp = jsonObj.getJSONArray("results");
                for (int i = 0; i < addrComp.length(); i++){
                    JSONObject ad=new JSONObject();
                    JSONArray addr = ad.getJSONArray("address_components");
                    for (i = 0; i < addr.length(); i++){
                        JSONObject ty=new JSONObject();
                        JSONArray typ=ty.getJSONArray("types");
                        for (i = 0; i < typ.length(); i++){
                            JSONObject c = typ.getJSONObject(i);
                            String locality=c.getString("locality");
                            String political=c.getString("political");
                            String sublocality=c.getString("sublocality");
                            String adminArea=c.getString("administrative_area_level_1");



                            /*HashMap<String, String> local = new HashMap<>();
                            local.put("locality",locality);
                            local.put("administrative_area_level_1",adminArea);
                            local.put("sublocality",sublocality);
                            local.put("political",political);

                            list.add(local);*/
                        }
                    }

                }

            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
        }
    }

1 个答案:

答案 0 :(得分:1)

你可以使用 https://developers.google.com/maps/documentation/android-api/infowindows

或者 http://androidfreakers.blogspot.in/2013/08/display-custom-info-window-with.html 达到要求。 您需要为它创建自定义布局并从适配器设置信息。