下面列出了怎么用com.google.android.gms.maps.model.Polyline的API类实例代码及写法,或者点击链接到github查看源代码。
public void clearMarker() {
if (this.polylineList != null) {
for (Polyline polyline : this.polylineList) {
polyline.remove();
}
}
if (this.limitCircle != null) {
this.limitCircle.remove();
this.limitCircle = null;
}
this.polylineList.clear();
this.mMarkerList.clear();
this.mMapPointList.clear();
this.interestMarkerList.clear();
this.arrowMarkerList.clear();
this.mSelectMarker = null;
}
/**
* Add a list of Polylines to the map
*
* @param map google map
* @param polylines multi polyline options
* @return multi polyline
*/
public static MultiPolyline addPolylinesToMap(GoogleMap map,
MultiPolylineOptions polylines) {
MultiPolyline multiPolyline = new MultiPolyline();
for (PolylineOptions polylineOption : polylines.getPolylineOptions()) {
if (polylines.getOptions() != null) {
polylineOption.color(polylines.getOptions().getColor());
polylineOption.geodesic(polylines.getOptions().isGeodesic());
polylineOption.visible(polylines.getOptions().isVisible());
polylineOption.zIndex(polylines.getOptions().getZIndex());
polylineOption.width(polylines.getOptions().getWidth());
}
Polyline polyline = addPolylineToMap(map, polylineOption);
multiPolyline.add(polyline);
}
return multiPolyline;
}
/**
* Add a Polyline to the map as markers
*
* @param map google map
* @param polylineOptions polyline options
* @param polylineMarkerOptions polyline marker options
* @param globalPolylineOptions global polyline options
* @return polyline markers
*/
public PolylineMarkers addPolylineToMapAsMarkers(GoogleMap map,
PolylineOptions polylineOptions,
MarkerOptions polylineMarkerOptions,
PolylineOptions globalPolylineOptions) {
PolylineMarkers polylineMarkers = new PolylineMarkers(this);
if (globalPolylineOptions != null) {
polylineOptions.color(globalPolylineOptions.getColor());
polylineOptions.geodesic(globalPolylineOptions.isGeodesic());
polylineOptions.visible(globalPolylineOptions.isVisible());
polylineOptions.zIndex(globalPolylineOptions.getZIndex());
polylineOptions.width(globalPolylineOptions.getWidth());
}
Polyline polyline = addPolylineToMap(map, polylineOptions);
polylineMarkers.setPolyline(polyline);
List<Marker> markers = addPointsToMapAsMarkers(map,
polylineOptions.getPoints(), polylineMarkerOptions, false);
polylineMarkers.setMarkers(markers);
return polylineMarkers;
}
@Override
public void onRoutingSuccess(ArrayList<Route> route, int shortestRouteIndex) {
if(polylines.size()>0) {
for (Polyline poly : polylines) {
poly.remove();
}
}
polylines = new ArrayList<>();
//add route(s) to the map.
for (int i = 0; i <route.size(); i++) {
//In case of more than 5 alternative routes
int colorIndex = i % COLORS.length;
PolylineOptions polyOptions = new PolylineOptions();
polyOptions.color(getResources().getColor(COLORS[colorIndex]));
polyOptions.width(10 + i * 3);
polyOptions.addAll(route.get(i).getPoints());
Polyline polyline = mMap.addPolyline(polyOptions);
polylines.add(polyline);
Toast.makeText(getApplicationContext(),"Route "+ (i+1) +": distance - "+ route.get(i).getDistanceValue()+": duration - "+ route.get(i).getDurationValue(),Toast.LENGTH_SHORT).show();
}
}
private Polyline drawPath(List<LatLong> latLongs, int color, float zIndex, Polyline polyline) {
if (googleMap != null && latLongs != null && !latLongs.isEmpty()) {
PolylineOptions polyLineOptions = new PolylineOptions();
polyLineOptions.width(getResources().getDimension(R.dimen._2dp));
polyLineOptions.color(color);
polyLineOptions.zIndex(zIndex);
for (LatLong latLong : latLongs) {
polyLineOptions.add(new LatLng(latLong.latitude(), latLong.longitude()));
}
if (polyline != null) polyline.remove();
return googleMap.addPolyline(polyLineOptions);
}
return null;
}
@Override
public void onRoutingSuccess(ArrayList<Route> route, int j) {
if (polylines.size() > 0) {
for (Polyline poly : polylines) {
poly.remove();
}
}
polylines = new ArrayList<>();
//add route(s) to the map.
for (int i = 0; i < route.size(); i++) {
//In case of more than 5 alternative routes
int colorIndex = i % COLORS.length;
PolylineOptions polyOptions = new PolylineOptions();
polyOptions.color(getResources().getColor(COLORS[colorIndex]));
polyOptions.width(10 + i * 3);
polyOptions.addAll(route.get(i).getPoints());
Polyline polyline = mMap.addPolyline(polyOptions);
polylines.add(polyline);
}
}
/**
* Updates the track, start and end markers, and waypoints.
*
* @param googleMap the google map
* @param paths the paths
* @param tripStatistics the trip statistics
* @param reload true to reload all points
* @return true if has the start marker
*/
public boolean update(GoogleMap googleMap, ArrayList<Polyline> paths,
TripStatistics tripStatistics, boolean reload) {
synchronized (locations) {
boolean hasStartMarker = false;
// Merge pendingLocations with locations
int newLocations = pendingLocations.drainTo(locations);
// Call updateState first because we want to update its state each time
// (for dynamic coloring)
if (trackPath.updateState(tripStatistics) || reload) {
googleMap.clear();
paths.clear();
trackPath.updatePath(googleMap, paths, 0, locations);
hasStartMarker = updateStartAndEndMarkers(googleMap);
updateWaypoints(googleMap);
} else {
if (newLocations != 0) {
int numLocations = locations.size();
trackPath.updatePath(googleMap, paths, numLocations - newLocations, locations);
}
}
return hasStartMarker;
}
}
/**
* Add a path.
*
* @param googleMap the google map
* @param paths the existing paths
* @param points the path points
* @param color the path color
* @param append true to append to the last path
*/
public static void addPath(GoogleMap googleMap, ArrayList<Polyline> paths,
ArrayList<LatLng> points, int color, boolean append) {
if (points.size() == 0) {
return;
}
if (append && paths.size() != 0) {
Polyline lastPolyline = paths.get(paths.size() - 1);
ArrayList<LatLng> pathPoints = new ArrayList<LatLng>();
pathPoints.addAll(lastPolyline.getPoints());
pathPoints.addAll(points);
lastPolyline.setPoints(pathPoints);
} else {
PolylineOptions polylineOptions = new PolylineOptions().addAll(points).width(5).color(color);
Polyline polyline = googleMap.addPolyline(polylineOptions);
paths.add(polyline);
}
points.clear();
}
/**
* Given a Marker, Polyline, Polygon or an array of these and removes it from the map
*
* @param mapObject map object or array of map objects to remove from the map
*/
protected void removeFromMap(Object mapObject) {
if (mapObject instanceof Marker) {
mMarkers.remove((Marker) mapObject);
} else if (mapObject instanceof Polyline) {
mPolylines.remove((Polyline) mapObject);
} else if (mapObject instanceof Polygon) {
mPolygons.remove((Polygon) mapObject);
} else if (mapObject instanceof GroundOverlay) {
mGroundOverlays.remove((GroundOverlay) mapObject);
} else if (mapObject instanceof ArrayList) {
for (Object mapObjectElement : (ArrayList) mapObject) {
removeFromMap(mapObjectElement);
}
}
}
public Polyline getPolyline(int index, Polyline pl, int color) {
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.addAll(pl.getPoints());
polylineOptions.color(this.context.getResources().getColor(color)).zIndex(3.0f);
polylineOptions.width(4.0f);
Polyline line = this.googleMap.addPolyline(polylineOptions);
line.setPattern(PATTERN_DASHED);
return line;
}
public void drawPointLine(LatLng latLngDevice) {
if (this.gglMapLocationManager != null && this.gglMapLocationManager.getDevLocation() != null) {
Polyline polyline;
if (this.polylineList != null) {
for (Polyline polyline2 : this.polylineList) {
polyline2.remove();
}
this.polylineList.clear();
}
for (int i = 0; i < this.mMarkerList.size(); i++) {
PolylineOptions polylineOptions = new PolylineOptions();
if (i == 0) {
polylineOptions.add(latLngDevice);
polylineOptions.add(((Marker) this.mMarkerList.get(i)).getPosition());
} else {
polylineOptions.add(((Marker) this.mMarkerList.get(i - 1)).getPosition());
polylineOptions.add(((Marker) this.mMarkerList.get(i)).getPosition());
}
polylineOptions.color(this.context.getResources().getColor(this.lineDefaultColor)).zIndex(3.0f);
polylineOptions.width(4.0f);
polyline2 = this.googleMap.addPolyline(polylineOptions);
polyline2.setPattern(PATTERN_DASHED);
try {
this.polylineList.add(polyline2);
} catch (Exception e) {
}
}
}
}
public void changeLine() {
if (this.mMarkerList != null && this.nPos == -1 && this.polylineList != null && this.polylineList.size() > 0) {
Polyline c = (Polyline) this.polylineList.get(0);
List<LatLng> mLatLng = new ArrayList();
mLatLng.add(this.gglMapLocationManager.getDevLocation());
mLatLng.add(((Marker) this.mMarkerList.get(0)).getPosition());
c.setPoints(mLatLng);
}
}
public static List<Polyline> drawDashedPolyLine(GoogleMap mMap, ArrayList<LatLng> listOfPoints, int color) {
List<Polyline> tracklines = new ArrayList();
boolean added = false;
for (int i = 0; i < listOfPoints.size() - 1; i++) {
double distance = getConvertedDistance((LatLng) listOfPoints.get(i), (LatLng) listOfPoints.get(i + 1));
if (distance >= 0.02d) {
int countOfDivisions = (int) (distance / 0.02d);
double latdiff = (((LatLng) listOfPoints.get(i + 1)).latitude - ((LatLng) listOfPoints.get(i)).latitude) / ((double) countOfDivisions);
double lngdiff = (((LatLng) listOfPoints.get(i + 1)).longitude - ((LatLng) listOfPoints.get(i)).longitude) / ((double) countOfDivisions);
LatLng lastKnowLatLng = new LatLng(((LatLng) listOfPoints.get(i)).latitude, ((LatLng) listOfPoints.get(i)).longitude);
for (int j = 0; j < countOfDivisions; j++) {
LatLng nextLatLng = new LatLng(lastKnowLatLng.latitude + latdiff, lastKnowLatLng.longitude + lngdiff);
if (added) {
added = false;
} else {
tracklines.add(mMap.addPolyline(new PolylineOptions().add(lastKnowLatLng).add(nextLatLng).color(color)));
added = true;
}
lastKnowLatLng = nextLatLng;
}
} else if (added) {
added = false;
} else {
tracklines.add(mMap.addPolyline(new PolylineOptions().add((LatLng) listOfPoints.get(i)).add((LatLng) listOfPoints.get(i + 1)).color(color)));
added = true;
}
}
return tracklines;
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// Runs on UI thread
if (msg.what == Constants.TASK_COMPLETE) {
CurveOptions curveOptions = (CurveOptions) msg.obj;
// Draws curve on map
Polyline real = googleMap.addPolyline(curveOptions.getReal());
Curve curve = new DelegatingCurve(real, CurveManager.this);
curves.put(real, curve);
if (onCurveDrawnCallback != null) {
onCurveDrawnCallback.onCurveDrawn(curve);
}
}
}
/**
* Convert a list of {@link Polyline} to a {@link MultiLineString}
*
* @param polylineList polyline list
* @param hasZ has z flag
* @param hasM has m flag
* @return multi line string
*/
public MultiLineString toMultiLineString(List<Polyline> polylineList,
boolean hasZ, boolean hasM) {
MultiLineString multiLineString = new MultiLineString(hasZ, hasM);
for (Polyline polyline : polylineList) {
LineString lineString = toLineString(polyline);
multiLineString.addLineString(lineString);
}
return multiLineString;
}
/**
* Convert a list of {@link Polyline} to a {@link CompoundCurve}
*
* @param polylineList polyline list
* @param hasZ has z flag
* @param hasM has m flag
* @return compound curve
*/
public CompoundCurve toCompoundCurve(List<Polyline> polylineList,
boolean hasZ, boolean hasM) {
CompoundCurve compoundCurve = new CompoundCurve(hasZ, hasM);
for (Polyline polyline : polylineList) {
LineString lineString = toLineString(polyline);
compoundCurve.addLineString(lineString);
}
return compoundCurve;
}
@Override
public void updatePath(GoogleMap googleMap, ArrayList<Polyline> paths, int startIndex,
List<CachedLocation> locations) {
if (googleMap == null) {
return;
}
if (startIndex >= locations.size()) {
return;
}
boolean newSegment = startIndex == 0 || !locations.get(startIndex - 1).isValid();
ArrayList<LatLng> lastSegmentPoints = new ArrayList<LatLng>();
boolean useLastPolyline = true;
for (int i = startIndex; i < locations.size(); i++) {
CachedLocation cachedLocation = locations.get(i);
// If not valid, start a new segment
if (!cachedLocation.isValid()) {
newSegment = true;
continue;
}
LatLng latLng = cachedLocation.getLatLng();
if (newSegment) {
TrackPathUtils.addPath(googleMap, paths, lastSegmentPoints, color, useLastPolyline);
useLastPolyline = false;
newSegment = false;
}
lastSegmentPoints.add(latLng);
}
TrackPathUtils.addPath(googleMap, paths, lastSegmentPoints, color, useLastPolyline);
}
public void addPolyline(String layerId, Polyline polyline, String popupHTML) {
if (featurePolylines.get(layerId) == null) {
featurePolylines.put(layerId, new ArrayList<Polyline>());
}
featurePolylines.get(layerId).add(polyline);
featurePolylineDescriptions.put(polyline, popupHTML);
}
public Collection<Polyline> getPolylines() {
Collection<Polyline> polylines = new ArrayList<Polyline>();
for (Map.Entry<String, Collection<Polyline>> entry : featurePolylines.entrySet()) {
for (Polyline p : entry.getValue()) {
polylines.add(p);
}
}
return polylines;
}
@Test
public void shouldEmmitPolyline() throws Exception {
TestSubscriber<Polyline> testSubscriber = new TestSubscriber<>();
new PolylineClickFunc().call(googleMap)
.subscribe(testSubscriber);
verify(googleMap).setOnPolylineClickListener(argumentCaptor.capture());
argumentCaptor.getValue().onPolylineClick(null);
testSubscriber.assertNoErrors();
testSubscriber.assertValueCount(1);
argumentCaptor.getValue().onPolylineClick(null);
testSubscriber.assertValueCount(2);
}
/**
* Styles the polyline, based on type.
* @param polyline The polyline object that needs styling.
*/
private void stylePolyline(Polyline polyline) {
String type = "";
// Get the data object stored with the polyline.
if (polyline.getTag() != null) {
type = polyline.getTag().toString();
}
switch (type) {
// If no type is given, allow the API to use the default.
case "A":
// Use a custom bitmap as the cap at the start of the line.
polyline.setStartCap(
new CustomCap(
BitmapDescriptorFactory.fromResource(R.drawable.ic_arrow), 10));
break;
case "B":
// Use a round cap at the start of the line.
polyline.setStartCap(new RoundCap());
break;
}
polyline.setEndCap(new RoundCap());
polyline.setWidth(POLYLINE_STROKE_WIDTH_PX);
polyline.setColor(COLOR_BLACK_ARGB);
polyline.setJointType(JointType.ROUND);
}
/**
* Listens for clicks on a polyline.
* @param polyline The polyline object that the user has clicked.
*/
@Override
public void onPolylineClick(Polyline polyline) {
// Flip from solid stroke to dotted stroke pattern.
if ((polyline.getPattern() == null) || (!polyline.getPattern().contains(DOT))) {
polyline.setPattern(PATTERN_POLYLINE_DOTTED);
} else {
// The default pattern is a solid stroke.
polyline.setPattern(null);
}
Toast.makeText(this, "Route type " + polyline.getTag().toString(),
Toast.LENGTH_SHORT).show();
}
/**
* Does the actual drawing of the route polyline, based on the geo points provided in the navset
*
* @param navSet Navigation set bean that holds the route information, incl. geo pos
* @param color Color in which to draw the lines
*/
private void drawPath(Deque<LatLng> navSet, int color) {
clearAllMapRoutes();
PolylineOptions polyOpt = new PolylineOptions()
.color(color)
.width(5f);
polyOpt.addAll(navSet);
Polyline routePolyline = mMap.addPolyline(polyOpt);
polylineMap.put(routePolyline.getId(), routePolyline);
}
/**
* Removes all given Features from the map and clears all stored features.
*
* @param features features to remove
*/
private void removeFeatures(Collection features) {
// Remove map object from the map
for (Object mapObject : features) {
if (mapObject instanceof Collection) {
removeFeatures((Collection) mapObject);
} else if (mapObject instanceof Marker) {
mMarkers.remove((Marker) mapObject);
} else if (mapObject instanceof Polyline) {
mPolylines.remove((Polyline) mapObject);
} else if (mapObject instanceof Polygon) {
mPolygons.remove((Polygon) mapObject);
}
}
}
/**
* Adds a LineString to the map as a Polyline
*
* @param polylineOptions contains relevant styling properties for the Polyline
* @param lineString contains coordinates for the Polyline
* @return Polyline object created from given LineString
*/
private Polyline addLineStringToMap(PolylineOptions polylineOptions,
LineString lineString) {
// Add coordinates
polylineOptions.addAll(lineString.getGeometryObject());
Polyline addedPolyline = mPolylines.addPolyline(polylineOptions);
addedPolyline.setClickable(polylineOptions.isClickable());
return addedPolyline;
}
/**
* Adds all GeoJsonLineString objects in the GeoJsonMultiLineString to the map as multiple
* Polylines
*
* @param lineStringStyle contains relevant styling properties for the Polylines
* @param multiLineString contains an array of GeoJsonLineStrings
* @return array of Polylines that have been added to the map
*/
private ArrayList<Polyline> addMultiLineStringToMap(GeoJsonLineStringStyle lineStringStyle,
GeoJsonMultiLineString multiLineString) {
ArrayList<Polyline> polylines = new ArrayList<>();
for (GeoJsonLineString geoJsonLineString : multiLineString.getLineStrings()) {
polylines.add(addLineStringToMap(lineStringStyle.toPolylineOptions(), geoJsonLineString));
}
return polylines;
}
@Override
public void onPolylineClick(Polyline polyline) {
Collection collection = mAllObjects.get(polyline);
if (collection != null && collection.mPolylineClickListener != null) {
collection.mPolylineClickListener.onPolylineClick(polyline);
}
}
@Override
public void onPolylineClick(Polyline polyline)
{
onClick((CustomTag) polyline.getTag());
}
@Override
public void onPolylineClick(Polyline polyline) {
if (onCurveClickListener != null) {
onCurveClickListener.onCurveClick(curves.get(polyline));
}
}
final void onRemove(Polyline polyline) {
curves.remove(polyline);
}