【问题标题】:Android-Google Maps V2-Adding custom infowindows to markers created from an arrayAndroid-Google Maps V2-向从数组创建的标记添加自定义信息窗口
【发布时间】:2015-11-12 04:45:05
【问题描述】:

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

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

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

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;
    }
}

【问题讨论】:

标签: android google-maps google-maps-api-2


【解决方案1】:

你必须实现一个 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;
                    }
                }
            });

希望对你有所帮助。

【讨论】:

  • 谢谢,但是如何在单击标记时显示与每个标记相关的信息窗口?
  • 最后一段代码就是为了这个,你只需要设置一个标记点​​击监听器,监听器旁边是if (marker.equals(hashMarkers.get("the number of the marker")))来区分每个标记
猜你喜欢
  • 2014-11-13
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-15
  • 1970-01-01
相关资源
最近更新 更多