如何在Android应用中添加自定义.png文件作为Google地图标记

时间:2013-03-19 22:41:59

标签: android google-maps google-maps-markers

我有一个活动,显示使用谷歌地图的Android应用程序的地图,我想用我创建的ping图像替换我正在使用的黄色标记,名为fillingstation.png。我将如何做到这一点,因为我对Android App Development还不熟悉。

MapViewActivity.java

 private void showFuelStops(String location) throws IOException{
       Geocoder geocoder = new Geocoder(this, Locale.getDefault());  
       List<Address> addresses;

       addresses = geocoder.getFromLocationName(location, 1);
       if(addresses.size() > 0) {
          BitmapDescriptor subwayBitmapDescriptor = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW);
          double latitude= addresses.get(0).getLatitude();
          double longitude= addresses.get(0).getLongitude();
          LatLng subLoc = new LatLng(latitude, longitude);
          Marker fuelMarker = map.addMarker(new MarkerOptions().position(subLoc).icon(subwayBitmapDescriptor).title("Subway, " + location));

       }        
 }

1 个答案:

答案 0 :(得分:7)

如果PNG文件是资源(即,res/drawable-NNNN/用于各种密度),请在fromResource()上使用defaultMarker()代替BitmapDescriptorFactory

如果PNG文件是资产(即项目中的assets/),请使用fromAsset()

如果PNG文件是设备内部存储空间中的文件,请使用fromFile()