-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to display routes? #591
Comments
I'm having the same issue on my end, and from what I gather, there are three ways do draw lines/routes using this library. First, and probably the easiest is using deck.gl's LineLayer. Deck.gl is a companion library of this one, they're both managed by Uber, and deck.gl is great to do all sorts of data visualizations on top of your maps. If all we need is to draw a line, however, it would probably bloat the bundle. The second way is to add a GeoJSON layer to our mapStyle. You can see how it's done in the GeoJSON example. This is the way I'm currently doing it, but I find that working with the mapStyle and GeoJSON is neither easy nor readable. I would also like to have an easier way to use React's composition model, so I could have my line/route components as children of The last way I've found is to create a custom overlay. This is probably the way I would like to go, but the documentation around it isn't very extensive. I found a folder with various overlay examples, but none of them make lines (as far as I could tell). The draggable points overlay might be the place to start, but I haven't gotten that far. My dream scenario would be to have a line component that you could place inside the ReactMapGl component, just like the Marker and like react-leaflet's PolyLine works.
I'm not really happy with my solution to add the GeoJSON to the mapStyle so I plan on redoing it. My question to the maintainers is; would you accept a pr that adds a PolyLine component, similar to the Marker component, or should I create a custom overlay instead? |
One approach, as mentioned above already, would be to go with a custom overlay. I've used the ScatterOverlay example to create my own PolylineOverlay import React, { PureComponent } from 'react'
import { CanvasOverlay } from 'react-map-gl'
export default class PolylineOverlay extends PureComponent {
_redraw ({ width, height, ctx, isDragging, project, unproject }) {
const { points, color = 'red', lineWidth = 2, renderWhileDragging = true } = this.props
ctx.clearRect(0, 0, width, height)
ctx.globalCompositeOperation = 'lighter'
if ((renderWhileDragging || !isDragging) && points) {
ctx.lineWidth = lineWidth
ctx.strokeStyle = color
ctx.beginPath()
points.forEach(point => {
const pixel = project([point[0], point[1]])
ctx.lineTo(pixel[0], pixel[1])
})
ctx.stroke()
}
}
render () {
return <CanvasOverlay redraw={this._redraw.bind(this)} />
}
} and then use it as follows // points is an array of [[lat, lon], [lat, lon], ...]
<ReactMapGL ...>
<PolylineOverlay points={points} />
</ReactMapGL> This works quite well for my application |
@GunnarSturla
But sadly, it didn't work in my application. (I mean the routing path doesn't appear in a map.)
I'm gonna work on Deck.gl's line layer or change to the Google Map. If there are the people well-informed of GeoJSON, please teach me how to show the line. ps: |
@YIPG: I don't think the object you're using is correct. I'm not really an expert in GeoJSON but I've found geojson.io to be very helpful in visualizing my code. I built the line you're trying to draw there and got this: {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"stroke": "#f06292",
"stroke-width": 15,
"stroke-opacity": 1,
"line-join": "round",
"line-cap": "round"
},
"geometry": {
"type": "LineString",
"coordinates": [
[-122.48369693756104, 37.83381888486939],
[-122.48348236083984, 37.83317489144141],
[-122.48339653015138, 37.83270036637107]
]
}
}
]
} Try seeing if that works, but did you try @secretwpn's overlay, it looks very promising? |
@GunnarSturla Thank you for your reply. I stuck with GeoJSON. I tried @secretwpn's polylineoverlay and it's totally working well! (And of course, your way is correctly working) |
Does any one has a working example which uses map box routes api and plot routes from the response on map ? |
Hey @DiwasSubedi , I'm not sure if this is what you're looking for, but here's an example of how I did it recently as a gist here. I didn't end up going with this library, but the concept is essentially the same. |
Just in case anyone is trying and it isn't working, the format of the array should have the longitude first, then the latitude. So
@secretwpn thanks for your help! |
Any updated solution? Can someone post a full example of draw simple polylines on the react-map-gl? |
@ariel262626 you could try using DeckGL and PathLayer: https://deck.gl/docs/api-reference/layers/path-layer I tried using the custom overlay and got it working, but it was performing too slow when I had a lot of routes, so I switched to DeckGL. This medium post was helpful: https://medium.com/@sashakayola/map-layering-using-mapbox-gl-js-deck-gl-react-ba0ece89aaef |
I think I'm too new at this stuff, but in the deck tutorial, they say "I specified an array of coordinates to form my path from point A to point B. Add in whatever data is appropriate for your particular overlay." The data I received from the Optimization API is not an array of coords - it delivers a mysterious "geometry" string. Anyone have experience with this? |
Yep, you can use the geometry as the data of the GeoJsonLayer:
And then pass it to your DeckGL layers prop. Check the docs here: https://deck.gl/docs/api-reference/layers/geojson-layer |
Thank you so much for responding! A human being! :) <3
Since writing, I have figured out how to get an assembled coordinate list
for my line (yay!). Can I pass that list to the data here?
I have two branches of my project with equally dysfunctional lines (=no
lines with hardcoded data).
https://github.com/AlfieFeldspar/optimizedTravel
I don't suppose I could buy someone a venmo coffee/consulting fee for a 30
min zoom to help get this organized? I am at end of all wits.
Essentially:
-I have a map displaying points (static locations from my database)
-I have a button for 'route generation' that hits the Mapbox Optimization
API, assembles the coordinates and adds them to a possibly appropriate
geojson object, and then I want them to load.
-nothing fancy - no animation or anything.
Gratitude for any further guidance.
…On Sun, Nov 1, 2020 at 3:30 AM ariel262626 ***@***.***> wrote:
I think I'm too new at this stuff, but in the deck tutorial, they say "I
specified an array of coordinates to form my path from point A to point B.
Add in whatever data is appropriate for your particular overlay." The data
I received from the Optimization API is not an array of coords - it
delivers a mysterious "geometry" string. Anyone have experience with this?
Yep, you can use the geometry as the data of the GeoJsonLayer:
const layer = new GeoJsonLayer({
id: 'geojson-layer',
data: getGeometry(),
pickable: true,
stroked: false,
filled: true,
extruded: true,
lineWidthScale: 20,
lineWidthMinPixels: 2,
getFillColor: [160, 160, 180, 200],
getLineColor: d => colorToRGBArray(d.properties.color),
getRadius: 100,
getLineWidth: 1,
getElevation: 30
});
And then pass it to your DeckGL layers prop.
Check the docs here:
https://deck.gl/docs/api-reference/layers/geojson-layer
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#591 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AP25TWPDXLFLWRW5FMU6WJLSNUMARANCNFSM4FUSXHYA>
.
|
I want to use Mapbox for one of my projects and was thinking of using this library to use it in react.
I want a feature where i can display routes with multiple stops along the routes. I see that mapbox has a directions API to get direction info and layers API to display the route for this purpose. I don't see any such thing in here.
Can we display routes using this library. If yes how? A small example would be really helpful.
The text was updated successfully, but these errors were encountered: