How to draw route between multiple LatLong Android map? - android

Am working on an Android project where I want to draw route between 2 point on Google Map. I have successfully drawn route between source and destination point. But I have 1 problem in that, i.e. Some times I want to draw a path between more than 2 point, That time the code that I have written is drawing route between first and the last position and leaving the mid point position. What I exactly want is my route should go through the mid point to the destination point. How can I achieve this?

You can separately draw routes between three point. So, first draw the route from starting point to mid point then from mid point to end point. Then if you want add the required markers.

You can use PolylineOptions from GoogleMaps API Dcoumented Here and keep adding all the points which should be a part of your route. You can do something like this
ArrayList<LatLng> points;
PolylineOptions lineOptions = null;
// Traversing through all the routes
for (int i = 0; i < result.size(); i++) {
points = new ArrayList<>();
lineOptions = new PolylineOptions();
// Fetching i-th route
List<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th route
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
lineOptions.width(10);
lineOptions.color(Color.RED);
}
// Drawing polyline in the Google Map for the i-th route
if(lineOptions != null) {
mMap.addPolyline(lineOptions);
}
Hope this solves your problem.

Related

Markers in android google maps json

I have a hicking app, that gets a array of markers from my database and plots them on my map, afterward it draws a polyline connecting all the markers. What i want to do is , to draw the polyline connecting the markers, but instead of showing 10 or 20 or 100 markers, to only SHOW 2, the marker that starts and the one that ends.
Would appreciate any help.
Below is the function that i use to draw the polylines.
private void createPolylinesFromLatLng(ArrayList<LatLng> polylist){
PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int z = 0; z < polylist.size(); z++) {
LatLng point = polylist.get(z);
options.add(point);
totalDistanceInMeters = SphericalUtil.computeLength(polylist);
test=String.format("%.2f",totalDistanceInMeters);
}
and this is how i retrieve my markers into a array
JSONArray array = new JSONArray(response);
for (int i = 0; i < array.length(); i++) {
JSONObject jo = array.getJSONObject(i);
Double lat = Double.parseDouble(jo.getString("lat"));
Double lng = Double.parseDouble(jo.getString("lon"));
polyline = new LatLng(Double.parseDouble(jo.getString("lat")),
Double.parseDouble(jo.getString("lon")));
polylist.add(polyline);
// location = new LatLng(lat,lng);
location=polyline;
MarkerOptions options = new MarkerOptions();
options.position(location);
mMap.addMarker(options);
thank you
One way of doing this would to just surround your code where you add the Marker with a if statement like this:
if(i == 0 || i == (array.length() - 1)){
MarkerOptions options = new MarkerOptions();
options.position(location);
mMap.addMarker(options);
}
That way only the first location and the last location are added.

Populate map with Points of Interest markers around route

I draw routes on my google map using next code:
private void drawRoute(List<Route> route) {
for (int i = route.size() - 1; i >= 0 ; i--) {
final Route currentRoute = route.get(i);
final PolylineOptions polyOptions = new PolylineOptions();
polyOptions.color(Color.parseColor(allColors[inc_color]));
polyOptions.width(10);
polyOptions.addAll(currentRoute.getPoints());
mMap.addPolyline(polyOptions);
}
}
After this step I want to get from my SQLite database all PoI that are at max 5 km far from my routes and to draw them on map.
How can I filter my database as result to show all locations that are located at max 5 km along the route?
Maybe getting the bounds of route?

Android Google Map Alternate Route and Shortest Path

I have searched all over and I haven't found an answer. Similar to this
How can I change the color to the alternate route? urlDestination +"&alternatives=true"
Adding that code will display the shortest route and alternate routes. Problem is, I don't know how to change the color of the alternate route to a specific color.
Example: Shortest route should be blue and alternate routes should be grey.
Help is much needed.
Something like this... that the alternate routes should be grey
Check this Tutorial
You Need to add BelowLine.
polyLineOptions.color(Color.BLUE);
Hope this may help.
It's pretty straight forward. I believe you have a list or array of Routes.
Iterate through that to find the min distance index. (usually it's the first one, but just to make sure).
int minDistanceIndex = 0;
int minDistance = Integer.MAX_VALUE;
for(int i = 0; i < routes.size(); i++){
Route route = routes.get(i);
int distance = route.getDistanceValue();
if(distance < minDistance){
minDistance = distance;
minDistanceIndex = i;
}
}
Now use the minDistanceIndex to show the default route(blue) and others as gray like below.
PolylineOptions lineOptions;
for (int i = 0; i < routes.size(); i++) {
points = routes.get(i).getPoints();
lineOptions = new PolylineOptions();
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
if(minDistanceIndex != i) {
lineOptions.width(15);
lineOptions.color(ContextCompat.getColor(getActivity(), android.R.color.darker_gray));
}
lineOptions.clickable(true);
// Drawing polyline in the Google Map for the i-th route
if(map != null) {
polylines.add(map.addPolyline(lineOptions));
map.setOnPolylineClickListener(polylineListener);
}
}
//finally draw the shortest route
lineOptions = new PolylineOptions();
lineOptions.width(18);
lineOptions.color(ContextCompat.getColor(getActivity(), android.R.color.holo_blue_dark));
// Drawing polyline in the Google Map for the i-th route
if(map != null) {
polylines.add(map.addPolyline(lineOptions));
}
You can add an on click listener to polylines to change the color when user selects another polyline just like in Google Maps.
IMP: You need to draw the shortest route at last cause otherwise part of that route can be overlapped by alternate route, hence leaving you with part blue and part gray route.
Hope this helps!

Android google maps getting rid of polyline

I am building an android application that gets the users current position and find nearby attractions. When I select an attraction a route is drawn to it from the current position but when I do this a second time the first route stays there, I want it to disappear. Below is the code I use to draw the line. Each time a direction is drawn this is called. I have tried to use line.remove each time before method is called but this removes both lines then. Any suggestions?
for (int i = 0; i < pontos.size() - 1; i++) {
LatLng src = pontos.get(i);
LatLng dest = pontos.get(i + 1);
try{
//here is where it will draw the polyline in your map
line = mMap.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude, dest.longitude))
.width(2).color(Color.RED).geodesic(true));
Save your Polylines in an array so you can remove them before other ones are added:
List<Polyline> mPolylines = new ArrayList<>();
private void someMethod() {
// Remove polylines from map
for (Polyline polyline : mPolylines) {
polyline.remove();
}
// Clear polyline array
mPolylines.clear();
for (int i = 0; i < pontos.size() - 1; i++) {
LatLng src = pontos.get(i);
LatLng dest = pontos.get(i + 1);
mPolylines.add(mMap.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude, dest.longitude))
.width(2).color(Color.RED).geodesic(true)));
}
}

