I have calculated route for 10 waypoints. If i simply create new MapRoute it will draw full route from first to last waypoint. Is it possible to draw part of route, from first to second waypoint?
I use 3.4.0.165 HERE SDK version. I have found this answer, but it for previous version.
I have tried to draw only first subleg in this way:
int duration = route.getTta(Route.TrafficPenaltyMode.AVOID_LONG_TERM_CLOSURES, 0).getDuration();
RouteElements routeElementsFromDuration = route.getRouteElementsFromDuration(0, duration);
MapPolyline mapPolyline = new MapPolyline(routeElementsFromDuration.getGeometry());
map.addMapObject(mapPolyline);
But when map tilt enabled polyline drawn somewhere above roads, and if i change scale polyline change position relative to map tile objects.
Is there a reason you can't create a new route with your two waypoints? If you're using a stopover waypoint, you will be required to pass through the waypoint no matter what. Thus calculating the route between the first and second waypoint will provide you with the same route as the subset of the full route. If you're using a via waypoint, you can't count on drawing a subset of the route since it could change any minute given you are allowed to deviate away from the next waypoint at which point it will be ignored.
Currently it is not possible, workaround - calculate route between each point and draw required part.
Related
How to make MapBox SDK(Android) so that when the user moves around the map, a line is drawn behind him in real time (his movement itself)
https://docs.mapbox.com/help/tutorials/android-location-listening/ shows how to track a user's location.
Every time a new Location arrives in onSuccess(), you'll:
create a Point (Point point = Point.fromLngLat(LONGITUDE, LATITUDE);) with the Location object's coordinates.
Add the Point to a list of Points to create a LineString.
LineString lineString = LineString.fromLngLats(pointList);
Use the LineString to update the LineLayer's geojson with the new LineString (see the second code snippet box in https://docs.mapbox.com/android/java/overview/geojson/#geojson-updates.
All of this is basically a combination of https://docs.mapbox.com/help/tutorials/android-location-listening and https://docs.mapbox.com/android/maps/examples/moving-icon-with-trailing-line (without the animation or PointEvaluator stuff from the second example, because you don't need it to move the device location puck).
This question already has an answer here:
Google Maps API and custom polyline route between Markers
(1 answer)
Closed 5 years ago.
Basically, what I simply need to achieve is, first draw the route between two points, from current location to any other point on the map. And when a third point is place, the route must change from current to third point and finally from third point to final point.
For reference,
Point A - Current/Start Location
Point B - End Location
Point C - Third Location
Tasks
1) Draw point from A to B
2) If C point is placed, draw from A to C and C to B.
How can I implement this ?
Thank you
you can use way points. When calculating routes using the Google Maps Directions API, you may also specify waypoints for driving, walking or bicycling directions. Waypoints are not available for transit directions. You can use waypoints to calculate routes through additional locations, in which case the returned route includes stopovers at each of the given waypoints.
For more details visit here
Sample url will be like follows
https://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|Lexington,MA&key=YOUR_API_KEY
This should work.
private void displayDirection(List<LatLng> poly){
PolylineOptions polylineOptions= new PolylineOptions();
polylineOptions.color(Color.RED);
for(int i=0; i<poly.size(); i++){
polylineOptions.width(8);
polylineOptions.add(poly.get(i));
}
mMap.addPolyline(polylineOptions);
}
My app gets updated GPS coordinates periodically which I show using a Marker on map. I need to move the marker to a new position if the new GPS coordinates are different then what Marker is currently showing.
The problem is that comparing Marker.getPosition() is more accurate while LatLang is not, hence sometimes even when they are the same, my logic says they are different.
How to solve this issue?
Please note that the same LatLang i assign to Marker.
You can consider that two LatLngs are virtually the same if the distance between them is less than a given tolerance.
You can use the SphericalUtil.computeDistanceBetween method from the Google Maps Android API Utility Library
float YOUR_TOLERANCE = 1; // 1 meter
if (SphericalUtil.computeDistanceBetween(pos, buslatLng) < YOUR_TOLERANCE) {
// Both locations are considered the same
}
For example I have:
start (51.533407, -0.203948)
point (51.531966, -0.210987)
end (51.530951, -0.216823)
origin (51.533487, -0.187812)
destination (51.529189, -0.220814)
So, how can I know that "point" is between "start" and "end" and not between "origin" and "start" or "end" and "destination?
I already tried:
minimum distance between coordinates - is not a solution, because can give wrong results;
comparing street name (feature name, thoroughfare, subthoroughfare - Address) with the "html_instruction" from Directions API - is not working everytime and sometimes the street name is null;
comparing street name (feature name, thoroughfare, subthoroughfare - Address) with "long_name" ("address_components") from Geocode API -> useless, because i can't get coordinates from "start" and "end" and sometimes the street name is null (eg: on villages).
Any other ideas? I already searched on the internet but i couldn't find something that I need.
Thank you,
Maxim
Image Link: google map
This is just an analytically solution, its upto you how you can implement with the code (I would recommend using Geofencing API provided in Android's Google Play Services library).
Consider the "point" to be the center of circle.
Find the close point, For Example "Start".
Consider that distance as radius and draw a circle.
You may now know all the coodinates (points) inside the circle.
Find distances from the center to all the points included in the Geofence (Circle).
Subtract each with the "point's" distance.
The minimum distance obtained after subtraction would be the point opposite to the "point".
Hence you can know that in between which two coodinates your "point" lies.
If there are no coordintes in the geofence in the radius drawn between "point" & "start" then increase the radius till the time you dont find the first coordinate. This will be another point besides "point" & "Start".
Hope this would Help!!!
I need to fill the center of a polygon in Android maps v2 and am running into problems when the polygon is complex and has lines that cross each other. The user is able to draw on the map with their finger and then I use a map projection to convert my points to LatLng.
I need to fill the center even if it is drawn with lines that cross.
My code to draw is as follows:
PolygonOptions rectOptions = new PolygonOptions();
rectOptions.strokeColor(getResources().getColor(R.color.blue));
rectOptions.fillColor(getResources().getColor(R.color.blue_map_fill));
rectOptions.strokeWidth(4);
rectOptions.addAll(latLngs);
mMap.addPolygon(rectOptions);
Screenshot when I draw a star with the lines crossing:
Screenshot when I draw the star with only the outline:
Is there a way to calculate what LatLngs make up the outline or is there a different solution?
EDIT: The iOS version of the app I'm working on handles this perfectly..they just add all the points to a polygon and Google Maps figures it out. At this point I believe this is a bug/feature that is lacking from Android Google Maps.
EDIT: Bug report: https://code.google.com/p/gmaps-api-issues/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Introduced%20Fixed%20Summary%20Stars%20ApiType%20Internal&groupby=&sort=&id=6255
I know it's already a year gone but here is my solution.
I use the JTS library, I think you can use a client of this library as well, to create the surrounding polygon.
This method creates a new list of LatLng objects which you can use to draw the surrounding polygon of your input.
private void createSurroundingPolygon(List<LatLng> polygonPath) {
List<Coordinate> coordinates = new ArrayList<>();
for (LatLng latLng : polygonPath) {
coordinates.add(new Coordinate(latLng.longitude, latLng.latitude));
}
GeometryFactory factory = new GeometryFactory();
Geometry lineString = factory.createLineString(coordinates.toArray(new Coordinate[coordinates.size()]));
Polygon polygon = (Polygon) BufferOp.bufferOp(lineString, 0.0001);
Coordinate[] coordinatesSurroundingPolygon = polygon.getExteriorRing().getCoordinates();
List<LatLng> surroundingPolygon = new ArrayList<>();
for (int i = 0; i < coordinatesSurroundingPolygon.length; i++) {
surroundingPolygon.add(new LatLng(coordinatesSurroundingPolygon[i].y, coordinatesSurroundingPolygon[i].x));
}
drawPolygon(surroundingPolygon);
}
First it creates a new list of Coordindates. They are used to create a JTS Geometry, i.e. a LineString. You can't create a LinearRing or a Polygon directly from your coordinates list because you don't know if it is a valid (closed polygon without intersections) one. You get a Polygon when you buffer the given Geometry with a distance, in my case 0.0001. The distance is additional space which is added outside to your original polygon.
Finally with the method Polygon.getExtgeriorRing() you get the outline polygon without any intersections and crossing lines.
One possible way would be to detect where the lines collide with each other then convert those pixel positions to lat/lng points.
you would need to keep track of the first and last point which is the same in your case then check each line with all the other lines in the polygon. Also keep track of the order you find them in based on the direction you start checking (ie. clockwise/counter-clockwise). Convert all the intersection points from x,y to lat/lng.
after you found all the intersection positions then you can start at the first point and create a line the the first intersection point then the next line would be the first intersection point to your next non-intersection point(ie. the tips of the star) etc. etc. until you form a new list of all the new points then give the map that list to plot.
you can use this formula to get started http://en.wikipedia.org/wiki/Line-line_intersection
there may be some holes in my logic that i didnt think of.