【问题标题】:Android Google Maps Overlay image from URL来自 URL 的 Android 谷歌地图覆盖图像
【发布时间】:2015-06-01 02:59:48
【问题描述】:

最近我尝试在 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);

我面临的问题是:我可以从 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);

【问题讨论】:

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


    【解决方案1】:

    好像是从互联网获取BitmapDescriptor对象,所以你可以尝试使用Picasso库加载互联网图像。

    示例代码:

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

    【讨论】:

    • 感谢bjiang回复!我还有一个问题,如果我想让这个 AsyncTask 返回 GroundOverlay 对象,怎么做?因为,我会在一些事件发生后移除这个覆盖,谢谢!
    • 我想你可以只为GroundOverlay对象创建一个成员变量,然后将它保存在onPostExecutegoogleMap.addGroundOverlay(groundOverlay);之前查看我在代码中的更新。
    • 如何叠加一组图像?我应该在哪里添加 for 循环?
    • 再次感谢bjiang!我尝试成功添加 for 循环,但我现在面临的问题是我的 .gif 在谷歌地图上似乎不透明......有点奇怪
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多