I need to get many points of simulated route. My goal is get all points to paint a route using shape with SKPolyline in another view. How can I get that?
Regards.
If i understand your goal you want to draw the route that skobbler calculate for you using SKPolyline.
After request route calculation the callback onRouteCalculationCompleted will call with SKRouteInfo.
To get the route positions try:
List<SKExtendedRoutePosition> list = SKRouteManager.getInstance().getExtendedRoutePointsForRoute(skRouteInfo.getRouteID());
Don't forget to set ExtendedPointsReturned=true when you request route calculation.
SKRouteSettings routeS = new SKRouteSettings();
routeS.setStartCoordinate(startPosition.getCoordinate());
routeS.setDestinationCoordinate(destinationPosition.getCoordinate());
// number of alternative routes specified here
routeS.setNoOfRoutes(1);
routeS.setRouteMode(SKRouteSettings.SKRouteMode.CAR_FASTEST);
//set true if you want the route is draw on map
routeS.setRouteExposed();
routeS.setExtendedPointsReturned(true);
SKRouteManager.getInstance().calculateRoute(routeS);
etzuk's answer is correct, but remember to cached the route before you can get all the skPosition of created route.
On onRouteCalculationCompleted, add saveRouteToCahce before getting route's coordinates like this:
SKRouteManager.getInstance().saveRouteToCache(skRouteInfo.getRouteID());
List<SKExtendedRoutePosition> coordinatesForRoute = SKRouteManager.getInstance()
.getExtendedRoutePointsForRoute(
skRouteInfo.getRouteID());
Related
I am using Here maps SDK in an android project.
I want to calculate track here and put it into an android map to route exactly this way (calculated on API)
I tried to get points from Json and map them to waypoints, but the calculated way sometimes differs from the original.
What I have tried so far:
1. Get calculateroute.json from
https://route.api.here.com/routing/7.2/calculateroute.json
2. Put coordinates to RoutePlan
RoutePlan plan = new RoutePlan();
plan.addWaypoint(new RouteWaypoint(new GeoCoordinate(54.3782893, 18.7009037, 0), RouteWaypoint.Type.STOP_WAYPOINT));
plan.addWaypoint(new RouteWaypoint(new GeoCoordinate(54.2660919, 18.6583687, 0), RouteWaypoint.Type.STOP_WAYPOINT));
3. Calculate route and put to a map
new CoreRouter().calculateRoute(plan, new CoreRouter.Listener() {
#Override
public void onCalculateRouteFinished(List<RouteResult> list, RoutingError error) {
MapRoute mapRoute = new MapRoute(list.get(0).getRoute()
new Map().addMapObject(mapRoute)
}
}
I want to do :
1. Get calculateroute.json from
https://route.api.here.com/routing/7.2/calculateroute.json
2. Create MapRoute from the calculateroute.json result ( but don't calculate it )
3. Put MapRoute to map and start navigation
Basically there is no direct way to achieve this, a workaround solution is as described in the thread : Backend Calculated Route,Here-API to just show Guidance and route on Map
Is there a way to color only a part of the route in a different color?
Like drawing a polyline but one that takes the road configuration into consideration.
No, that is not supported. But if you are wondering about changing only the traveled portion color I previously answered here.
You can extract the route geometry to get the list of GeoCoordinates constituing the polyline:
geoCoordinates = mapRoute.getRoute().getRouteGeometry()
Then you can draw portions of the route in various colors by building new polylines from these coordinates:
GeoPolyline geoPolyline = new GeoPolyline();
for (GeoCoordinates point : geoCoordinates) {
if (<your condition>){
geoPolyline.add(point);
}
}
MapPolyline mapPolyline = new MapPolyline(geoPolyline);
mapPolyline.setLineColor(<color>);
map.addMapObject(mapPolyline)
I have a geojson with some polygons, added it as a source and created a layer to show those features on the mapbox map.
Since I am using this method: (Edit from the sample souce: MapboxGLAndroidSDKTestApp)
final PointF pixel = mapboxMap.getProjection().toScreenLocation(point);
List<Feature> fList = mapboxMap.queryRenderedFeatures(pixel, "polygons");
for(Feature feature:fList){
Log.d(TAG,"FeatureInfo: "+feature.toJson());
}
I can check if the marker is inside one of those polygons by passing the marker position to it.
But when I move my camera far away to the marker, the polygon is not rendered and the checker is not work anymore.
So my question is, is there any way to check if the marker is inside the polygon, which no matter where is my camera pointing at?
To check a point whether lies or not inside a polygon on mapbox
can use turf.js library http://turfjs.org/
This is the way in which you don't need to plot point or polygon on the mapbox(without plotting).
var point = turf.point([-75.343, 39.984]);
// here first is lng and then lat
var polygon = turf.polygon([[
[-2.275543, 53.464547],
[-2.275543, 53.489271],
[-2.215118, 53.489271],
[-2.215118, 53.464547],
[-2.275543, 53.464547]
]], { name: 'poly1'});
// here first is lng and then lat
now you can use inside method from turf lib
i.e turf.inside(point, polygon); // return boolean True/False
Use TurfJoins.inside(yourPoint, yourPolygon)
I have an example route that I've tested using the Skobbler SDK.
Start Point: around 820 S MacArthur Blvd
Irving, TX 75063
End Point: Bass Performance Hall, Fort Worth, TX
Via Point #1: Shell Gas Station, 1224 Oakland Blvd Fort Worth, TX
The code that's used to calculate the route is as follows:
SKRouteSettings route = new SKRouteSettings();
// set start and destination points
route.setStartCoordinate(start);
route.setDestinationCoordinate(end);
route.setNoOfRoutes(1);
// set the route mode
route.setRouteMode(SKRouteSettings.SKRouteMode.CAR_FASTEST);
// Traffic enabled
route.setUseLiveTraffic(true);
route.setUseLiveTrafficETA(true);
route.setTollRoadsAvoided(true);
route.setAvoidFerries(true);
route.setHighWaysAvoided(true);
ArrayList<SKViaPoint> viaPoints = new ArrayList<SKViaPoint>();
viaPoints.add(new SKViaPoint(VIA_POINT_ID_OTW_DEST, viaPoint));
route.setViaPoints(viaPoints);
// set whether the route should be shown on the map after it's computed
route.setRouteExposed(true);
// Set traffic routing mode
SKRouteManager.getInstance().setTrafficRoutingMode(SKMapSettings.SKTrafficMode.FLOW_AND_INCIDENTS);
// set the route listener to be notified of route calculation
// events
SKRouteManager.getInstance().setRouteListener(this);
// pass the route to the calculation routine
SKRouteManager.getInstance().calculateRoute(route);
When I compute this route via the Skobbler SDK and display the calculated route on the SKMapView, I sometimes get a route that doesn't make sense.
This route calculated on one of our test devices shows the calculated route taking us off the freeway, turning right onto a street and then making a u-turn at the end of the street, before turning right again into the Shell gas station. For some reason, the routing algorithm is not realizing that the route can go straight off the freeway and then turn right, or alternatively, turn right at the street off the freeway and then make an immediate left.
This is not the only instance of incorrect routing that we have witnessed, but it is the most recent. In the past, we have been able to make slight modifications to the coordinate passed for the via point to make the routing algorithm return a sensible route, and then were able to go back to recalculating the original route correctly without the long detours.
In this case, in order to view the corrected route below, we simply try routing the same route again some time later:
Has anyone experienced an issue similar to this and is there something that can be done on the client side to prevent it? Or perhaps a request setting that can be made while requesting a route calculation to the server that might help?
Thanks for any feedback on the matter!
-Keith
I would like to click a spot on a Google maps v2 android map. If the clicked point intersects a point on a polyline path, then display the polyline. I do not see any documented clickable events for polylines in android. I tried to extend the current Polyline object (marked final)
What other options do I have?
You can use library:
https://github.com/googlemaps/android-maps-utils
And detect clicks to polyline using next method (in OnMapClickListener):
PolyUtil.isLocationOnPath(point, polyline.getPoints(), isGeodesic, tolerance);
With the recent update of the maps api, v8.4, introduces clickable Polyline
As mentioned in the doc:
Use the OnPolylineClickListener to listen to click events on a
clickable polyline. To set this listener on the map, call
googleMap.setOnPolylineClickListener(...). When
a user clicks on a polyline, you will receive an
onPolylineClick(Polyline) callback.
gradle-dependency:
compile 'com.google.android.gms:play-services-maps:8.4.0'
implement callback: GoogleMap.OnPolylineClickListener
initialize Polyline:
Polyline polyline = googleMap.addPolyline(options);
polyline.setClickable(true);
...
receive events
#Override
public void onPolylineClick(Polyline polyline) {
....
}
Happy coding :)
Register an OnMapClickListener. Determine if a given click is on your line yourself. If it is, do whatever it was you wanted to do in this case.
I had a similar issue where I could not process click events on polylines. I was using Xamarin for Android which is C# but the functionality is largely the same as the Android Java Libraries in this case.
In the end, I ended up doing what seemed to be the only option.
This involved processing all of the midpoints of my polylines(of which there were around 1300). On every OnMapClick, I took the LatLng of the click event and performed a distance formula between it and the midpoint of all polylines in the static List<PolylineOptions>. I then attached a map marker to the closest polyline.
From a tap on a polyline, it pops up a marker in about a quarter of a second.
I imagine the implemented marker click events from the Google Maps API work in a similar way.
Here is the for loop that handles finding the closest point to a click.
int i = 0;//create an indexer for the loop
double shortestDist = 100;//set an initial very large dist just to be safe
int myIndex = 0;//set variable that will store the running index of the closest point
foreach (PolylineOptions po in myPolylines) {
var thisDist = Distance (point, midPoint (po.Points [0].Latitude, po.Points [0].Longitude, po.Points [1].Latitude, po.Points [1].Longitude));//calculate distance between point and midpoint of polyline
if (thisDist < shortestDist) {
shortestDist = thisDist;//remember current shortest distance
myIndex = i;//set closest polyline index to current loop iteration
}
i++;
}
I know it isn't the prettiest code but it gets the job done. I didn't see a real answer to this anywhere on the internet so here it is. It could probably be made more efficient by calculating the midpoints beforehand and storing them in an equally sized list and then not having to call the midpoint formula for each polyline on every map click but it works really fast already.
EDIT
I do my testing on a galaxy s3 by the way, so I think it's not too inefficient.
If you are using com.google.android.gms:play-services-maps:8.4.0 then it includes polylines click listener
googleMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener()
{
#Override
public void onPolylineClick(Polyline polyline)
{
//do your work selected polyline
}
});
PolylineOptions line = new PolylineOptions();
Polyline polyline = googleMap.addPolyline(line);
polyline.setClickable(true);