【问题标题】:Refresh marker on the google map to see image on the marker in Android刷新谷歌地图上的标记以在 Android 中查看标记上的图像
【发布时间】:2014-10-07 18:45:55
【问题描述】:

我是新手,我正在开发一个跟踪员工位置并将其显示在地图上的 Android 应用程序。它几乎完成了,只是它没有在标记上显示员工的图像。

我能够在网络上下载图像并将该图像设置在标记上。但问题是,它不会自动显示。我需要再次绘制坐标才能显示图像。这是我用来下载并在标记上设置图像的代码...

    private class DownloadEmployeeImage extends AsyncTask<String, Void, Bitmap> {

    ImageView image;

    public DownloadEmployeeImage(ImageView bmImage) {

        this.image = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {

        String url = urls[0];
        Bitmap bmpImg = null;

        try {

            InputStream in = new java.net.URL(url).openStream();
            bmpImg = BitmapFactory.decodeStream(in);

        } catch (Exception e) { 

            Log.e("Failed to download image: ", e.toString());
        }           

        return bmpImg;
    }

    protected void onPostExecute(Bitmap bmpImg) {

        try {

            image.setImageBitmap(RoundedImageView.getCroppedBitmap(bmpImg, 200));                               

        } catch(Exception e) {

            image.setImageBitmap(RoundedImageView.getCroppedBitmap(BitmapFactory
                    .decodeResource(MainActivity.this.getResources(), R.drawable.ic_user_placeholder), 200));               
        }
    }
}   

下载并设置标记上的图像后,我在地图上添加了标记

    View viewLocationMarker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_location_marker, null);
    ImageView ivPictureLocationMarker = (ImageView) viewLocationMarker.findViewById(R.id.ivPictureLocationMarker);

    new DownloadEmployeeImage(ivPictureLocationMarker).execute(TheURLOfTheImage);       

    Marker locationMarker = googleMap.addMarker(new MarkerOptions()
    .position(new LatLng(latitude, longitude)).title(name).anchor(0.33f, 1)
    .icon(BitmapDescriptorFactory.fromBitmap(TarkieManagerLib
            .createDrawableFromView(MainActivity.this, viewLocationMarker))));

关于如何做到这一点的任何绝妙主意?

【问题讨论】:

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


    【解决方案1】:

    好的,我认为问题出在下面的代码中

    new DownloadEmployeeImage(ivPictureLocationMarker).execute(TheURLOfTheImage);

    Marker locationMarker = googleMap.addMarker(new MarkerOptions()
    .position(new LatLng(latitude, longitude)).title(name).anchor(0.33f, 1)
    .icon(BitmapDescriptorFactory.fromBitmap(TarkieManagerLib
            .createDrawableFromView(MainActivity.this, viewLocationMarker))));
    

    在此代码中,您将下载图像(调用 asynktask)并按顺序在标记上设置图像,但实际上这两个过程并行工作。你的问题是如何?我会详细解释。 查看何时调用 Asynctask 然后它在后端工作并执行下面的代码。所以在这里你需要加载图像并在它加载系统之前设置标记。所以在这种情况下只需设置标记 在 OnPostexecute(...){

    Marker locationMarker = googleMap.addMarker(new MarkerOptions()
    .position(new LatLng(latitude, longitude)).title(name).anchor(0.33f, 1)
    .icon(BitmapDescriptorFactory.fromBitmap(TarkieManagerLib
            .createDrawableFromView(MainActivity.this, viewLocationMarker))));
    

    这里还要在地图上设置标记......

    } 就是这样……

    【讨论】:

      【解决方案2】:

      您必须使用自定义标记在标记上显示图像。点击此链接:

      Google Maps Android API v2 - Interactive InfoWindow (like in original android google maps)

      【讨论】:

      • 我在这里使用了自定义标记。实际上,我能够下载图像并将其设置在标记上。唯一的问题是,标记上的图像在设置后没有立即显示(在 onPostExecute 上)。我需要再次刷新或添加标记才能看到标记上的图像。
      • 我想是的,我遇到了问题,实际上你已经在执行下面写了 marker.set 图标。你应该在 postexecute() 中写它。实际上,异步任务执行代码并行和你正在设置下面的图片,有可能它没有被执行
      • 是的,我明白了。我必须在 postexecute() 上设置图像。顺便说一句,谢谢!
      猜你喜欢
      • 2013-08-28
      • 2020-02-16
      • 1970-01-01
      • 2015-05-09
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多