2017-02-19 159 views
0

我試圖在Google地圖上放置標記。但是,雖然我調試(所有東西都很好),但我的標記卻沒有顯示在地圖上。你能幫我解決問題嗎?BitmapDescriptorFactory.fromPath圖像未在地圖上顯示

           String imageInSD = Config.APP_IMAGES_URL + sh.cover_image_file;[enter image description here][1] 



                   customMarker = googleMap.addMarker(new MarkerOptions() 
                     .position(markerLatLng) 
                     .title(sh.name) 
                     .snippet(sh.description.substring(0, Math.min(sh.description.length(), 80)) + "...") 
                     .icon(BitmapDescriptorFactory.fromPath(imageInSD)) // in debug looking www.asd.com/abc.jpg(right paths) 
                     .anchor(0.5f, 1)); 

我也試過這個

.icon(BitmapDescriptorFactory.FromFile(imageInSD))

但不工作?問題在哪裏 在調試中尋找正確的路徑。增加了截圖。但在應用圖中爲空

+0

哪裏*確切*是'imageInSD'指向?您的評論不是完整的文件系統路徑。 – CommonsWare

+0

我無法對此表示抱歉。你什麼意思? 它是圖像路徑。 我有一個「for」循環。每次標記都必須得到另一個圖像。當我調試我控制的imageInSD每次獲得TRUE路徑。但在應用程序中它不起作用。空地圖顯示。 爲EXM 1.loop - 在這裏BitmapDescriptorFactory> imageInSD = www.abc.com/2.jpeg ... - > imageInSD = www.abc.com/1.jpeg 2.loop。 fromPath(imageInSD)不工作,我認爲 – user3404273

回答

0

www.abc.com/1.jpeg不是有效的文件系統路徑。它看起來像一個缺少其方案的HTTP URL。

fromPath()需要文件系統路徑。給定一個指向圖像的File對象,使用getAbsolutePath()將其轉換爲文件系統路徑的String表示形式,並將該值傳遞給fromPath()

如果您的imageInSD值實際上是網址,您需要先下載這些圖片。 BitmapDescriptorFactory不會爲你下載。而大多數圖像加載庫的目標是像ImageView這樣的東西,所以大多數不會幫助你。你可以看看是否有人使用像畢加索這樣的庫來填充標記。否則,在添加標記之前,請使用HttpURLConnection,OkHttp等來下載圖像。

+0

其實我可以說錯了。是的,我需要從我的服務器獲取每個圖像的http url。但在.icon(BitmapDescriptorFactory.fromPath(imageInSD))我couldnt。 你能幫助我怎麼解決。謝謝 – user3404273

+0

@ user3404273:「我需要從我的服務器獲取每個圖像的http url」 - 您需要自己下載這些圖像。查看更新後的答案。 – CommonsWare

+0

謝謝,但你有代碼示例?我不是那麼專業。取決於你的話,我需要一些幫助來獲取數據。 – user3404273

0

下面是一個類的示例,可用於從網站下載並轉換爲gmaps API可顯示的內容。我從我正在開發的一個項目中解除了這個問題,並且剔除了不適用的東西 - 沒有嘗試編譯它,但它應該很接近。

class DownloadWebpageAsynch extends AsyncTask<String, Void, Integer> 
{ 
    private String fileName; 
    private final String TAG = "DownloadWebpageAsynch"; 

    // onPostExecute displays the results of the AsyncTask. 
    @Override 
    protected void onPostExecute(Integer result) 
    { 
     // Perforrm any work here on the UI thread 
    // in your case, create the bitmap for the google map 

    BitmapDescriptor bd = BitmapDescriptorFactory.fromPath(fileName); 

    // use bd in google map calls as the bitmap to add to the map 
    } 

    protected void DownloadComplete(BufferedInputStream inStream) 
    { 
    // Perform work here upon download complete on the background thread 
     // inStream is what was returned by the web server 

    // Safe file to temp storage 
    try 
    { 
     File tempFile = File.createTempFile("1", ".jpg"); 
     tempFile.deleteOnExit(); 
     fileName = tempFile.toString(); 
     FileOutputStream fos = new FileOutputStream(tempFile); 
     byte[] readyBytes = new byte[1000]; 
     int numRead = inStream.read(readyBytes, 0, 999); 
     while(numRead != -1) 
     { 
     fos.write(readyBytes, 0, numRead); 
     numRead = inStream.read(readyBytes, 0, 999); 
     } 
     fos.close(); 
    } catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    } 



    @Override 
    protected Integer doInBackground(String... urls) 
    { 
     // params comes from the execute() call: params[0] is the url. 
     try 
     { 
     downloadUrl(urls[0]); 
     } 
     catch (IOException e) 
     { 
      Log.d(TAG, "Failed with exception " + e.toString()); 
     } 
     return 0; 
    } 

    // Given a URL, establishes an HttpUrlConnection and retrieves 
    // the web page content as a InputStream, which it returns as 
    // a string. 
    private void downloadUrl(String myurl) throws IOException 
    { 
     BufferedInputStream is = null; 
     downloadFailed = false; 

     try 
     { 
      URL url = new URL(myurl); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setReadTimeout(10000); // 10 second timeout 
      conn.setConnectTimeout(10000); // 10 second timeout 
      conn.setDoInput(true); 
      // Starts the query 
      conn.connect(); 
      responseCode = conn.getResponseCode(); 
      if(responseCode != 200) 
      { 
       Log.d(TAG, "Download Failed"); 
      } 
      else 
      { 
      DownloadComplete(new BufferedInputStream(conn.getInputStream()); 
      } 
      conn.disconnect(); 
     } 
     catch (SocketTimeoutException e) 
     { 
     Log.d(TAG, "Failed with exception " + e.toString()); 
     } 
     finally 
     { 
      if (is != null) 
      { 
       is.close(); 
      } 
     } 
    }// private String downloadUrl(String myurl) throws IOException 

希望這會有所幫助。