java - 我有已分配给某些地点的图像,我知道代码正在返回地点,但它没有将它们绘制在 map 上。

标签 java android drawable google-places-api

我有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;
    }

}

最佳答案

我认为这可能是您测试 OverlayItem 类型的方式有问题。

仅当类型字符串为 "bar||club" 时,条件 type.equals("bar||club") 才会为 true。这意味着如果类型是“bar”,它将不会有任何标记。

我建议创建一个静态 map :

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

在静态 block 上初始化它:

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 文件并解析它以获取映射,使用: org.json 。这样您就可以更改图标,而无需转到代码并能够在其他类中重用它。

如果您有勇气实现它,另一种选择是使用注释来创建 map 。

祝你好运

关于java - 我有已分配给某些地点的图像,我知道代码正在返回地点,但它没有将它们绘制在 map 上。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9719144/

相关文章:

java - 如何去除JTable 中列标题的边框?

Java 邮件 : SSLHandshakeException when sending email on port 25 without SSL

java - Android 5.0.1 更新时出现 Unsatisfiedlinkerror

android - 如何在View的背景上设置drawable?安卓

java - 多个可绘制背景图像以适应屏幕分辨率

java - TestNG 重试类不运行 @BeforeClass 和 @AfterClass 方法

java - 在Java中使用RSA公钥文件加密AES key

android - 没有通知显示安卓

android - 如何在 23 以下的 api 上修复可绘制气泡不正确

安卓径向渐变