2017-12-14 12 views
2

アンドロイドマップのカスタムマーカーを作成しようとしています。 GoogleユーティリティのIconGeneratorを使用しています。私のカスタムマーカーには、ParseImageView(Parse SDKの画像ビュー)が含まれています。画像ビューでカスタムマーカーをマップできない

これはカスタムマーカーのコードです。

これは私の関数の内部generateRichMarkerIcon

 IconGenerator iconGenerator = new IconGenerator(getContext()); 
     ParseImageView imageView = new ParseImageView(getContext()); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     imageView.setLayoutParams(new ViewGroup.LayoutParams(70, 70)); 
     imageView.setParseFile(imageFile); 
     imageView.loadInBackground(); 

     iconGenerator.setContentView(imageView); 

     if (selected) { 
      iconGenerator.setColor(Color.BLUE); 
     } 

     return BitmapDescriptorFactory.fromBitmap(iconGenerator.makeIcon()); 

され、その後、私はまだこの

BitmapDescriptor icon = generateRichMarkerIcon(false, card); 
      Marker marker = this.googleMap.addMarker(new MarkerOptions() 
        .icon(icon) 
        .position(new LatLng(lat, lng))); 

のようにそれを使用し、私は、サイズは大丈夫です(画像が、空白のマーカーを見ることができません70x70)を使用しています。

これはスクリーンショットです:このコードと間違っている何https://drive.google.com/file/d/1-t5FOrKotac5YMfNoLbZo1NFjEFGpqtg/view?usp=sharing

答えて

0

画像が表示される前にアイコンを作って、画像をバックグラウンドで読み込むときに、画像が読み込まれるまで待たなければならないことが原因かもしれません。

+0

ありがとうございます!それはそれでした:-) – Abhab

0

キャンバスを使用してマーカーをカスタマイズすることもできます。方法

private Bitmap writeTextOnDrawable(int drawableId, String text) { 

     Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId) 
       .copy(Bitmap.Config.ARGB_8888, true); 

     Typeface tf = Typeface.create("Helvetica", Typeface.BOLD); 

     Paint paint = new Paint(); 
     paint.setStyle(Paint.Style.FILL); 
     paint.setColor(Color.WHITE); 
     paint.setTypeface(tf); 
     paint.setTextAlign(Paint.Align.CENTER); 
     paint.setTextSize(convertToPixels(MapActivity.this, 18)); 

     Rect textRect = new Rect(); 
     paint.getTextBounds(text, 0, text.length(), textRect); 

     Canvas canvas = new Canvas(bm); 

     //If the text is bigger than the canvas , reduce the font size 
     if (textRect.width() >= (canvas.getWidth() - 4))  //the padding on either sides is considered as 4, so as to appropriately fit in the text 
      paint.setTextSize(convertToPixels(MapActivity.this, 18));  //Scaling needs to be used for different dpi's 

     //Calculate the positions 
     int xPos = (canvas.getWidth()/2) - 2;  //-2 is for regulating the x position offset 

     //"- ((paint.descent() + paint.ascent())/2)" is the distance from the baseline to the center. 
     int yPos = (int) ((canvas.getHeight()/2) - ((paint.descent() + paint.ascent())/2)); 

     canvas.drawText(text, xPos, yPos, paint); 

     return bm; 
    } 


    public static int convertToPixels(Context context, int nDP) { 
     final float conversionScale = context.getResources().getDisplayMetrics().density; 

     return (int) ((nDP * conversionScale) + 0.5f); 

    } 

・ホープこれはあなたを助け、次の

Marker myLocMarker = mMap.addMarker(new MarkerOptions() 
         .position(sydney) 
         .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_marker_black, "$125")))); // where ic_marker_black is my marker custom Image, & "$125" is text i want to put on marker. 

用途:私はそれが私を助けキャンバス&を使用してそれをやりました!

あなたの質問はQuestionのようになります。このtutorialも参照してください。

+0

私はすでにキャンバスを使ってみましたが、何も変わっていません... – Abhab

+0

@AbhabあなたのOutPutスクリーンを共有してください。 –

+0

スクリーンショットが追加されました!編集した質問を確認してください:-) – Abhab

関連する問題