Android将自定义图标设置为标记在Marshmallow中返回null

时间:2016-07-21 14:49:32

标签: android maps

我正在尝试为谷歌地图中的标记设置自定义图标,它可以正常运行Android 4.1.2的手机,但我测试了我的应用程序与运行6.0.1的三星Galaxy tabS2似乎它找不到图标。 下面是我的代码

@Override
public void onMapReady(GoogleMap googleMap) {
    deviceMap = googleMap;
    deviceMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng point) {
            if(customM != null)
                customM.remove();
            customM = deviceMap.addMarker(new MarkerOptions().position(point).icon(bmD).title(getString(R.string.customPos)));
        }
    });
    if (deviceMap != null) {
        try{
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.temp_bts_icon);
            deviceM = deviceMap.addMarker(new MarkerOptions().position(devicePos).title(getString(R.string.devicePos))
                    .icon(BitmapDescriptorFactory.fromBitmap(bm)));
            bm = BitmapFactory.decodeResource(getResources(), R.drawable.phone);
            telephoneM = deviceMap.addMarker(new MarkerOptions().position(telephonePos).title(getString(R.string.telephonePos))
                    .icon(BitmapDescriptorFactory.fromBitmap(bm)));
        }catch (Exception e){
            Toast.makeText(Globals.MainActivity, "Error retrieving icons", Toast.LENGTH_SHORT).show();
            deviceM = deviceMap.addMarker(new MarkerOptions().position(devicePos).title(getString(R.string.devicePos))
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
            telephoneM = deviceMap.addMarker(new MarkerOptions().position(telephonePos).title(getString(R.string.telephonePos))
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
        }

        updateDeviceMarker();
        deviceMap.getUiSettings().setMyLocationButtonEnabled(true);
        deviceMap.getUiSettings().setZoomGesturesEnabled(true);
        deviceMap.getUiSettings().setZoomControlsEnabled(true);
        deviceMap.getUiSettings().setCompassEnabled(true);
        deviceMap.getUiSettings().setAllGesturesEnabled(true);
        deviceMap.moveCamera(CameraUpdateFactory.newLatLngZoom(deviceM.getPosition(), 1f));
    }
}

我也试过这样做:

 telephoneM = deviceMap.addMarker(new MarkerOptions().position(telephonePos).title(getString(R.string.telephonePos).icon(BitmapDescriptorFactory.fromResource(R.drawable.temp_bts_icon));

1 个答案:

答案 0 :(得分:0)

我正在与您分享我的标记代码,这在Android Marshmallow上也能正常运行。

来自资源:

map.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.
                fromResource(R.drawable.xyz)));

来自位图:

Bitmap markerBitmap = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(markerBitmap);
Drawable shape = ResourcesCompat.getDrawable(getResources(), R.drawable.xyz, null);
shape.setBounds(0, 0, markerBitmap.getWidth(), markerBitmap.getHeight());
shape.draw(canvas); 
map.addMarker(new MarkerOptions().position(newLatLng(lastLocation.getLatitude(),
             lastLocation.getLongitude())).icon(BitmapDescriptorFactory.fromBitmap(markerBitmap)));