Android MapBox使用之自定义Marker(三)

前面两篇讲的是自定义MarkerView,而MarkerView已经被MapBox废弃了,本篇给出一个Marker的例子。

自定义CustomMarker:

public class CustomMarker extends Marker{
    Icon iconBig;
    /**
     * Creates a instance of {@link Marker} using the builder of Marker.
     *
     * @param baseMarkerOptions The builder used to construct the Marker.
     */
    public CustomMarker(BaseMarkerOptions baseMarkerOptions) {
        super(baseMarkerOptions);
    }

    public Icon getIconBig() {
        return iconBig;
    }

    public void setIconBig(Icon iconBig) {
        this.iconBig = iconBig;
    }
}

 自定义CustomMarkerOptions:

public class CustomMarkerOptions extends BaseMarkerOptions<CustomMarker,CustomMarkerOptions> implements Parcelable {

    public static final Creator<CustomMarkerOptions> CREATOR = new Creator<CustomMarkerOptions>() {
        public CustomMarkerOptions createFromParcel(Parcel in) {
            return new CustomMarkerOptions(in);
        }

        public CustomMarkerOptions[] newArray(int size) {
            return new CustomMarkerOptions[size];
        }
    };

    public CustomMarkerOptions() {
    }

    protected CustomMarkerOptions(Parcel in) {
        this.position((LatLng)in.readParcelable(LatLng.class.getClassLoader()));
        this.snippet(in.readString());
        this.title(in.readString());
        if(in.readByte() != 0) {
            String iconId = in.readString();
            Bitmap iconBitmap = (Bitmap)in.readParcelable(Bitmap.class.getClassLoader());
            Icon icon = IconFactory.recreate(iconId, iconBitmap);
            this.icon(icon);
        }

    }

    public CustomMarkerOptions getThis() {
        return this;
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeParcelable(this.getPosition(), flags);
        out.writeString(this.getSnippet());
        out.writeString(this.getTitle());
        Icon icon = this.getIcon();
        out.writeByte((byte)(icon != null?1:0));
        if(icon != null) {
            out.writeString(this.getIcon().getId());
            out.writeParcelable(this.getIcon().getBitmap(), flags);
        }

    }

    public CustomMarker getMarker() {
        if(this.position == null) {
            throw new InvalidMarkerPositionException();
        } else {
            return new CustomMarker(this);
        }
    }

    public LatLng getPosition() {
        return this.position;
    }

    public String getSnippet() {
        return this.snippet;
    }

    public String getTitle() {
        return this.title;
    }

    public Icon getIcon() {
        return this.icon;
    }

    public boolean equals(Object o) {
        if(this == o) {
            return true;
        } else if(o != null && this.getClass() == o.getClass()) {
            CustomMarkerOptions marker;
            label59: {
                marker = (CustomMarkerOptions)o;
                if(this.getPosition() != null) {
                    if(this.getPosition().equals(marker.getPosition())) {
                        break label59;
                    }
                } else if(marker.getPosition() == null) {
                    break label59;
                }

                return false;
            }

            label52: {
                if(this.getSnippet() != null) {
                    if(this.getSnippet().equals(marker.getSnippet())) {
                        break label52;
                    }
                } else if(marker.getSnippet() == null) {
                    break label52;
                }

                return false;
            }

            if(this.getIcon() != null) {
                if(!this.getIcon().equals(marker.getIcon())) {
                    return false;
                }
            } else if(marker.getIcon() != null) {
                return false;
            }

            boolean var10000;
            label77: {
                if(this.getTitle() != null) {
                    if(this.getTitle().equals(marker.getTitle())) {
                        break label77;
                    }
                } else if(marker.getTitle() == null) {
                    break label77;
                }

                var10000 = false;
                return var10000;
            }

            var10000 = true;
            return var10000;
        } else {
            return false;
        }
    }

    public int hashCode() {
        int result = 1;
        result = 31 * result + (this.getPosition() != null?this.getPosition().hashCode():0);
        result = 31 * result + (this.getSnippet() != null?this.getSnippet().hashCode():0);
        result = 31 * result + (this.getIcon() != null?this.getIcon().hashCode():0);
        result = 31 * result + (this.getTitle() != null?this.getTitle().hashCode():0);
        return result;
    }
}

自定义CustomMarker使用:

CustomMarkerOptions markerOptions = new CustomMarkerOptions();
markerOptions
      .icon(icon)
      .position(latlng);
CustomMarker customMarker = (CustomMarker) mAMap.addMarker(markerOptions);

Mapbox中添加自定义地图样式可以通过使用Mapbox GL JS库中的`style-spec`语法。Mapbox GL JS允许开发者完全控制地图的视觉呈现,包括标记、颜色、线宽等各个方面。以下是一些基本步骤: 1. **创建`.mapboxgl.style.json`文件**: 创建一个JSON文件,这是地图样式的源文件。在这个文件里,你可以定义图层(layers)、符号(features)以及各种规则(rules),比如根据数据字段调整颜色或透明度。 ```json { "version": "8.0.0", "name": "custom-style-name", // 自定义名称 "sources": { "my-source-id": { /* 数据源配置 */ } }, "layers": [ { "id": "base-layer", // 层级名 "type": "background", "source": "my-source-id", "paint": { "background-color": "#f5f5f5" } }, { "id": "road-layer", "type": "line", "source": "my-source-id", "paint": { "line-color": "#ff0000", "line-width": 6 } } ] } ``` 2. **加载样式到地图**: 在你的JavaScript代码中,使用`mapbox-gl`库的`loadStyle`方法加载这个自定义样式,并将其应用到地图上。 ```javascript import mapboxgl from 'mapbox-gl'; mapboxgl.accessToken = 'your-access-token'; // 替换为你的实际token const map = new mapboxgl.Map({ container: 'map', // 容器元素ID style: 'file:///path/to/your/custom-style.json' // 或者URL }); ``` 3. **根据需求调整样式**: 可以在样式文件中定义更复杂的逻辑,如条件渲染、渐变色、图标集等等。查看官方文档 `https://docs.mapbox.com/mapbox-gl-js/style-spec/` 获取更多详细信息。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值