Android Google 来自 URL 的地图覆盖图像

Android Google Maps Overlay image from URL

最近我尝试在 Google Maps v2 中为 Android 使用 Overlay,从一些教程我们知道

BitmapDescriptor image = BitmapDescriptorFactory.fromResource(R.drawable.android);
GroundOverlayOptions groundOverlay = new GroundOverlayOptions()
                .image(image)
                .position(point1, 500f)
                .transparency(0.5f);
googleMap.addGroundOverlay(groundOverlay);

我面临的问题是:我可以叠加来自 URL 的图像吗?喜欢:

BitmapDescriptor image = BitmapDescriptorFactory.fromResource(R.drawable.android);
            GroundOverlayOptions groundOverlay = new GroundOverlayOptions()
            .image(***"http://image path...."***)
            .position(point1, 500f)
            .transparency(0.5f);
            googleMap.addGroundOverlay(groundOverlay);

它似乎从 Internet 获取 BitmapDescriptor 对象,因此您可以尝试使用 Picasso 库加载 Internet 图像。

示例代码:

    GroundOverlayOptions mGroundOverlayOptions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        new AddGroundOverlay().execute(MY_URL);
        ...
    }


public class AddGroundOverlay extends AsyncTask<String, Integer, BitmapDescriptor> {

    BitmapDescriptor bitmapDescriptor;

    @Override
    protected BitmapDescriptor doInBackground(String... url) {
        myUrl = url[0];
        try {
            bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(Picasso.with(getActivity()).load(myUrl).get());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmapDescriptor;
    }

    protected void onPostExecute(BitmapDescriptor icon) {

        try {

            GroundOverlayOptions groundOverlay = new GroundOverlayOptions()
                    .image(bitmapDescriptor)
                    .position(point1, 500f)
                    .transparency(0.5f);
            // Updated
            mGroundOverlayOptions = groundOverlay;
            googleMap.addGroundOverlay(groundOverlay);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}