Android Google Maps API v2-AS无法识别自定义标记.png文件 [英] Android Google Maps API v2 - Custom marker .png file not recognized by AS
问题描述
我正在尝试使用.png文件自定义标记.它在drawable下复制为gsw.png.
I am trying to customize my marker with a .png file. It is copied under drawable as gsw.png.
当我将代码放置在代码中作为R.drawable.gsw时,该代码(在MapActivity.java中)显示该图标的缩略图.但是,出现错误,错误无法找到符号变量gsw".调用setUpMap()来设置带有所有标记的地图.
The code (in MapActivity.java) shows the thumbnail of the icon when I place it in the code as R.drawable.gsw However, I get the error, "error cannot find symbol variable gsw". setUpMap() is called to set up the map with all the markers.
我看到有人通过清理,重新启动eclipse/Android Studio等解决了该问题.它对我不起作用.有人可以帮忙吗?
I see this has been solved for people by cleaning up, restarting eclipse/Android Studio etc. It does not work for me. Can anyone help?
MapActivity.java中的代码
Code in MapActivity.java
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
mMap.addMarker(new MarkerOptions().position(new LatLng(37.497836, -121.9216775)).title("Title"));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(18.500486, 73.866899))
.title("Pune")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.gsw)));
}
推荐答案
Android不会以这种方式加载PNG -不要问我为什么.一种选择是将PNG另存为BMP,但是您可能不想这样做,因为您担心与另一个应用程序的交叉兼容性.不管您是什么原因,我都认为这段代码非常有用:
Android won't load PNGs this way -- don't ask me why. One option is to save the PNG as a BMP, but maybe you don't want to do this because you're concerned about cross-compatibility with another application. Whatever your reason, I found this code to be very useful:
Bitmap img = BitmapFactory.decodeResource(getResources(),R.drawable.your_drawable);
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(img);
marker.icon(bitmapDescriptor);
这篇关于Android Google Maps API v2-AS无法识别自定义标记.png文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!