2014-03-03 117 views
2

我無法將Google Places API提供的圖標添加到標記。例如,我想有該圖標在谷歌Places API的的JSON返回:谷歌地圖v2安卓設置標記圖標從URL

"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png" 

這裏是我怎麼做,現在不添加圖標:

for (Place place : mNearPlaces.results) { 
    LatLng placeLatLng = new LatLng(place.geometry.location.lat, 
     map.addMarker(new MarkerOptions() 
      .position(placeLatLng) 
      .title(place.name)); 
} 

我看着this post和答案建議使用一些AsyncTask來下載圖像。有沒有其他的方式來做到這一點(這種方式似乎很慢)?有沒有設置Google Places API提供的標記圖片的標準方式?

這裏是Google Maps Android API v2 Documentation for Markers

感謝

回答

3

嗨使用圖書館,可以幫助您查看來自URL的圖像。參觀https://github.com/nostra13/Android-Universal-Image-LoaderAndroid的通用 - 圖片 - 裝載機

還可以參觀瞭解下載的圖像

http://brainflush.wordpress.com/2009/11/23/droid-fu-part-2-webimageview-and-webgalleryadapter/

,之後

爲自定義標記圖標,就可以使用圖標顯示爲標記。從任何支持的來源加載圖標。

fromAsset(字符串ASSETNAME) - 裝載從資產文件夾

fromBitmap(位圖圖像​​) - 裝載位圖圖像

FROMFILE(字符串路徑) - 裝載從文件

fromResource(INT RESOURCEID) - 從繪製資源加載

嘗試如下

// latitude and longitude 
double latitude = 17.385044; 
double longitude = 78.486671; 

// create marker 
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps"); 

// Changing marker icon 
// set yours icon here 
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker_icon))); 

// adding marker 
googleMap.addMarker(marker); 
1

有很多庫,可幫助您從網址查看圖像,如:URLImageViewhelper不幸的是,你不能加載從URL的圖像,而不AsyncTask但是所有的庫,幫助您查看來自URL的圖像正在使用Asynctask,因此只需要調用該功能即可。

庫類的實現在github頁面中可用。

2

您可以使用以下代碼將url映像加載到GoogleMap mapMarker。第一設置其他屬性,如標題,摘要,位置等....

mapMarker= mGoogleMap.addMarker(new MarkerOptions() 
        .title(Integer.toString(total_steps)) 
        .snippet("Steps") 
        .position(new LatLng(current_lat,current_longi))); 
      mapMarker.setTag("current_position"); 
      mapMarker.showInfoWindow(); 
      loadMarkerIcon(mapMarker); 

使用後的自定義方法來加載用於圖標特性的圖像。您可以將Glide gradle鏈接添加到項目。

private void loadMarkerIcon(final Marker marker) { 
     String burlImg = "Url_imagePath; 
     Glide.with(this).load(burlImg) 
       .asBitmap().fitCenter().into(new SimpleTarget<Bitmap>() { 
      @Override 
      public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) { 

       if(bitmap!=null){ 
        // Bitmap circularBitmap = getRoundedCornerBitmap(bitmap, 150); 
        Bitmap mBitmap = getCircularBitmap(bitmap); 
        mBitmap = addBorderToCircularBitmap(mBitmap, 2, Color.WHITE,squareBitmapWidth); 
        BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(mBitmap); 
        marker.setIcon(icon); 
       } 

      } 
     }); 

    }