I have a List with LatLng objects that form a route I want to draw on my map. I am using a SupportMapFragment and I call this method below on onActivityCreated. I have another method called from there that creates markers and that one is executed fine but my method below does not draw the polyline. I have searched for examples but could not find any that suit my needs. Can someone please point out what I'm doing wrong here?
private void drawRoute() {
List<LatLng> latLngs = CoordinateEntity.getRouteLatLngs();
PolylineOptions line = new PolylineOptions();
line.width(5);
line.color(Color.RED);
for (LatLng latLng : latLngs) {
line.add(latLng);
}
getMap().addPolyline(line);
}
I have looked at this and other similar examples and all follow this code pattern
mMap.addPolyline(new PolylineOptions()
.add(new LatLng(lats, lons), new LatLng(late,lone))
.width(5)
.color(color));
Is this the only way a Polyline can be added to the map?
Found my problem.
I had the play services jar added to my project and for some reason everything of the maps V2 api worked except drawing lines.
I then added the play services project as a dependency and after I did that everything worked.
Related
I want a Map UI like above dotted circle route path .I searched a lot through web and stack overflow i cant find any source.
I tried
addCircle polyline but the circle is large when i zoom the camera to
an extend in google map
I don't know how google map implemented it either with marker or
polyline
Provided help is appreciated rather than downvotes!!
thanks :-)
You can use Stroke Patterns for polylines like this:
#Override
public void onMapReady(GoogleMap googleMap) {
List<LatLng> sourcePoints = new ArrayList<>();
sourcePoints.add(new LatLng(22.724526, 75.825979));
sourcePoints.add(new LatLng(22.720165, 75.905953));
sourcePoints.add(new LatLng(22.766649, 76.075773));
sourcePoints.add(new LatLng(22.862032, 76.098302));
PolylineOptions polyLineOptions = new PolylineOptions();
polyLineOptions.addAll(sourcePoints);
polyLineOptions.width(50f);
polyLineOptions.color(Color.rgb(0, 178, 255));
Polyline polyline = googleMap.addPolyline(polyLineOptions);
List<PatternItem> pattern = Arrays.<PatternItem>asList(new Dot(), new Gap(10f));
polyline.setPattern(pattern);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(sourcePoints.get(2))
.zoom(14)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
to get result like that:
Unfortunately it's impossible to draw bigger circles under smaller to achieve "bordered" circles path because of nonlinear dependency between Dot() and Gap() sizes.
Or you can draw whatever you want on MapVew canvas via overriding dispatchDraw() of MapView like in this answer (it's about MapBox MapView, but approach is applicable for Google Maps MapView too).
I think its overkill, but you can use some of Google APIs from https://console.developers.google.com/apis/library
and get path and make from them set of LatLngs, then draw them on googleMap just like normal marker, but with small round circle as icon.
To be honest, I am not sure what you want to achieve, I think simpler idea is to use origin and destination and open it in GoogleMaps Android App - do not reinvent the wheel.
I work for a project about Google Maps and Traffic.
I want to draw route but ı want it to be colorful for my situation.
How can I draw a route colorful same line ?
Can anyone help me ?
Thanks a lot :)
If you are using PolylineOptions class to draw line then the following will help you:
PolylineOptions lineOptions = new PolylineOptions();
/* code for adding points ... */
lineOptions.color(Color.RED);
// You can also use following
// lineOptions.color(Color.parseColor("#ff00ff"));
I am getting the latitude and longitude on every onLocationChange and make a route like this:
public void onLocationChanged(Location location) {
PolylineOptions rectLine2;
rectLine2.add(new LatLng(lat, lon));
mGoogleMap.addPolyline(rectLine2);
}
Now I want to clear this route on one click, and again when I move, a fresh route should draw,
but in my case, old route doesn't clear.
I did the following but not working:
mGoogleMap.clear();// It clear's the map, but when I again draw the old route again comes.
Also
Polyline polyline;
polyline = mGoogleMap.addPolyline(rectLine2);
polyline.reomve()// This also didn't work.
Is there any other solution to clear the map?
Thanks In Advance!
mGoogleMap.clear()
is used to clear the whole map so that you can redraw the polylines...
else you need to assign a variable to polyline and you can remove it...
PolylineOptions rectLine2;
rectLine2.add(new LatLng(lat, lon));
Polyline polyline = mGoogleMap.addPolyline(rectLine2);
polyline.remove();
These are the ways... Why do you need any other solution to clear..?
If you have number of polylines then just create an array and remove all polylines once you need to clear
Hmm, that's intresting, try this way, it's working for me:
private void clearPath() {
if (polyline != null)
polyline.remove();
polyline = null;
}
mGoogleMap.clear() is the best way to clean the map. It removes all markers, polylines, polygons, overlays, etc from the map. see this http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html#clear()
So the problem is not in that. You are doing something wrong in reDrawing the new path. I think you have not given all code in your onLocationChanged function. And if this is your final code than this should not work as you are only adding one point in the PolylineOptions.
Please check this How to draw interactive Polyline on route google maps v2 android
Hope it will help
Try clearing your rectLine2 variable also. May be it's having all old latlangs. So on your click.
rectLine2 = new PolylineOptions();
Use map.clear() method to clear all map (polylines, markers, etc) or save all polylines somewhere and them remove them to save everything else
for(Polyline polyline : polylineList) {
polyline.remove();
}
I'm writing program in android google map v2 .
I created markers as ArrayList
Now, I want to find the street, between two points on the map how ???
Help me !!
Thanks
Use the Following Code:-
/************************** Drawing polyline if there are more than 1 latlngs **************/
if (arraylist_lat_lon.size() > 1) {
PolylineOptions polyline_options = new PolylineOptions()
.addAll(arraylist_lat_lon).color(Color.GREEN).width(2);
polyline = googleMap.addPolyline(polyline_options);
}
Note: arraylist_lat_lon : Array List where you are storing your LatLngs
I am developing an android application to trace the device movement.I am able to get the current location but how can i draw the route with set of points collected during trip
Define List for all points.
List< LatLng> points = new List< LatLng>();
//Add all points in list
points.add(new LatLng(lat1,lng1)); // POINT1
points.add(new LatLng(lat2,lng2)); // POINT2 and so on.
// then define PolylineOptions
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.addAll(points);
Polyline route = googleMap.addPolyline(polylineOptions);
// for marker use MarkerOptions
Use something more reliable than drawing a route: Markers.
Check the google maps Sample application from the Android SDK, and take a look on the MarkerDemoActivity.java class.
Open eclipse:
New Project->Android Sample Project->[YOUR_BUILD_TARGET]->maps [Google Play Service]
This will give you a good idea about what you're trying to search for.
Here's an example of what you'll get: