In a project I'm displaying a route using PolylineOptions for drawing rects from one POI to the next POI.
Also in each POI I'm drawing a circle.
The objective is to represent the direction of the route in some way, for example, drawing an arrow in the middle of each PolylineOptions rect. The problem is that I can't find how to do this.
This is my code:
PolylineOptions rectOptions = new PolylineOptions();
float[] prevHSV = new float[3];
Color.colorToHSV(getResources().getColor(R.color.colorPrimary), prevHSV);
rectOptions.color(Color.HSVToColor(255, prevHSV));
String[][] routeInformation = ((MyApplication)getApplication()).getLineInformation(line);
ArrayList<Double[]> routeStops = Util.getFullRouteFromLine(this, line);
final LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (int i=0; i<routeInformation.length; i++){
LatLng latlng = new LatLng(Double.parseDouble(routeInformation[i][0]),Double.parseDouble(routeInformation[i][1]));
builder.include(latlng);
mMap.addCircle(new CircleOptions().center(latlng).radius(15).strokeColor(Color.HSVToColor(255, prevHSV)).fillColor(Color.HSVToColor(255, prevHSV)).zIndex(7777));
}
for (Double[] pos : routeStops){
rectOptions.add(new LatLng(pos[0],pos[1])).width(5);
}
mMap.addPolyline(rectOptions);
Is there a easy way to represent the direction of a route?
Something similar to this, but this is for the web version of google maps, not for the android version: http://jsfiddle.net/nX8U8/2/
IMHO easiest way is to use flat "north-oriented" direction marker (arrow) like that:
created via vector drawable ic_arrow_up.xml:
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:viewportHeight="560"
android:viewportWidth="560"
android:height="24dp"
android:width="24dp"
>
<path android:fillColor="#0000FF"
android:pathData="M0,559.43C0,557.943 279.994,-0.561 280.458,0C282.014,1.884 560.512,559.569 559.999,559.776C559.665,559.911 496.562,532.823 419.77,499.581C419.77,499.581 280.15,439.14 280.15,439.14C280.15,439.14 140.756,499.57 140.756,499.57C64.089,532.807 1.056,560 0.681,560C0.307,560 0,559.743 0,559.43C0,559.43 0,559.43 0,559.43Z"
/>
</vector>
placed on each path polyline segment middle point with angle calculated on segment bearing. Both polyline segment middle and bearing you can determine via SphericalUtil class of Google Maps Android API Utility Library . Bearing can be found via SphericalUtil.computeHeading() with first and second points of segment as arguments and LatLng of segment middle point - via SphericalUtil.interpolate() also with first and second points of segment as arguments from and to and constant 0.5 (half) as fraction argument.
So, with source like this:
...
#Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
#Override
public void onMapLoaded() {
List<LatLng> sourcePoints = new ArrayList<>();
PolylineOptions polyLineOptions;
sourcePoints.add(new LatLng(-35.27801,149.12958));
sourcePoints.add(new LatLng(-35.28032,149.12907));
sourcePoints.add(new LatLng(-35.28099,149.12929));
sourcePoints.add(new LatLng(-35.28144,149.12984));
sourcePoints.add(new LatLng(-35.28194,149.13003));
sourcePoints.add(new LatLng(-35.28282,149.12956));
sourcePoints.add(new LatLng(-35.28302,149.12881));
sourcePoints.add(new LatLng(-35.28473,149.12836));
polyLineOptions = new PolylineOptions();
polyLineOptions.addAll(sourcePoints);
polyLineOptions.width(10);
polyLineOptions.color(Color.BLUE);
mGoogleMap.addPolyline(polyLineOptions);
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sourcePoints.get(0), 15));
for (int i = 0; i < sourcePoints.size() - 1; i++) {
// get first and second points of polyline segment
LatLng segmentP1 = sourcePoints.get(i);
LatLng segmentP2 = sourcePoints.get(i+1);
// calculate middle point
LatLng segmentMiddlePoint = SphericalUtil.interpolate(segmentP1, segmentP2, 0.5);
// calculate bearing
float segmentBearing = (float) SphericalUtil.computeHeading(segmentP1, segmentP2);
// add flat marker at segment middle
addDirectionMarker(segmentMiddlePoint, segmentBearing);
}
});
}
...
public void addDirectionMarker(LatLng latLng, float angle) {
Drawable circleDrawable = ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_arrow_up);
BitmapDescriptor markerIcon = getMarkerIconFromDrawable(circleDrawable, 30, 30);
mGoogleMap.addMarker(new MarkerOptions()
.position(latLng)
.anchor(0.5f, 0.5f)
.rotation(angle)
.flat(true)
.icon(markerIcon)
);
}
you'll got something like that:
Also you can change size of arrows markers by set other values than 30, 30 in
BitmapDescriptor markerIcon = getMarkerIconFromDrawable(circleDrawable, 30, 30);
line.
In my android app, I want to create a single path/polyline from a specific location(far end) to my current location while moving. I could draw the polyline on map with now issue while when I moves to a new location another polyline is been drown on the map, so my map output is full of polylines whichh I don't need, I want only one polyline to be visible to my current location.
PolylineOptions polylineOptions = new PolylineOptions().add(currentPosition).add(farEndPosition).width(10).color(Color.GREEN);
Polyline polyline = googleMap.addPolyline(polylineOptions);
Remove old Line before adding new one.
Polyline polyline ;
public void addUpdatePolyLine()
{
PolylineOptions polylineOptions = new PolylineOptions().add(currentPosition).add(farEndPosition).width(10).color(Color.GREEN);
if(polyline !=null)
{
polyline.remove();
}
polyline = googleMap.addPolyline(polylineOptions);
}
In onMapReady I am defining a few markers
First I declare the marker and his icon that is common for all markers and then each marker with it's own attributes. The problem is that when I declare the marker I cannot declare it as Marker beerMarker = new Marker() options as it makes me cast the Marker into MarkerOptions. What I would like is to call the info window for every marker but I cant as the beerMarker.showInfoWindow() is not acceptable for MarkerOptions. What am I doing wrong and what are the alternatives?
MarkerOptions beerMarker = new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.beer_marker));
//Carciuma
LatLng carciuma = new LatLng(43.604892, 1.476562);
mMap.addMarker(beerMarker.position(carciuma).title("Carciuma"));
//Boca
LatLng boca = new LatLng(43.604496, 1.474924);
mMap.addMarker(beerMarker.position(boca).title("Boca"));
//Bar Acasa
LatLng barAcasa = new LatLng(43.604781, 1.474502);
mMap.addMarker(beerMarker.position(barAcasa).title("Bar Acasa"));
Here is the updated version of the marker that meens I have t have to add for every individual marker the same icon, no?
LatLng barAcasa = new LatLng(43.604781, 1.474502);
Marker beerMarkerAcasa = mMap.addMarker(new MarkerOptions()
.position(barAcasa)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title("Bar Acasa"));
beerMarkerAcasa.showInfoWindow();
You can get this way Marker marker = mMap.addMarker(markeroption);
you can pass marker option for particulate marker in addMarker() method, they return marker instance which you can use.
Suppose i have 100 markers on the map what i want is when i apply GoogleMap.clear(); to clear the map it clear all the other markers on the map except 2 markers and 1 polyline between them a path.
say
marker1 = GoogleMap .addMarker(new MarkerOptions().position(latLng1).title(A));
marker2 = GoogleMap .addMarker(new MarkerOptions().position(latLng2).title(B));
line = GoogleMap.addPolyline(options1);
I don't want to clear these three. I want this so user don't have to experience a blink.
There is no way to clear everything except some things. However, you can keep a reference to any markers that you want to clear and loop over them.
ArrayList<Marker> markersToClear = new ArrayList<Marker>();
marker1 = GoogleMap.addMarker(new MarkerOptions().position(latLng1).title(A));
marker2 = GoogleMap.addMarker(new MarkerOptions().position(latLng2).title(B));
marker3 = GoogleMap.addMarker(new MarkerOptions().position(latLng3).title(C));
markersToClear.add(marker2);
markersToClear.add(marker3);
for (Marker marker : markersToClear) {
marker.remove();
}
markersToClear.clear();
// marker1 left on map
I connected map locations using Polyline API. It creates polyline properly.But sometimes unwanted polyline extended when zoom in and disappears on zoom out.
My code:
locations.clear();
for(int i=0;i<maplatitude.size();i++)
{
locations.add(new LatLng(maplatitude.get(i), maplongitude.get(i)));
}
PolylineOptions rectOptions = new PolylineOptions();
rectOptions.addAll(locations);
// Get back the mutable Polyline
Polyline polyline = mMap.addPolyline(rectOptions);
for(int i=0;i<maplatitude.size();i++)
{
//mMap.addMarker(new MarkerOptions().position(new LatLng(maplatitude.get(i), maplongitude.get(i))).title(name).snippet(mapdate.get(i)+"\n"+maptime.get(i)));
mMap.addMarker(new MarkerOptions().position(new LatLng(maplatitude.get(i), maplongitude.get(i))).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)).title(String.valueOf(i)));
}
I solved this: it's a bug from Google. We must filter some close points within 1 or 2 meters.
Polylines appearing on map where they shouldn't