Google地图 - 使用可绘制资源图像为每个标记设置不同的图像

时间:2015-02-16 12:49:13

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

我正在寻找使用可绘制资源图像为每个标记设置不同的图像。

我拥有的就是这个。

public View getInfoContents(Marker marker) {
                        View v = getLayoutInflater().inflate(R.layout.windowlayout, null);
                        TextView tv1 = (TextView) v.findViewById(R.id.tv1);
                        TextView tv2 = (TextView) v.findViewById(R.id.tv2);
                        Button btn = (Button) findViewById(R.id.button);

                        LatLng pos = marker.getPosition();
                        tv1.setText(marker.getTitle());
                        tv2.setText(marker.getSnippet());   

                        ImageView image = ((ImageView) findViewById(R.id.image42));

                        return v;
                    }

这会设置标记的图像,但我希望它是每个标记的不同图像。

我正在考虑像这样的if / else语句..

public View getInfoContents(Marker marker) {
                        View v = getLayoutInflater().inflate(R.layout.windowlayout, null);
                        TextView tv1 = (TextView) v.findViewById(R.id.tv1);
                        TextView tv2 = (TextView) v.findViewById(R.id.tv2);
                        Button btn = (Button) findViewById(R.id.button);

                        LatLng pos = marker.getPosition();
                        tv1.setText(marker.getTitle());
                        tv2.setText(marker.getSnippet());   

                        ImageView image = ((ImageView) findViewById(R.id.image42));
                        if (marker == OldMellifontAbbey){
                            image= image.setBackground(BitmapDescriptorFactory.fromResource(R.drawable.oldmellifont));
                        }

                        else{


                        }
                        return v;
                    }
                });

欢迎任何建议。 感谢。

好的,所以我觉得我正在接受一些事情,但我仍然无法理解。 我编辑的代码如下。

    @Override
                    public View getInfoContents(Marker marker) {
                        View v = getLayoutInflater().inflate(R.layout.windowlayout, null);
                        TextView tv1 = (TextView) v.findViewById(R.id.tv1);
                        TextView tv2 = (TextView) v.findViewById(R.id.tv2);
                        Button btn = (Button) findViewById(R.id.button);

                        LatLng pos = marker.getPosition();
                        tv1.setText(marker.getTitle());
                        tv2.setText(marker.getSnippet());   

                        ImageView image = ((ImageView) findViewById(R.id.image42));
                        try{
                            Drawable drawable = getResources().getDrawable(R.drawable.oldmellifont);


                            image.setBackground(drawable);



                        }
                        catch(Exception e){
                            Toast.makeText(getApplicationContext(), "Hairy ball sack!!!", Toast.LENGTH_LONG).show();
                        }
                        return v;
                    }
                });

5 个答案:

答案 0 :(得分:1)

getInfoContents方法属于InfoWindowAdapter,所以我想通过说"每个标记的不同图像"你真的是指每个InfoWindow。如果是这种情况,那么我的建议是:

每次创建和添加标记时,都可以调用它的getId()方法,并将此唯一ID用作HashMap<String, Drawable>HashMap<String, Integer>中的关键字(稍后存储资源ID)。然后在InfoWindowAdapter中再次调用marker.getId()并将其映射到右侧drawable。

如果您尝试设置标记自己的图像,那么您应该使用BitmapDescriptorFactory方法之一,如下所示:

private Marker melbourne = mMap.addMarker(new MarkerOptions()
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));

答案 1 :(得分:0)

您必须使用BitmapDescriptor类才能将可绘制资源中的图像设置为标记。

试试这段代码:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize the HashMap for Markers and MyMarker object
    mMarkersHashMap = new HashMap<Marker, MyMarker>();

    mMyMarkersArray.add(new MyMarker("Brasil", "icon1", Double.parseDouble("-28.5971788"), Double.parseDouble("-52.7309824")));
    mMyMarkersArray.add(new MyMarker("United States", "icon2", Double.parseDouble("33.7266622"), Double.parseDouble("-87.1469829")));
    mMyMarkersArray.add(new MyMarker("Canada", "icon3", Double.parseDouble("51.8917773"), Double.parseDouble("-86.0922954")));
    mMyMarkersArray.add(new MyMarker("England", "icon4", Double.parseDouble("52.4435047"), Double.parseDouble("-3.4199249")));
    mMyMarkersArray.add(new MyMarker("España", "icon5", Double.parseDouble("41.8728262"), Double.parseDouble("-0.2375882")));
    mMyMarkersArray.add(new MyMarker("Portugal", "icon6", Double.parseDouble("40.8316649"), Double.parseDouble("-4.936009")));
    mMyMarkersArray.add(new MyMarker("Deutschland", "icon7", Double.parseDouble("51.1642292"), Double.parseDouble("10.4541194")));
    mMyMarkersArray.add(new MyMarker("Atlantic Ocean", "icondefault", Double.parseDouble("-13.1294607"), Double.parseDouble("-19.9602353")));

    setUpMap();

    plotMarkers(mMyMarkersArray);
}