Center android google map on group of markers?

I have a bunch of markers in which I load onto a google map. My problem is that, the camera is not ontop of the markers. Can I center it on top of all my markers?
I am adding my markers with this code:
for(int i = 0; i < jsonArray.length(); i++) {
String breweryName = jsonArray.getJSONObject(i).getString("brewery");
String lat = jsonArray.getJSONObject(i).getString("lat");
String lng = jsonArray.getJSONObject(i).getString("lng");
String bID = jsonArray.getJSONObject(i).getString("breweryID");
double latD = Double.parseDouble(lat);
double lngD = Double.parseDouble(lng);
m.addMarker(new MarkerOptions()
.position(new LatLng(latD, lngD))
.title(breweryName));
}
I looked on stack and found this question, which is the same as mine:
Android Google maps API V2 center markers
But there isnt much context behind it and am not sure how to implement it.
You can animate your camera within specific bounds. Let's say you have an ArrayList of markers, something like (this could also be a list of LatLngs, doubles, anything that contains the latitude/longitude for your marker)
List<MarkerOptions> markers = new ArrayList<MarkerOptions>();
You can then make sure they are all visible on your map by doing the following
LatLngBounds.Builder builder = new LatLngBounds.Builder();
LatLng position;
for(int i = 0; i < markers.size(); i++){
position = markers.get(i).getPosition();
builder.include(new LatLng(position.latitude, position.longitude));
}
LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 15));

Categories

Resources