首先看一下向地图上添加一个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 这就完成了