使用集群管理器时为标记创建自定义信息窗口?

时间:2019-05-10 11:52:57

标签: java android google-maps

我一直遵循这个guide,直到我意识到他正在将标记直接添加到地图上并且没有让集群管理器来处理它。

我创建了一个实现CustomInfo的{​​{1}}类。在InfoWindowAdapter中,我使用getInfoContents(Marker marker)从标记中获取数据。问题是我从来没有在标记上设置标签(我在哪里?)。

这是当我读取位置对象时的代码:

getTag()

要将标记添加到地图,我对群集管理器使用以下方法:

private void generateShelterObjects()
{

    Log.i(TAG, "METHOD generateShelterObjects() STARTED");

    InputStream is = getResources().openRawResource(R.raw.shelters_csv_file);
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(is, Charset.forName("UTF-8")));
    String line = "";

    try {
        while ((line = reader.readLine()) != null) {
            // Split the line into different tokens (using the comma as a separator).
            String[] tokens = line.split(",");

            String address = tokens[0];
            double latitude = Double.parseDouble(tokens[1]);
            double longitude = Double.parseDouble(tokens[2]);
            LatLng latLng = new LatLng(latitude, longitude);
            String numberOfOccupants = tokens[3];

            MarkerOptions markerOptions = new MarkerOptions().position(latLng).title(address).snippet("Antal platser: " + numberOfOccupants);

            ShelterObject obj = new ShelterObject(latLng, address, numberOfOccupants);

            listMarkers.add(markerOptions);

        }

    } catch (IOException e1) {
        Log.e(TAG, "Error" + line, e1);
        e1.printStackTrace();
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

检查此

private void setUpcustomInfoWindow(){
    myMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }
        @Override
        public View getInfoContents(Marker marker) {
            View myContentView =getActivity().getLayoutInflater().inflate(
                    R.layout.custom_info_window, null);
            TextView tvAddress = ((TextView) myContentView
                    .findViewById(R.id.tv_address_info_window));
            TextView tvAveragePrice = ((TextView) myContentView
                    .findViewById(R.id.tv_averageprice_info_window));
            TextView tvAverageSqft = ((TextView) myContentView
                    .findViewById(R.id.tv_averagesqft_info_window));
            MicroLocality microLocality = (MicroLocality) marker.getTag();
            tvAddress.setText(microLocality.getMicroLocality());
            tvAveragePrice.setText(getActivity().getString(R.string.Rs)+" "+microLocality.getPricerange());
            tvAverageSqft.setText(getActivity().getString(R.string.Rs)+" "+microLocality.getPricePerSqft());
            return myContentView;
        }
    });
}