Mapbox Driving Direction with Custom Marker

為{幸葍}努か 提交于 2019-11-29 16:57:01

The Mapbox Directions API inserts the standard markers on top of your custom ones. You also seem to add the directionsLayer and directionsRoutesControl several times.

This should work:

var geoJson = {
    type: "FeatureCollection",
    features: [
        {
            type: "Feature",
            properties: {
                "marker-color": "#f76565",
                "marker-symbol":"circle-stroked",
                "marker-color":"ff1f20",
                "marker-size": "medium",                
                route: {id: 1, type: "origin", points: 2}
            },
            geometry: {
                type: "Point",
                coordinates: ['144.9758769', '-37.8437403']}
        },{
            type: "Feature",
            properties: {
                "marker-color": "#f76565",
                "marker-symbol":"circle-stroked",
                "marker-color":"23be20",
                "marker-size":"medium",                
                route: {id: 2, type: "destination", points: 3}
            },
            geometry: {
                type: "Point",
                coordinates: ['115.869578', '-31.980216']
            }
        }
    ]
};

var origin = geoJson.features[0];
var destination = geoJson.features[1];

var directions = L.mapbox.directions();
var directionsLayer = L.mapbox.directions.layer(directions).addTo(map);
var directionsRoutesControl = L.mapbox.directions.routesControl('routes', directions).addTo(map);
directions.setOrigin(origin).setDestination(destination).query();

var myLayer = L.mapbox.featureLayer().addTo(map);
myLayer.setGeoJSON(geoJson);

Here is a working example.

All I needed to do was to customise the directions.js file, unfortunately even if you post your customise marker as part of your geoJson because it is hardcoded withing directions.js it won't change it unless you download the library and customise it yourself (you can make it dynamic as you load the geoJson object or change it as hardcode as you want) in the directions.js look for the following code

                L.setOptions(this, options);
            this._directions = directions || new L.Directions();
            L.LayerGroup.prototype.initialize.apply(this);

            this._drag = debounce(L.bind(this._drag, this), 100);

            this.originMarker = L.marker([0, 0], {
                draggable: !this.options.readonly,
                icon: L.mapbox.marker.icon({
                    "marker-symbol": "circle-stroked",
                    "marker-color": "23be20",
                    "marker-size": "medium",
                    "stroke": "#e8b920",
                    "stroke-opacity": "1.0",
                    "stroke-width": 5,
                    "fill":"#e8b920",
                })
            }).on('drag', this._drag, this);

            this.destinationMarker = L.marker([0, 0], {
                draggable: !this.options.readonly,
                icon: L.mapbox.marker.icon({
                    "marker-symbol": "circle-stroked",
                    "marker-color": "ff1f20",
                    "marker-size": "medium",
                    "stroke": "#e8b920",
                    "stroke-opacity": "1.0",
                    "stroke-width": 5,
                    "fill":"#e8b920",
                })
            }).on('drag', this._drag, this);

            this.stepMarker = L.marker([0, 0], {
                icon: L.divIcon({
                    className: 'mapbox-marker-drag-icon mapbox-marker-drag-icon-step',
                    iconSize: new L.Point(12, 12)
                })
            });

            this.dragMarker = L.marker([0, 0], {
                draggable: !this.options.readonly,
                icon: this._waypointIcon()
            });

            this.dragMarker
                .on('dragstart', this._dragStart, this)
                .on('drag', this._drag, this)
                .on('dragend', this._dragEnd, this);

            this.routeLayer = L.geoJson(null, {
                style: this.options.routeStyle
            });
            this.routeHighlightLayer = L.geoJson(null, {
                style: this.options.routeStyle
            });

            this.waypointMarkers = [];
        },
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!