来自网址的Android Google地图覆盖图片

时间:2015-03-27 17:18:57

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

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

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

我面临的问题是:我可以覆盖网址中的图片吗?像:

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

1 个答案:

答案 0 :(得分:5)

似乎从BitmapDescriptor对象形成了Internet,因此您可以尝试使用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();
        }
    }
}