I have an ArrayList<LatLng> that I need to submit to an API as an encoded Polyline. What I've used before is something like:
PolylineOptions pathOptions = new PolylineOptions();
Polyline polyline = googleMap.addPolyline(pathOptions);
polyline.setPoints(getRouteLatLngs());
except that in the case where I'm uploading, I don't have a GoogleMap to create the Polyline for me. Is it possible to create a Polyline without a GoogleMap or do I need to make another plan?
You don't want the Polyline object from GoogleMap. You need a tool to encode a list a points like PolyUtil, available in googlemaps utils library
PolyUtil.encode(latLngPath)
PolyUtil.decode(encodedPath)
Related
First, i'm sorry about my English.
I created the app for detecting walking and tracking step. Tracking step is good. But problem is that the app redetecting steps, Doesn't erase before polyline.
I try to map.clear(), polyline.remove()... etc. But Doesn't activate.
this is my source for draw polyline
polyline = map.addPolyline(polylineOptions.add(new LatLng(location.getLatitude(), location.getLongitude())));
polylineOptions.width(5);
polylineOptions.color(Color.RED);
map.addPolyline(polylineOptions);
this places onLocationListener in LocationListener.
location is onLocationListener's parameter.
How to remove previous polylines...?
Im sorry about my English. Please Help me.
To remove polyline.
//Adds the line to map
Polyline line = map.addPolyline(polylineOptions);
//removes the same line from map
line.remove();
As mentioned in the Shapes documentation specifically on Remove a Polyline, call the setMap() method passing null as the argument to remove a polyline from the map.
How to do it is also shown in Remove a Polyline sample.
I am a little bit confused since everything seems to be alright with the code.
The problem is that the polyline won't show up on the map.
Here is the function that I call to place polyline everytime I receive a location
(I added markers in a similar way and they work great)
private void addPolylineLocationOnMap(LatLng newLoc)
{
PolylineOptions poly = new PolylineOptions()
.add(newLoc)
.color(Color.BLUE)
.width(5)
.visible(true)
.zIndex(30);
googleMap.addPolyline(poly);
}
A Polyline needs multiple points!
For example, pass an ArrayList<LatLng> to your method and use addAll() rather than just add().
From the PolylineOptions documentation:
add(LatLng... points) : Adds vertices to the end of the polyline being built.
Alternatively, you can keep a reference to one Polyline and use add() to add points to it as you receive them.
Add poly as an instance variable in your class:
PolylineOptions poly;
Then in onCreate() (or wherever you set up the map):
poly = new PolylineOptions()
.color(Color.BLUE)
.width(5)
.visible(true)
.zIndex(30);
googleMap.addPolyline(poly);
Then as you receive more points:
poly.add(newLoc);
I added some Markers in my map and now I want to remove them. Is there any method that I can use to remove my Marker? I used MarkerOptions class to build my Markers and add() method of GoogleMap to add them. And I have a reference of all of my Markers in a List.
I really need your help.
After adding the marker it is possible to obtain its reference:
Marker marker = map.addMarker(..);
Marker class has remove method:
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Marker#remove
Removes this marker from the map. After a marker has been removed, the behavior of all its methods is undefined.
marker.remove()
It will remove all the markers. use this one to remove particular one
for(Marker m : marker)
m.remove();
I have added map on fragment activity and added several marker using addMarker function, but i am able to remove all markers , I am getting notification for different list of markers,
Now i wants to remove all markers and add new one.
one way to keep all markers in list and remove one by one, (marker.remove())
Is there any better way to clear all marker.
If you want to clear "all markers, overlays, and polylines from the map", use clear() on your GoogleMap.
If you do not wish to clear polylines and only the markers need to be removed follow the steps below.
First create a new Marker Array like below
List<Marker> AllMarkers = new ArrayList<Marker>();
Then when you add the marker on the google maps also add them to the Marker Array (its AllMarkers in this example)
for(int i=0;i<places.length();i++){
LatLng location = new LatLng(Lat,Long);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(location);
markerOptions.title("Your title");
Marker mLocationMarker = Map.addMarker(markerOptions); // add the marker to Map
AllMarkers.add(mLocationMarker); // add the marker to array
}
then finally call the below method to remove all markers at once
private void removeAllMarkers() {
for (Marker mLocationMarker: AllMarkers) {
mLocationMarker.remove();
}
AllMarkers.clear();
}
call from anywhere to remove all markers
removeAllMarkers();
I found this solution when i was looking for a way to remove only the map markers without clearing the polylines. Hope this will help you too.
I'm wondering what the best practice is to draw a dynamic route on a map with the Google Maps API v2. I want to have a map that's able to prolong the route while the user is moving. There seems to be the obvious solution by using a Polyline and PolylineOptions. But I just can't find an easy way to add points after I instantiated the Polyline. To draw a Polyline is something like this:
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.add(POINT1, POINT2, POINT3);
Polyline line = googleMap.addPolyline(polylineOptions);
But after I passed the line to GoogleMap I can't add any new points to it. Something like
polylineOptions.add(POINT1, POINT2, POINT3);
doesn't add anything to my route.
I could just add complete new Polyline. But isn't there a way to prolong just the existing one? I figured out a way by getting all the points of the Polyline, add the new point, and write them back to the line:
List<LatLng> points = line.getPoints();
points.add(POINT4);
line.setPoints(points);
But it seems to be cumbersome to me. Any ideas?
In the mainActivity class, define a private static LatLng variable named prev and initialize it to (0,0) first. Also make a flag variable and assign 0 to it. In the Listener's OnLocationChanged method, create a local variable LatLng named current and get current co-ordinates here... check the value of flag first, if it is 0 then assign current to prev. Then add a polyline.
Assign current to prev again (this will happen every time as after the first time, the flag will be 1)
For example:
public void onLocationChanged(Location location)
{
LatLng current = new LatLng(location.getLatitude(), location.getLongitude());
if(flag==0) //when the first update comes, we have no previous points,hence this
{
prev=current;
flag=1;
}
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(current, 16);
map.animateCamera(update);
map.addPolyline((new PolylineOptions())
.add(prev, current).width(6).color(Color.BLUE)
.visible(true));
prev=current;
current = null;
}
Something like this. Of course performance improvements can be made, this is just an example code. But it should work. Every time the polyline will add only the previous and current point, thereby extending it point by point.
Looking at the documentation, it appears that polylineOptions.add(LatLng) and googleMap.addPolyline(polylineOptions) methods return the polylineOptions object. The first method will also return polylineOptions WITH the point added to the end.
I think you'll have to add the polylineOptions to googleMap.addPolyline(polylineOptions) again or use googleMap.clear() before adding it a second time. Somehting like this:
polylineOptions = googleMap.addPolyline(polylineOptions);
// googleMap.clear();
googleMap.addPolyline(polylineOptions.add(point));