2017-02-19 162 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 

希望这会有所帮助。