簡體   English   中英

從 URL 在 Google Maps v2 Android 上設置標記圖標

[英]Set marker icon on Google Maps v2 Android from URL

我無法將 Google Places API 提供的圖標添加到標記中。 例如,我希望從 Google Places API 的 JSON 返回圖標:

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

我看了這篇文章,答案建議使用一些 AsyncTask 來下載圖像。 有沒有其他方法可以做到這一點(這種方式看起來很慢)? 是否有設置 Google Places API 提供的標記圖像的標准方法?

這是標記Google Maps Android API v2 文檔

謝謝

嗨,使用可幫助您從 URL 查看圖像的庫。 訪問https://github.com/nostra13/Android-Universal-Image-Loader for Android-Universal-Image-Loader

您也可以訪問以了解下載圖像

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

在那之后

對於自定義標記圖標,您可以使用圖標顯示為標記。 從任何類型的受支持來源加載圖標。

fromAsset(String assetName) – 從資產文件夾加載

fromBitmap (Bitmap image) – 加載位圖圖像

fromFile (String path) – 從文件加載

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

您可以使用以下代碼將 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);
                }

            }
        });

    }

有很多庫可以幫助您從 URL 查看圖像,例如: URLImageViewhelper不幸的是,如果沒有AsyncTask ,您無法從 URL 加載圖像,但是所有幫助您從 URL 查看圖像的庫都使用Asynctask因此您只需調用功能。

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

在調用 .icon() 時執行以下操作

 protected Marker createMarker(double latitude, double longitude, String title, String snippet, String imageUrl) {

    MarkerOptions markerOptions = new MarkerOptions();
    return googleMap.addMarker(markerOptions
            .position(new LatLng(latitude, longitude))
            .anchor(0.5f, 0.5f)
            .title(title)
            .snippet(snippet)
            .icon(BitmapDescriptorFactory.fromBitmap(getBitmapFromLink(imageUrl))));
}

此方法會將圖像 url 轉換為位圖

   public Bitmap getBitmapFromLink(String link) {
    try {
        URL url = new URL(link);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        try {
            connection.connect();
        } catch (Exception e) {
            Log.v("asfwqeds", e.getMessage());
        }
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        Log.v("asfwqeds", e.getMessage());
        e.printStackTrace();
        return null;
    }
}

現在,connection.connect() 中會發生一個異常,表示 android.os.NetworkOnMainThreadException。 要處理此異常,請在您創建時或添加標記之前添加此代碼

if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM