2016-09-04 12 views
7

マップ上にユーザーを表示しようとしていますが、既にマップをアプリケーションに実装していますが、ユーザーの写真をピンの中に表示するカスタムマーカーを作成しようとしていますこれは: enter image description hereピン内にユーザー画像があるカスタムマーカー

どうすればいいですか?

+0

Uが見ている[これ](https://developers.google.com/maps/documentation/android-api/marker?hl= ja Markerオブジェクトのiconプロパティがあります –

+0

私はそれを見ましたが、それは十分に助けになりませんでした –

答えて

1

独自のビットマップでマーカーをカスタマイズできます。

private static final LatLng mArea= new LatLng(lat, long); 
private Marker marker= mMap.addMarker(new MarkerOptions() 
          .position(mArea) 
          .title("mArea") 
          .snippet("Snippet").icon(yourBitmap)); 
+0

ビットマップのための簡単なコードを書くことができますか? ..ありがとうございます –

+0

http://stackoverflow.com/questions/14811579/how-to-create-a-custom-shaped-bitmap-marker-with-android-map-api-v2 どちらかあなたはカスタムビットマップを作成する必要がありますキャンバスを使用するか、単にWebサービスを介して取得することができます –

4

私は電報アプリTelegram App

からの参照を取ることによってこれを行ってきたビットマップ

private Bitmap createUserBitmap() { 
    Bitmap result = null; 
    try { 
     result = Bitmap.createBitmap(dp(62), dp(76), Bitmap.Config.ARGB_8888); 
     result.eraseColor(Color.TRANSPARENT); 
     Canvas canvas = new Canvas(result); 
     Drawable drawable = getResources().getDrawable(R.drawable.livepin); 
     drawable.setBounds(0, 0, dp(62), dp(76)); 
     drawable.draw(canvas); 

     Paint roundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     RectF bitmapRect = new RectF(); 
     canvas.save(); 

     Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.avatar); 
     //Bitmap bitmap = BitmapFactory.decodeFile(path.toString()); /*generate bitmap here if your image comes from any url*/ 
     if (bitmap != null) { 
      BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 
      Matrix matrix = new Matrix(); 
      float scale = dp(52)/(float) bitmap.getWidth(); 
      matrix.postTranslate(dp(5), dp(5)); 
      matrix.postScale(scale, scale); 
      roundPaint.setShader(shader); 
      shader.setLocalMatrix(matrix); 
      bitmapRect.set(dp(5), dp(5), dp(52 + 5), dp(52 + 5)); 
      canvas.drawRoundRect(bitmapRect, dp(26), dp(26), roundPaint); 
     } 
     canvas.restore(); 
     try { 
      canvas.setBitmap(null); 
     } catch (Exception e) {} 
    } catch (Throwable t) { 
     t.printStackTrace(); 
    } 
    return result; 
} 

計算DPを作成するために、Googleマップで

GoogleMap mMap; 
Marker marker; 

LatLng latLng = new LatLng(Double.parseDouble(lat), Double.parseDouble(long)); 
MarkerOptions options = new MarkerOptions().position(latLng); 
Bitmap bitmap = createUserBitmap(); 
if(bitmap!=null){ 
    options.title("Ketan Ramani"); 
    options.icon(BitmapDescriptorFactory.fromBitmap(bitmap)); 
    options.anchor(0.5f, 0.907f); 
    marker = mMap.addMarker(options); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null); 
} 

機能をマーカーの追加に従ってデバイス密度

public int dp(float value) { 
    if (value == 0) { 
     return 0; 
    } 
    return (int) Math.ceil(getResources().getDisplayMetrics().density * value); 
} 

livepin.png

avatar.png

Output

関連する問題