private void plotMarkers(ArrayList<MyMarker> markers)
{
    if(markers.size() > 0)
    {
        for (MyMarker myMarker : markers)
        {

            // Create user marker with custom icon and other options
            MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
            markerOption.icon(BitmapDescriptorFactory.fromResource(R.drawable.currentlocation_icon));

            Marker currentMarker = mMap.addMarker(markerOption);
            mMarkersHashMap.put(currentMarker, myMarker);

            mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
        }
    }
}

private int manageMarkerIcon(String markerIcon)
{
    if (markerIcon.equals("icon1"))
        return R.drawable.icon1;
    else if(markerIcon.equals("icon2"))
        return R.drawable.icon2;
    else if(markerIcon.equals("icon3"))
        return R.drawable.icon3;
    else if(markerIcon.equals("icon4"))
        return R.drawable.icon4;
    else if(markerIcon.equals("icon5"))
        return R.drawable.icon5;
    else if(markerIcon.equals("icon6"))
        return R.drawable.icon6;
    else if(markerIcon.equals("icon7"))
        return R.drawable.icon7;
    else
        return R.drawable.icondefault;
}

private void setUpMap()
{
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null)
    {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        // Check if we were successful in obtaining the map.

        if (mMap != null)
        {
            mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
            {
                @Override
                public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker)
                {
                    marker.showInfoWindow();
                    return true;
                }
            });
        }
        else
            Toast.makeText(getApplicationContext(), "Unable to create Maps", Toast.LENGTH_SHORT).show();
    }
}

public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter
{
    public MarkerInfoWindowAdapter()
    {
    }

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

    @Override
    public View getInfoContents(Marker marker)
    {
        View v  = getLayoutInflater().inflate(R.layout.infowindow_layout, null);

        MyMarker myMarker = mMarkersHashMap.get(marker);

        ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);

        TextView markerLabel = (TextView)v.findViewById(R.id.marker_label);

        markerIcon.setImageResource(manageMarkerIcon(myMarker.getmIcon()));

        markerLabel.setText(myMarker.getmLabel());

        return v;
    }
}

}

答案 2 :(得分:0)

getInfoContents方法中,从标记处获取Icon

public View getInfoContents(Marker marker) {

     ... ...

     ImageView image = ((ImageView) findViewById(R.id.image42));
     image.setImageDrawable(marker.getIcon()); 

     return v;
}

对于每个Marker,您应该为其设置特定图标。然后当你打电话:

marker.showInfoWindow();

每个标记应显示自己的图像/图标。

答案 3 :(得分:0)

尝试此设置图像资源

image.setBackgroundResource(drawable);

答案 4 :(得分:0)

看起来您正在尝试在infowindow中添加更改图像而不是更改标记图标。为此,您可以做的是在添加标记时创建标记类型引用,假设将此引用创建为类的字段,以便所有内部函数都可以访问它,为该标记创建相应的Drawable引用

marker1 = myMap.addMarker(new MarkerOptions().position(yourLatLng1).icon(BitmapDescriptorFactory.fromResource(R.drawable.yourmarkericon)));
drawable1 = getResources().getDrawable(R.drawable.drawable1);

marker2 = myMap.addMarker(new MarkerOptions().position(yourLatLng2).icon(BitmapDescriptorFactory.fromResource(R.drawable.yourmarkericon)));
drawable2 = getResources().getDrawable(R.drawable.drawable2);
//and so on

确保所有必需的方法都可以访问marker1和drawable1,所以让它们成为类的领域,并且getInfoContents方法就是这样做的

Override
                public View getInfoContents(Marker marker) {
                    View v = getLayoutInflater().inflate(R.layout.windowlayout, null);
                    TextView tv1 = (TextView) v.findViewById(R.id.tv1);
                    TextView tv2 = (TextView) v.findViewById(R.id.tv2);
                    Button btn = (Button) findViewById(R.id.button);

                    LatLng pos = marker.getPosition();
                    tv1.setText(marker.getTitle());
                    tv2.setText(marker.getSnippet());   

                    ImageView image = ((ImageView) findViewById(R.id.image42));
                    if(marker.equals(marker1){
                        image= image.setBackground(drawable1);
                    } else if((marker.equals(marker2)){
                        image= image.setBackground(drawable2);
                    } //and so on
                    return v;
                }
            });