MapViewer and DrawRoute

Anyone know has an example code to draw a route on WebMapViever ?
The example project inside xojo shows add mapLocation only and not draw route
The documentation don’t expalin nothing abaut it
Ai generates bad code that refer to inexistent methods and proprierties
Does anyone uses mapViever ?

It looks like you use the Route method which returns a MapRoute object and call DrawRoute and pass in that MapRoute object.

1 Like

sorry but in documentation there is not examples to use Route/MapRoute or DrawRoute
i have try this code : (im using mapLibre)

MapViewer1.RemoveAllLocations

dim p1 as New WebMapLocation(43.7636, 11.2562)
dim p2 as New WebMapLocation(43.7633, 11.2560)    
dim p3 as New WebMapLocation(43.7629, 11.2558)       

p1.Visible=True
p2.Visible=true
p3.Visible=True

MapViewer1.AddLocation(p1)
MapViewer1.AddLocation(p2)
MapViewer1.AddLocation(p3)
MapViewer1.GoToLocation(p1,True)
MapViewer1.Zoom = 18

dim r as new MapRoute
r=MapViewer1.Route(MapRoute.TransportTypes.Car)
MapViewer1.RoutePenSize=3
MapViewer1.RouteDrawingColor=Color.Red

MapViewer1.DrawRoute(r)

but the result is this error

It looks like you’re doing it right, but getting a security error.

ERROR_WINHTTP_SECURE_FAILURE (12175)

Someone with more web knowledge will need to help from here.

1 Like

Um, @Ricardo_Cruz should getting a Route be making a HTTP request?

Yes, if you use the WebMapViewer.DrawRoute(transportType As MapRoute.TransportTypes = MapRoute.TransportTypes.Car) method signature, or the WebMapViewer.Route method, it needs to connect to a web service for getting routes between two or more waypoints. Same as WebMapLocation, it also needs to make HTTP requests for its LookupAddress.

If you already have a MapRoute (i.e. you’re caching the route in DB), then WebMapViewer.DrawRoute(route As MapRoute) won’t make any HTTP request.

At the moment, the provider host for the routing service cannot be configured, but it would be possible, same as we do with WebMapLocation.LookupProviderURL, where you configure it to point to a Nominatim service.

For routing, the required service is Project OSRM. Xojo uses a public one from OpenStreetMap at the moment. At the moment, if you can host the OSRM service and build the MapRoute properties on your own, you should be able to just call DrawRoute without relying on third party endpoints.

For the tiles service, which also uses OpenStreetMap by default, it’s possible to configure the WebMapViewer.StyleURL and host a Tile Service, like the Martin Vector Tiles Server.

2 Likes

Not sure why it’s throwing a security issue, it connects fine from macOS, but I’ve found an unrelated issue while testing this, when calling DrawRoute outside of the Opening event.

Issue #81563

A workaround is possible, I’ll post a comment there with the instructions.

Hi all. While we’re on topic, does anyone have a navigation option? Ie. Turn by turn directions? Obviously need realtime gps input.

Inside MapRoute, there is a GeoJSON (Wikipedia) property that we use for drawing the route on the map. There you’ll find a list of waypoints that (with some maths) you should be able to use for turn by turn directions, I guess. GeoJSON format is broad, we’re just using LineString so you can ignore everything else from the specifications. For example:

WebMapLocation.DistanceTo can also help finding the nearest waypoint.

For generating a MapRoute between two or more locations, add some to a WebMapViewer with AddLocation and then call the Route method.

Please make sure to credit OpenStreetMap folks for using their data :slight_smile: