2012-03-15 53 views
0

我擁有drawable-hdpi中的所有圖像。我是否需要將它們放入其他可繪製文件中?如果它與疊加層有什麼關係,有人可以給我一個關於如何用疊加層處理每條語句的例子嗎?我有分配給某些地方的圖像,我知道該代碼正在返回地點,但它不會將它們繪製在地圖上。

package com.CS3040.Places; 

import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import com.CS3040.Coursework.R; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.OverlayItem; 

public class PlaceOverlayItem extends OverlayItem { 
    private final GeoPoint point; 
    private final Place place; 
    private final Drawable marker; 
    //private final Context context; 

    public PlaceOverlayItem(Context context, Place p, String type) { 
     super(p.getGeoPoint(), p.getName(), p.getFormatted_address()); 

     if(type.equals("restaurant")) 
     { 
      //this.marker = 

      Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.restaurant); 
      this.marker = new BitmapDrawable(context.getResources(), bmp); 

      //Drawable d = context.getResources().getDrawable(R.drawable.restaurant); 
      //d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
      //this.marker = d; 

//   Resources resources = context.getResources(); 
//   Bitmap bmp = BitmapFactory.decodeResource(resources, R.drawable.restaurant); 
//   this.marker = new BitmapDrawable(resources, bmp); 

     }else if(type.equals("bar||club")){ 
      Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.bars); 
      this.marker = new BitmapDrawable(context.getResources(), bmp); 
     }else if(type.equals("amusement_park||aquarium||park||zoo")){ 
      Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.attractions); 
      this.marker = new BitmapDrawable(context.getResources(), bmp); 
     }else if(type.equals("hotel||motel||bread_and_breakfast")){ 
      Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.hotels); 
      this.marker = new BitmapDrawable(context.getResources(), bmp); 
     }else if(type.equals("shopping")){ 
      Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.shopping); 
      this.marker = new BitmapDrawable(context.getResources(), bmp); 
     }else if(type.equals("food||meal_takeaway")){ 
      Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.fastfood); 
      this.marker = new BitmapDrawable(context.getResources(), bmp); 
     }else{ 
      this.marker = null; 
     } 

     super.setMarker(this.marker); 
     this.point = p.getGeoPoint(); 
     this.place = p; 
    } 

    /** 
    * @return the point 
    */ 
    public GeoPoint getPoint() { 
     return point; 
    } 

    /** 
    * @return the place 
    */ 
    public Place getPlace() { 
     return place; 
    } 

} 

回答

1

我認爲這可能是您測試OverlayItem類型的方式的問題。

條件type.equals("bar||club")只有在類型字符串爲"bar||club"時才爲真。這意味着如果類型是「酒吧」,它不會有任何標記。

我有什麼建議是建立一個靜態的地圖:

private static Map<String, Integer> types = new HashMap<String, Integer>(); 

的初始化一個靜態塊:

static { 
    types.put("bar", R.drawable.bars); 
    types.put("club", R.drawable.bars); 
    // ... the rest of mappings 
} 

,然後在覆蓋的構造簡單地做到這一點:

public PlaceOverlayItem(Context context, Place p, String type) { 
    super(p.getGeoPoint(), p.getName(), p.getFormatted_address()); 
    int resId = types.get(type); 
    Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resId); 
    this.marker = new BitmapDrawable(context.getResources(), bmp); 
    super.setMarker(this.marker); 
    this.point = p.getGeoPoint(); 
    this.place = p; 
} 

另外,你也可以在原始文件夾中創建一個json文件並解析它以獲得th e映射使用:org.json。這樣,您可以更改圖標而無需轉到代碼並可以在其他類中重複使用它。

另一種方法是,如果你覺得實施它很勇敢,那就是使用註釋來創建地圖。

祝你好運

相關問題