2014-10-07 119 views
1

我是新手,我正在开发一个跟踪员工位置并在地图上显示他们的android应用程序。这几乎完成了,只是它不显示员工在标记上的图像。刷新google地图上的标记以查看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)))); 

任何我该如何做到这一点的绝妙主意?

回答

1

那么好吧,我认为问题是在这里在下面的代码

新DownloadEmployeeImage(ivPictureLocationMarker)。执行(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.so设置(...){

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

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

} 多数民众赞成它...

+0

不错!它的作用就像一种魅力。谢谢! – mgcaguioa 2014-10-07 11:14:13

0

你必须使用自定义标记用于显示该链路上的marker.Follow图像:

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

+0

我在这里使用自定义标记。实际上,我能够下载图像并将其设置在标记上。唯一的问题是,标记上的图像没有立即显示(在onPostExecute上)。我需要再次刷新或添加标记以查看标记上的图像。 – mgcaguioa 2014-10-07 10:52:24

+0

我想是的,我得到了问题,实际上你已经在执行下面写了marker.set图标。你应该把它写在postexecute()中。实际上,一个异步任务执行代码paralley并且你正在设置下面的图像,是机会它不会被执行 – 2014-10-07 10:58:06

+0

是的,我明白了。我必须在postexecute()上设置图像。顺便说一句,谢谢! – mgcaguioa 2014-10-07 11:16:33