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



我有两个自动完成来寻找我完成的路线。现在我有两个地点的路线。我有起点和终点标记。在那个里,我设置了信息窗口来显示纬度,经度,地区,子地区,管理区域。我已经成功地展示了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;
        }
    }

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

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

最新更新