我试图将标记放在Google地图上。但是,尽管我调试(每一个看起来都不错),但我的标记没有在地图上显示。你能帮我什么问题吗?
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))
但不起作用?问题在哪里在调试中看起来正确的道路。添加了屏幕截图。但是在应用程序图中为null
www.abc.com/1.jpeg
不是有效的文件系统路径。它看起来像是缺少其方案的HTTP URL。
fromPath()
采用文件系统路径。给定指向您图像的File
对象,使用getAbsolutePath()
将其转换为文件系统路径的String
表示,然后将该值传递给fromPath()
。
如果您的imageInSD
值实际上是URL,则需要先下载这些图像。BitmapDescriptorFactory
不会为您下载。而且大多数图像加载库都针对ImageView
之类的东西,因此大多数都不会为您提供帮助。您可以查看某人是否有使用像毕加索这样的库来填充标记的食谱。否则,请在添加标记之前使用HttpURLConnection
,OKHTTP等下载图像。
这是一个类的示例,可用于从网站下载并转换为gmaps apis可以显示的内容。我从一个正在进行的项目中提出了这一点,并拿出了不适用的东西 - 没有尝试编译它,但应该很近。
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
希望这会有所帮助。