下面列出了怎么用com.google.android.gms.maps.model.PatternItem的API类实例代码及写法,或者点击链接到github查看源代码。
public static GoogleMapConfig.Builder getBuilder(Context context) {
int width = context.getResources().getDisplayMetrics().widthPixels;
int height = context.getResources().getDisplayMetrics().heightPixels;
int mapRouteWidth = context.getResources().getDimensionPixelSize(R.dimen.map_route_width);
GoogleMapConfig.TripOptions tripOptions = GoogleMapConfig.newTripOptions()
.tripDestinationMarker(new MarkerOptions()
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_destination_marker)))
.tripPassedRoutePolyline(null)
.tripComingRoutePolyline(new PolylineOptions()
.width(mapRouteWidth)
.color(Color.BLACK)
.pattern(Collections.<PatternItem>singletonList(new Dash(mapRouteWidth))));
return GoogleMapConfig.newBuilder(context)
.tripOptions(tripOptions)
.boundingBoxDimensions(width, height / 3);
}
/**
* Convert the list of latitude and longitude to the polyline options.
*
* @param context A context.
* @param locationList A list of latitude and longitude.
* @param width Width of the polyline in screen pixels.
* @param color Color of the polyline as a 32-bit ARGB color.
* @param clickable Is polyline clickable.
* @param patternItemList Stroke pattern for the polyline.
* @return Options for a polyline.
* @since 1.2.0
*/
public static PolylineOptions createPolyline(
@NonNull Context context,
@Nullable ArrayList<LatLng> locationList,
@Dimension(unit = Dimension.DP) int width,
@ColorInt int color,
boolean clickable,
@Nullable List<PatternItem> patternItemList
) {
return createPolyline(
context,
locationList,
width,
color,
clickable,
JointType.DEFAULT,
null,
null,
patternItemList
);
}
/**
* Convert the list of latitude and longitude to the polyline options in transit mode.
*
* @param context A context.
* @param stepList A list of latitude and longitude for the steps.
* @param transitWidth Width of the polyline in screen pixels for transit polyline.
* @param transitColor Color of the polyline as a 32-bit ARGB color for transit polyline.
* @param transitPatternItemList Stroke pattern for the polyline for transit polyline.
* @param walkingWidth Width of the polyline in screen pixels for walking polyline.
* @param walkingColor Color of the polyline as a 32-bit ARGB color for walking polyline.
* @param walkingPatternItemList Stroke pattern for the polyline for walking polyline.
* @return Options for a polyline.
* @since 1.2.0
*/
public static ArrayList<PolylineOptions> createTransitPolyline(
@NonNull Context context,
@Nullable List<Step> stepList,
@Dimension(unit = Dimension.DP) int transitWidth,
@Nullable List<PatternItem> transitPatternItemList,
@ColorInt int transitColor,
@Dimension(unit = Dimension.DP) int walkingWidth,
@ColorInt int walkingColor,
@Nullable List<PatternItem> walkingPatternItemList) {
return createTransitPolyline(
context,
stepList,
transitWidth,
transitColor,
transitPatternItemList,
walkingWidth,
walkingColor,
walkingPatternItemList,
true,
JointType.DEFAULT,
null,
null
);
}
@Override
public void onMapReady(GoogleMap googleMap) {
this.map = googleMap;
curveManager = new CurveManager(map);
ArrayList<LatLng> latLngArrayList = new ArrayList<>();
latLngArrayList.add(new LatLng(12.30548451, 76.65521267));
latLngArrayList.add(new LatLng(19.81516491, 85.83133625));
latLngArrayList.add(new LatLng(26.9124336, 75.7872709));
latLngArrayList.add(new LatLng(28.596111, 83.820278));
CurveOptions curveOptions = new CurveOptions();
curveOptions.addAll(latLngArrayList);
curveOptions.color(Color.DKGRAY);
curveOptions.setComputePointsBasedOnScreenPixels(false);
curveOptions.setAlpha(0.5f);
curveOptions.width(10);
List<PatternItem> pattern = Arrays.asList(new Dash(30), new Gap(20));
curveOptions.pattern(pattern);
curveOptions.geodesic(false);
for (LatLng position : latLngArrayList) {
new MarkerOptions().position(position);
map.addMarker(new MarkerOptions().position(position));
}
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(21.146633, 79.088860), 5));
curveManager.drawCurveAsync(curveOptions);
}
@Override
public void onMapReady(GoogleMap googleMap) {
this.map = googleMap;
curveManager = new CurveManager(map);
CurveOptions curveOptions = new CurveOptions();
curveOptions.add(sourceLatLng);
curveOptions.add(destinationLatLng);
curveOptions.color(Color.DKGRAY);
curveOptions.setComputePointsBasedOnScreenPixels(true);
curveOptions.setAlpha(0.5f);
curveOptions.width(12);
List<PatternItem> pattern = Arrays.asList(new Dash(30), new Gap(30));
curveOptions.pattern(pattern);
curveOptions.geodesic(false);
map.addMarker(new MarkerOptions()
.position(sourceLatLng)
.anchor(0.5f, 1f));
map.addMarker(new MarkerOptions()
.position(destinationLatLng)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
.anchor(0.5f, 1f));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(56.14683221, 10.2079815), 14));
curveManager.drawCurveAsync(curveOptions);
}
/**
* Convert the list of latitude and longitude to the polyline options.
*
* @param context A context.
* @param locationList A list of latitude and longitude.
* @param width Width of the polyline in screen pixels.
* @param color Color of the polyline as a 32-bit ARGB color.
* @param clickable Is polyline clickable.
* @param jointType Joint type for all vertices of the polyline except the start and end vertices.
* @param startCap Cap at the start vertex of the polyline.
* @param endCap Cap at the end vertex of the polyline.
* @param patternItemList Stroke pattern for the polyline.
* @return Options for a polyline.
* @since 1.2.0
*/
public static PolylineOptions createPolyline(
@NonNull Context context,
@Nullable ArrayList<LatLng> locationList,
@Dimension(unit = Dimension.DP) int width,
@ColorInt int color,
boolean clickable,
int jointType,
@Nullable Cap startCap,
@Nullable Cap endCap,
@Nullable List<PatternItem> patternItemList) {
PolylineOptions rectLine = new PolylineOptions().width(dpToPx(context, width)).color(color).geodesic(true);
rectLine.clickable(clickable);
rectLine.jointType(jointType);
if (patternItemList != null) {
rectLine.pattern(patternItemList);
}
if (startCap != null) {
rectLine.startCap(startCap);
}
if (endCap != null) {
rectLine.endCap(endCap);
}
if (locationList != null && locationList.size() > 0) {
for (LatLng location : locationList) {
rectLine.add(location);
}
}
return rectLine;
}
/**
* Styles the polygon, based on type.
* @param polygon The polygon object that needs styling.
*/
private void stylePolygon(Polygon polygon) {
String type = "";
// Get the data object stored with the polygon.
if (polygon.getTag() != null) {
type = polygon.getTag().toString();
}
List<PatternItem> pattern = null;
int strokeColor = COLOR_BLACK_ARGB;
int fillColor = COLOR_WHITE_ARGB;
switch (type) {
// If no type is given, allow the API to use the default.
case "alpha":
// Apply a stroke pattern to render a dashed line, and define colors.
pattern = PATTERN_POLYGON_ALPHA;
strokeColor = COLOR_GREEN_ARGB;
fillColor = COLOR_PURPLE_ARGB;
break;
case "beta":
// Apply a stroke pattern to render a line of dots and dashes, and define colors.
pattern = PATTERN_POLYGON_BETA;
strokeColor = COLOR_ORANGE_ARGB;
fillColor = COLOR_BLUE_ARGB;
break;
}
polygon.setStrokePattern(pattern);
polygon.setStrokeWidth(POLYGON_STROKE_WIDTH_PX);
polygon.setStrokeColor(strokeColor);
polygon.setFillColor(fillColor);
}
private List<PatternItem> getSelectedPattern(int pos) {
switch (PATTERN_TYPE_NAME_RESOURCE_IDS[pos]) {
case R.string.pattern_solid:
return null;
case R.string.pattern_dotted:
return PATTERN_DOTTED;
case R.string.pattern_dashed:
return PATTERN_DASHED;
case R.string.pattern_mixed:
return PATTERN_MIXED;
default:
return null;
}
}
private List<PatternItem> getSelectedPattern(int pos) {
switch (PATTERN_TYPE_NAME_RESOURCE_IDS[pos]) {
case R.string.pattern_solid:
return null;
case R.string.pattern_dotted:
return PATTERN_DOTTED;
case R.string.pattern_dashed:
return PATTERN_DASHED;
case R.string.pattern_mixed:
return PATTERN_MIXED;
default:
return null;
}
}
private List<PatternItem> getSelectedPattern(int pos) {
switch (PATTERN_TYPE_NAME_RESOURCE_IDS[pos]) {
case R.string.pattern_solid:
return null;
case R.string.pattern_dotted:
return PATTERN_DOTTED;
case R.string.pattern_dashed:
return PATTERN_DASHED;
case R.string.pattern_mixed:
return PATTERN_MIXED;
default:
return null;
}
}
@Test
public void testStrokePattern() {
List<PatternItem> strokePatternItems = new ArrayList<>();
strokePatternItems.add(new Dot());
polygonStyle.setStrokePattern(strokePatternItems);
assertEquals(strokePatternItems, polygonStyle.getStrokePattern());
assertEquals(strokePatternItems, polygonStyle.toPolygonOptions().getStrokePattern());
}
private void drawCurveLine() {
String sourceLatitude = latitudeInputOneEditText.getText().toString().trim();
String sourceLongitude = longitudeInputOneEditText.getText().toString().trim();
String destLatitude = latitudeInputTwoEditText.getText().toString().trim();
String destLongitude = longitudeInputTwoEditText.getText().toString().trim();
String alpha = alphaEditText.getText().toString().trim();
if (sourceLatitude.equals("") || sourceLongitude.equals("")
|| destLatitude.equals("") || destLongitude.equals("") || alpha.equals("")) {
return;
}
LatLng initLatLng = new LatLng(Double.valueOf(sourceLatitude), Double.valueOf(sourceLongitude));
LatLng finalLatLng = new LatLng(Double.valueOf(destLatitude), Double.valueOf(destLongitude));
CurveOptions curveOptions = new CurveOptions();
curveOptions.add(initLatLng);
curveOptions.add(finalLatLng);
curveOptions.clickable(true);
curveOptions.setComputePointsBasedOnScreenPixels(checkBox.isChecked());
curveOptions.setZoomToPosition(true);
curveOptions.setAlpha(Float.valueOf(alpha));
curveOptions.width(12);
List<PatternItem> pattern = Arrays.asList(new Dash(30), new Gap(25));
curveOptions.pattern(pattern);
curveOptions.geodesic(false);
if (checkBox.isChecked()) {
curveOptions.color(getResources().getColor(R.color.red_500));
} else {
curveOptions.color(getResources().getColor(R.color.indigo_a700));
}
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(initLatLng);
builder.include(finalLatLng);
LatLngBounds bounds = builder.build();
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 20));
if (curveManager != null) {
curveManager.drawCurveAsync(curveOptions);
}
map.addMarker(new MarkerOptions().position(initLatLng).anchor(0.5f, 1f));
map.addMarker(new MarkerOptions().position(finalLatLng)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
.anchor(0.5f, 1f));
}
@Override
public List<PatternItem> getPattern() {
return polyline.getPattern();
}
@Override
public void setPattern(List<PatternItem> pattern) {
polyline.setPattern(pattern);
}
public List<PatternItem> getPattern() {
return real.getPattern();
}
/**
* Convert the list of latitude and longitude to the polyline options in transit mode.
*
* @param context A context.
* @param stepList A list of latitude and longitude for the steps.
* @param transitWidth Width of the polyline in screen pixels for transit polyline.
* @param transitColor Color of the polyline as a 32-bit ARGB color for transit polyline.
* @param transitPatternItemList Stroke pattern for the polyline for transit polyline.
* @param walkingWidth Width of the polyline in screen pixels for walking polyline.
* @param walkingColor Color of the polyline as a 32-bit ARGB color for walking polyline.
* @param walkingPatternItemList Stroke pattern for the polyline for walking polyline.
* @param clickable Is polyline clickable.
* @param jointType Joint type for all vertices of the polyline except the start and end vertices.
* @param startCap Cap at the start vertex of the polyline.
* @param endCap Cap at the end vertex of the polyline.
* @return Options for a polyline.
* @since 1.2.0
*/
public static ArrayList<PolylineOptions> createTransitPolyline(
@NonNull Context context,
@Nullable List<Step> stepList,
@Dimension(unit = Dimension.DP) int transitWidth,
@ColorInt int transitColor,
@Nullable List<PatternItem> transitPatternItemList,
@Dimension(unit = Dimension.DP) int walkingWidth,
@ColorInt int walkingColor,
@Nullable List<PatternItem> walkingPatternItemList,
boolean clickable,
int jointType,
@Nullable Cap startCap,
@Nullable Cap endCap
) {
ArrayList<PolylineOptions> polylineOptionsList = new ArrayList<>();
if (stepList != null && stepList.size() > 0) {
for (Step step : stepList) {
ArrayList<LatLng> directionPointList = new ArrayList<>();
convertStepToPosition(step, directionPointList);
if (step.isContainStepList()) {
polylineOptionsList.add(createPolyline(
context,
directionPointList,
walkingWidth,
walkingColor,
clickable,
jointType,
startCap,
endCap,
walkingPatternItemList
));
} else {
polylineOptionsList.add(createPolyline(
context,
directionPointList,
transitWidth,
transitColor,
clickable,
jointType,
startCap,
endCap,
transitPatternItemList
));
}
}
}
return polylineOptionsList;
}
public List<PatternItem> getPatternItemList() {
return patternItemList;
}
public PathOption setPatternItemList(List<PatternItem> patternItemList) {
this.patternItemList = patternItemList;
return this;
}
@Nullable
public List<PatternItem> getTransitPatternItemList() {
return transitPatternItemList;
}
public TransitPathOption setTransitPatternItemList(@Nullable List<PatternItem> transitPatternItemList) {
this.transitPatternItemList = transitPatternItemList;
return this;
}
@Nullable
public List<PatternItem> getWalkingPatternItemList() {
return walkingPatternItemList;
}
public TransitPathOption setWalkingPatternItemList(@Nullable List<PatternItem> walkingPatternItemList) {
this.walkingPatternItemList = walkingPatternItemList;
return this;
}
public void setStrokePattern(List<PatternItem> pattern) {
circle.setStrokePattern(pattern);
}
/**
* Gets the pattern of the GeoJsonLineString
*
* @return line style of GeoJsonLineString
*/
public List<PatternItem> getPattern() {
return mPolylineOptions.getPattern();
}
/**
* Sets the pattern of the GeoJsonLineString
*
* @param pattern line style of GeoJsonLineString
*/
public void setPattern(List<PatternItem> pattern) {
mPolylineOptions.pattern(pattern);
styleChanged();
}
/**
* Gets the stroke pattern of the GeoJsonPolygon as a list of pattern items
*
* @return stroke pattern of the GeoJsonPolygon
*/
public List<PatternItem> getStrokePattern() {
return mPolygonOptions.getStrokePattern();
}
/**
* Sets the stroke pattern of the GeoJsonPolygon as a list of pattern items
*
* @param strokePattern stroke pattern value of the GeoJsonPolygon
*/
public void setStrokePattern(List<PatternItem> strokePattern) {
mPolygonOptions.strokePattern(strokePattern);
styleChanged();
}
List<PatternItem> getPattern();
void setPattern(List<PatternItem> pattern);