Android-Google Maps V2-将自定义信息窗口添加到从数组创建的标记

Android-Google Maps V2-Adding custom infowindows to markers created from an array

我想创建一个可以从数据库接收一组点并为地图上的每个点添加标记的应用程序。我目前正在使用对象数组进行测试。我在循环中使用相同的标记变量将标记放在地图上。

包含我的标记位置的数组还包含一些关于这些特定位置的其他数据,当标记被触摸时,我想在自定义信息窗口(从数组获取数据)中显示这些数据。

我不确定在调用此信息窗口时应该如何区分不同的标记。

OnMarkerClickListener document on developers.google.com

我添加标记的代码:

for (pointNumber= 0; pointNumber<pointArray.length; pointNumber++) {
    taxiLatitude = pointArray[pointNumber].position.latitude;
    taxiLongitude = pointArray[pointNumber].position.longitude;

    if (valueInArray<someValue) {
        pointMarker = mMap.addMarker(new MarkerOptions()
            .position(new LatLng(pointArray[pointNumber].position.latitude, carsArray[pointNumber].position.longitude))
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
            .title("Title: "+pointArray[pointNumber].pointTitle)
            .snippet("Snippet: "+pointArray[pointNumber].pointSnippet));
    } else {
        System.out.println("Out of range");
    }
}

我的代码将数据添加到我的自定义信息窗口,我得到了 here

class CustomWindowAdapter implements GoogleMap.InfoWindowAdapter {
    LayoutInflater mInflater;
    public CustomWindowAdapter(LayoutInflater i){
        mInflater = i;
    }

    @Override
    public View getInfoContents(Marker marker) {
        // Getting view from the layout file
        View v = mInflater.inflate(R.layout.custom_window_layout, null);
        // Populate fields
        ImageView image = (ImageView) v.findViewById(R.id.image);
        image.setImageResource(pointArray[pointNumber].image);

        TextView title = (TextView) v.findViewById(R.id.tv_1);
        title.setText(pointArray[pointNumber].text1);

        TextView description = (TextView) v.findViewById(R.id.text_2);
        description.setText(pointArray[pointNumber].text2);
        }
        // Return info window contents
        return v;      

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }
}

你必须实现一个 HashMap,realx,很简单,我会告诉你如何做到这一点来识别你所有的标记:

创建键类型为 String 且对象类型为 Marker 的 HashMap:

HashMap<String,Marker> hashMarkers = new HashMap<>();

将标记添加到由数字(转换为字符串)标识的哈希图中:

for (int x= 0; x < yourArray.length; x++) {
        hashMarkers.put(String.valueOf(x), mMap.addMarker(new MarkerOptions()
                .position( your array (or arrays) of positions(s) )
    }

然后您就可以识别您想要的每个标记:

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {
                    if (marker.equals(hashMarkers.get("the number of the marker"))) {
                        //here you can call your custom infoWindow or set the content 
                        return true;
                    } else {
                        return false;
                    }
                }
            });

希望对您有所帮助。