Android MapBox使用之自定义MapboxMap.InfoWindowAdapter

首先看一下向地图上添加一个marker

mMap.addMarker(new MarkerOptions()
                    .title("title")
                    .snippet("snippet")
                    .icon(IconFactory.getInstance(MyApplication.getInstance()).fromResource(R.drawable.point))
                    .anchor(0.5f, 0.5f)
                    .position(Location));

默认点击marker显示气泡形式第一行title 第二行为snippet 而且必须是字符串

如果有需求想修改其中内容,那就需要自己自定义了,我们看一下MapboxMap.InfoWindowAdapter

/**
   * Interface definition for a callback to be invoked when an info window will be shown.
   *
   * @see MapboxMap#setInfoWindowAdapter(InfoWindowAdapter)
   */
  public interface InfoWindowAdapter {
    /**
     * Called when an info window will be shown as a result of a marker click.
     *
     * @param marker The marker the user clicked on.
     * @return View to be shown as a info window. If null is returned the default
     * info window will be shown.
     */
    @Nullable
    View getInfoWindow(@NonNull Marker marker);
  }

可以看到是接口实现它就可以了,代码如下:

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.maps.MapboxMap;

public class CustomInfoWindowAdapter implements MapboxMap.InfoWindowAdapter{
    private Context mContext;

    public CustomInfoWindowAdapter(Context context) {
        this.mContext=context;
    }

    @Nullable
    @Override
    public View getInfoWindow(@NonNull Marker marker) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.custom_info_contents, null);
        ImageView custom_marker_title= (ImageView) view.findViewById(R.id.custom_marker_title);
        TextView custom_marker_snippet= (TextView) view.findViewById(R.id.custom_marker_snippet);
        custom_marker_title.setImageResource(R.drawable.ic_laucner);
        custom_marker_snippet.setText("我上面有个图片");
        return view;
    }
}

最后不要忘了设置使用自定义:  mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(this));

ok 这就完成了




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值