如何将图像放入GoogleMaps api片段中

时间:2017-06-12 13:35:31

标签: java android image google-maps-markers google-maps-android-api-2

我想在里面插入一个带有图片的片段,只要有人点击标记,他们就会在标记描述(片段)中看到位图图片。

  mSydney = mMap.addMarker(new MarkerOptions()
            .position(SYDNEY)
            .title("test")
            .snippet(IMAGE HERE)
            .icon();

我有一个带图片的网址,我该怎么做?

2 个答案:

答案 0 :(得分:1)

我使用Picasso进行图片加载

这是用于扩充自定义信息窗口的java代码

googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
        @Override
        public View getInfoWindow(final Marker marker) {
            v[0] = getActivity().getLayoutInflater().inflate(R.layout.map_info_design, null);
            if (marker != null) {

                final ImageView logo=(ImageView)v[0].findViewById(R.id.logo);




                t1=new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        logo.setImageBitmap(bitmap);

                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {

                    }
                };
                Picasso.with(getActivity())
                        .load(b1.get(Integer.parseInt(marker.getSnippet()))).error(R.drawable.logo_icon)
                        .into(logo,new MarkerCallback(marker));

            }
            return (v[0]);
        }

        @Override
        public View getInfoContents(Marker marker) {


            return null;
        }
    });

布局XML文件(R.layout.custom_marker_maps)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:background="@drawable/comment_icon"
android:padding="10dp"
android:layout_height="wrap_content">

<ImageView
    android:layout_width="60dp"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:layout_centerInParent="true"
    android:layout_height="60dp"
    android:id="@+id/logo"/>

</RelativeLayout>

添加此代码以进行回调(以解决Picasso错误)

static class MarkerCallback implements Callback {
    Marker marker=null;

    MarkerCallback(Marker marker) {
        this.marker=marker;
    }

    @Override
    public void onError() {
        Log.e(getClass().getSimpleName(), "Error loading thumbnail!");
    }

    @Override
    public void onSuccess() {
        if (marker != null && marker.isInfoWindowShown()) {
            marker.hideInfoWindow();
            marker.showInfoWindow();
        }
    }
}

<强>输出

enter image description here

答案 1 :(得分:0)

fromAsset(String assetName) – Loading from assets folder
fromBitmap (Bitmap image) – Loading bitmap image
fromFile (String path) – Loading from file
fromResource (int resourceId) – Loading from drawable resource

// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");

// Changing marker icon
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker_icon)));

// adding marker
googleMap.addMarker(marker);