2015-04-01 167 views
1

我有一個帶有標記的地圖視圖。我想知道是否可以將其他字符串值添加到諸如電話號碼和網站等標記。點擊標記時,我不希望這些信息顯示在信息窗口中。當點擊信息窗口時,它會轉到所選標記的詳細活動。標記標題和代碼片段作爲附加內容傳遞給細節活動,我想另外傳遞兩個附加字符串作爲附加內容。自定義地圖標記

這裏是我創造的標誌:

for(int i = 0; i < Lat.length; i++) { 
      Marker marker = map.addMarker(new MarkerOptions() 
      .position(new LatLng(Lat[i], Lon[i])) 
      .title(Market[i]) 
      .snippet(Address[i]) 
      .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))); 

      list.add(marker); 
     } 

這裏是我開始了詳細的活動。

map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() { 
      @Override 
      public void onInfoWindowClick(Marker marker) { 
       // Show Details 
       Intent intent = new Intent(getActivity(), FarmMarketDetails.class); 
       intent.putExtra("selectedTitle", marker.getTitle()); 
       intent.putExtra("selectedAddress", marker.getSnippet()); 
       startActivity(intent); 
      } 
     }); 

回答

1

我創建的自定義標記用下面的代碼,請檢查是否是有幫助的,你自定義標記

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.setColor(Color.BLUE); 
     paint.setTypeface(tf); 
     paint.setTextAlign(Align.CENTER); 
     paint.setTextSize(convertToPixels(context,11)); 

     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(context,7));  //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) ; 

    } 
+0

所有我想要做的是設置一個電話號碼

Marker marki=map.addMarker(new MarkerOptions() .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.my, ""))) .position((new LatLng(latitude,longitude)))) ; 

方法以及每個標記的網站值,例如設置標題和代碼段。 – raginggoat 2015-04-01 13:57:05

+0

難道你不想顯示,當地圖加載我認爲這樣! – 2015-04-01 13:59:26

+0

不需要。我希望所有內容都完全一樣,除了使用與每個標記關聯的電話號碼和網站的附加字符串。我會像設置標記標題和代碼片段一樣設置這些值。然後,我可以將這些值作爲一個額外的意圖傳遞給我的詳細活動,就像我目前正在處理標題和片段一樣。 – raginggoat 2015-04-01 14:19:14