自定义标记图像不显示在谷歌地图中

时间:2014-05-23 14:46:16

标签: android google-maps

位图图像不是drawing.i不明白为什么图像被绘制为谷歌地图中的标记。还有其他方法可以自定义谷歌地图标记作为图像 我无法理解图像是否正在加载

final LatLng MELBOURNE = new LatLng(-37.813, 144.962);

            final URL url = new URL(
                    "http://indervilla.com/home/Mickey-Mouse-Cartoon-HD.jpg");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            Bitmap bmImg = BitmapFactory.decodeStream(is);

            Bitmap.Config conf = Bitmap.Config.ARGB_8888;
            Bitmap bmp = Bitmap.createBitmap(80, 80, conf);
            Canvas canvas1 = new Canvas(bmp);

            // paint defines the text color,
            // stroke width, size
            Paint color = new Paint();
            color.setTextSize(20);
            color.setColor(Color.RED);
            color.setStrokeWidth(10);

            // modify canvas
            canvas1.drawBitmap(bmImg, -10, -10, color);


            // add marker to Map
            googleMap.addMarker(new MarkerOptions().position(MELBOURNE)
                    .icon(BitmapDescriptorFactory.fromBitmap(bmp))
                    // Specifies the anchor to be at a particular point in the
                    // marker image.
                    .anchor(.5f, 1));

            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(-37.813, 144.962)).zoom(15).build();

            googleMap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

    /**
     * function to load map If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

}

1 个答案:

答案 0 :(得分:1)

我建议你看一下Maps Utils Library。它有很多简洁的功能,包括相对简单的自定义Marker创建和插入GoogleMap