下面列出了怎么用com.google.android.gms.maps.model.PolygonOptions的API类实例代码及写法,或者点击链接到github查看源代码。
private void addPolygon()
{
googleMap.clear();
PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.add(new LatLng(0, 0));
polygonOptions.add(new LatLng(-3, 2.5));
polygonOptions.add(new LatLng(0, 5));
polygonOptions.add(new LatLng(3, 5));
polygonOptions.add(new LatLng(3, 0));
polygonOptions.add(new LatLng(0, 0));
polygonOptions.clickable(true);
polygonOptions.fillColor(Color.BLUE);
googleMap.addPolygon(polygonOptions).setTag(new CustomTag("polygon"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(0, 0)));
}
/**
* Set the style into the polygon options
*
* @param polygonOptions polygon options
* @param style style row
* @param density display density: {@link android.util.DisplayMetrics#density}
* @return true if style was set into the polygon options
*/
public static boolean setStyle(PolygonOptions polygonOptions, StyleRow style, float density) {
if (style != null) {
Color color = style.getColorOrDefault();
polygonOptions.strokeColor(color.getColorWithAlpha());
double width = style.getWidthOrDefault();
polygonOptions.strokeWidth((float) width * density);
Color fillColor = style.getFillColor();
if (fillColor != null) {
polygonOptions.fillColor(fillColor.getColorWithAlpha());
}
}
return style != null;
}
/**
* Is the point of the polygon
*
* @param point point
* @param polygon polygon
* @param geodesic geodesic check flag
* @param tolerance distance tolerance
* @return true if on the polygon
*/
public static boolean isPointOnPolygon(LatLng point, PolygonOptions polygon, boolean geodesic, double tolerance) {
boolean onPolygon = PolyUtil.containsLocation(point, polygon.getPoints(), geodesic) ||
PolyUtil.isLocationOnEdge(point, polygon.getPoints(), geodesic, tolerance);
if (onPolygon) {
for (List<LatLng> hole : polygon.getHoles()) {
if (PolyUtil.containsLocation(point, hole, geodesic)) {
onPolygon = false;
break;
}
}
}
return onPolygon;
}
/**
* Add a MultiPolygonOptions to the map as markers
*
* @param shapeMarkers google map shape markers
* @param map google map
* @param multiPolygon multi polygon options
* @param polygonMarkerOptions polygon marker options
* @param polygonMarkerHoleOptions polygon marker hole options
* @param globalPolygonOptions global polygon options
* @return multi polygon markers
*/
public MultiPolygonMarkers addMultiPolygonToMapAsMarkers(
GoogleMapShapeMarkers shapeMarkers, GoogleMap map,
MultiPolygonOptions multiPolygon,
MarkerOptions polygonMarkerOptions,
MarkerOptions polygonMarkerHoleOptions,
PolygonOptions globalPolygonOptions) {
MultiPolygonMarkers multiPolygonMarkers = new MultiPolygonMarkers();
for (PolygonOptions polygon : multiPolygon.getPolygonOptions()) {
PolygonMarkers polygonMarker = addPolygonToMapAsMarkers(
shapeMarkers, map, polygon, polygonMarkerOptions,
polygonMarkerHoleOptions, globalPolygonOptions);
shapeMarkers.add(polygonMarker);
multiPolygonMarkers.add(polygonMarker);
}
return multiPolygonMarkers;
}
/**
* Compare Polygon with Map Polygon
*
* @param converter
* @param polygon
* @param polygon2
*/
private static void comparePolygonAndMapPolygon(GoogleMapShapeConverter converter,
Polygon polygon,
PolygonOptions polygon2) {
List<LineString> rings = polygon.getRings();
List<LatLng> points = polygon2.getPoints();
List<List<LatLng>> holes = polygon2.getHoles();
TestCase.assertEquals(polygon.numRings(), 1 + holes.size());
LineString polygonRing = rings.get(0);
compareLineStringAndLatLngs(converter, polygonRing, points);
for (int i = 1; i < rings.size(); i++) {
LineString ring = rings.get(i);
List<LatLng> hole = holes.get(i - 1);
compareLineStringAndLatLngs(converter, ring, hole);
}
}
@Override
public void drawCodeArea(final OpenLocationCode code) {
if (code.equals(mLastDisplayedCode)) {
// Skip if we're already displaying this location.
return;
}
mLastDisplayedCode = code;
if (mMap != null) {
CodeArea area = code.decode();
LatLng southWest = new LatLng(area.getSouthLatitude(), area.getWestLongitude());
LatLng northWest = new LatLng(area.getNorthLatitude(), area.getWestLongitude());
LatLng southEast = new LatLng(area.getSouthLatitude(), area.getEastLongitude());
LatLng northEast = new LatLng(area.getNorthLatitude(), area.getEastLongitude());
if (mCodePolygon != null) {
mCodePolygon.remove();
}
mCodePolygon = mMap.addPolygon(
new PolygonOptions().add(southWest, northWest,northEast, southEast)
.strokeColor(ContextCompat.getColor(this.getContext(), R.color.code_box_stroke))
.strokeWidth(CODE_AREA_STROKE_WIDTH)
.fillColor(ContextCompat.getColor(this.getContext(), R.color.code_box_fill)));
}
}
/**
* Sets the inline polygon style by copying over the styles that have been set
*
* @param polygonOptions polygon options object to add inline styles to
* @param inlineStyle inline styles to apply
*/
private void setInlinePolygonStyle(PolygonOptions polygonOptions, KmlStyle inlineStyle) {
PolygonOptions inlinePolygonOptions = inlineStyle.getPolygonOptions();
if (inlineStyle.hasFill() && inlineStyle.isStyleSet("fillColor")) {
polygonOptions.fillColor(inlinePolygonOptions.getFillColor());
}
if (inlineStyle.hasOutline()) {
if (inlineStyle.isStyleSet("outlineColor")) {
polygonOptions.strokeColor(inlinePolygonOptions.getStrokeColor());
}
if (inlineStyle.isStyleSet("width")) {
polygonOptions.strokeWidth(inlinePolygonOptions.getStrokeWidth());
}
}
if (inlineStyle.isPolyRandomColorMode()) {
polygonOptions.fillColor(KmlStyle.computeRandomColor(inlinePolygonOptions.getFillColor()));
}
}
/**
* Create new polygon options populated with the feature style
*
* @param featureStyle feature style
* @param density display density: {@link android.util.DisplayMetrics#density}
* @return polygon options populated with the feature style
*/
public static PolygonOptions createPolygonOptions(FeatureStyle featureStyle, float density) {
PolygonOptions polygonOptions = new PolygonOptions();
setFeatureStyle(polygonOptions, featureStyle, density);
return polygonOptions;
}
/**
* Create new polygon options populated with the style
*
* @param style style row
* @param density display density: {@link android.util.DisplayMetrics#density}
* @return polygon options populated with the style
*/
public static PolygonOptions createPolygonOptions(StyleRow style, float density) {
PolygonOptions polygonOptions = new PolygonOptions();
setStyle(polygonOptions, style, density);
return polygonOptions;
}
/**
* Is the point on the multi polygon
*
* @param point point
* @param multiPolygon multi polygon
* @param geodesic geodesic check flag
* @param tolerance distance tolerance
* @return true if on the multi polygon
*/
public static boolean isPointOnMultiPolygon(LatLng point, MultiPolygonOptions multiPolygon, boolean geodesic, double tolerance) {
boolean near = false;
for (PolygonOptions polygon : multiPolygon.getPolygonOptions()) {
near = isPointOnPolygon(point, polygon, geodesic, tolerance);
if (near) {
break;
}
}
return near;
}
/**
* Convert a {@link MultiPolygon} to a {@link MultiPolygonOptions}
*
* @param multiPolygon multi polygon
* @return multi polygon options
*/
public MultiPolygonOptions toPolygons(MultiPolygon multiPolygon) {
MultiPolygonOptions polygons = new MultiPolygonOptions();
for (Polygon polygon : multiPolygon.getPolygons()) {
PolygonOptions polygonOptions = toPolygon(polygon);
polygons.add(polygonOptions);
}
return polygons;
}
/**
* Convert a list of {@link PolygonOptions} to a {@link MultiPolygon}
*
* @param multiPolygonOptions multi polygon options
* @param hasZ has z flag
* @param hasM has m flag
* @return multi polygon
*/
public MultiPolygon toMultiPolygonFromOptions(
MultiPolygonOptions multiPolygonOptions, boolean hasZ, boolean hasM) {
MultiPolygon multiPolygon = new MultiPolygon(hasZ, hasM);
for (PolygonOptions mapPolygon : multiPolygonOptions
.getPolygonOptions()) {
Polygon polygon = toPolygon(mapPolygon);
multiPolygon.addPolygon(polygon);
}
return multiPolygon;
}
/**
* Convert a {@link PolyhedralSurface} to a {@link MultiPolygonOptions}
*
* @param polyhedralSurface polyhedral surface
* @return multi polygon options
*/
public MultiPolygonOptions toPolygons(PolyhedralSurface polyhedralSurface) {
MultiPolygonOptions polygons = new MultiPolygonOptions();
for (Polygon polygon : polyhedralSurface.getPolygons()) {
PolygonOptions polygonOptions = toPolygon(polygon);
polygons.add(polygonOptions);
}
return polygons;
}
/**
* Convert a {@link MultiPolygonOptions} to a {@link PolyhedralSurface}
*
* @param multiPolygonOptions multi polygon options
* @param hasZ has z flag
* @param hasM has m flag
* @return polyhedral surface
*/
public PolyhedralSurface toPolyhedralSurfaceWithOptions(
MultiPolygonOptions multiPolygonOptions, boolean hasZ, boolean hasM) {
PolyhedralSurface polyhedralSurface = new PolyhedralSurface(hasZ, hasM);
for (PolygonOptions mapPolygon : multiPolygonOptions
.getPolygonOptions()) {
Polygon polygon = toPolygon(mapPolygon);
polyhedralSurface.addPolygon(polygon);
}
return polyhedralSurface;
}
/**
* Add a Polygon to the map as markers
*
* @param shapeMarkers google map shape markers
* @param map google map
* @param polygonOptions polygon options
* @param polygonMarkerOptions polygon marker options
* @param polygonMarkerHoleOptions polygon marker hole options
* @param globalPolygonOptions global polygon options
* @return polygon markers
*/
public PolygonMarkers addPolygonToMapAsMarkers(
GoogleMapShapeMarkers shapeMarkers, GoogleMap map,
PolygonOptions polygonOptions, MarkerOptions polygonMarkerOptions,
MarkerOptions polygonMarkerHoleOptions,
PolygonOptions globalPolygonOptions) {
PolygonMarkers polygonMarkers = new PolygonMarkers(this);
if (globalPolygonOptions != null) {
polygonOptions.fillColor(globalPolygonOptions.getFillColor());
polygonOptions.strokeColor(globalPolygonOptions.getStrokeColor());
polygonOptions.geodesic(globalPolygonOptions.isGeodesic());
polygonOptions.visible(globalPolygonOptions.isVisible());
polygonOptions.zIndex(globalPolygonOptions.getZIndex());
polygonOptions.strokeWidth(globalPolygonOptions.getStrokeWidth());
}
com.google.android.gms.maps.model.Polygon polygon = addPolygonToMap(
map, polygonOptions);
polygonMarkers.setPolygon(polygon);
List<Marker> markers = addPointsToMapAsMarkers(map,
polygon.getPoints(), polygonMarkerOptions, true);
polygonMarkers.setMarkers(markers);
for (List<LatLng> holes : polygon.getHoles()) {
List<Marker> holeMarkers = addPointsToMapAsMarkers(map, holes,
polygonMarkerHoleOptions, true);
PolygonHoleMarkers polygonHoleMarkers = new PolygonHoleMarkers(
polygonMarkers);
polygonHoleMarkers.setMarkers(holeMarkers);
shapeMarkers.add(polygonHoleMarkers);
polygonMarkers.addHole(polygonHoleMarkers);
}
return polygonMarkers;
}
/**
* Test the Polygon conversion
*
* @param converter
* @param polygon
*/
private static void convertPolygon(GoogleMapShapeConverter converter,
Polygon polygon) {
PolygonOptions polygonOptions = converter.toPolygon(polygon);
TestCase.assertNotNull(polygonOptions);
comparePolygonAndMapPolygon(converter, polygon, polygonOptions);
Polygon polygon2 = converter.toPolygon(polygonOptions);
comparePolygons(polygon, polygon2);
}
/**
* Compare list of polygons with list of map polygons
*
* @param converter
* @param polygons
* @param mapPolygons
*/
private static void comparePolygonsAndMapPolygons(GoogleMapShapeConverter converter, List<Polygon> polygons,
List<PolygonOptions> mapPolygons) {
TestCase.assertEquals(polygons.size(), mapPolygons.size());
for (int i = 0; i < polygons.size(); i++) {
comparePolygonAndMapPolygon(converter, polygons.get(i), mapPolygons.get(i));
}
}
/**
* Set the polygon options
*
* @param style shape style
* @param polygonOptions polygon options
* @param visible visible flag
*/
private void setPolygonOptions(ObservationShapeStyle style, PolygonOptions polygonOptions, boolean visible) {
polygonOptions.strokeWidth(style.getStrokeWidth());
polygonOptions.strokeColor(style.getStrokeColor());
polygonOptions.fillColor(style.getFillColor());
polygonOptions.visible(visible);
polygonOptions.geodesic(MapShapeObservation.GEODESIC);
}
private void drawPolygon( LatLng startingLocation ) {
LatLng point2 = new LatLng( startingLocation.latitude + .001, startingLocation.longitude );
LatLng point3 = new LatLng( startingLocation.latitude, startingLocation.longitude + .001 );
PolygonOptions options = new PolygonOptions();
options.add(startingLocation, point2, point3);
options.fillColor( getResources().getColor( R.color.fill_color ) );
options.strokeColor( getResources().getColor( R.color.stroke_color ) );
options.strokeWidth( 10 );
getMap().addPolygon(options);
}
/**
* Add a Polyline and a Polygon to the map.
* The Polyline connects Melbourne, Adelaide and Perth. The Polygon is located in the Northern
* Territory (Australia).
*/
private void addPolyObjects() {
map.addPolyline((new PolylineOptions())
.add(MELBOURNE, ADELAIDE, PERTH)
.color(Color.GREEN)
.width(5f));
map.addPolygon(new PolygonOptions()
.add(POLYGON)
.fillColor(Color.CYAN)
.strokeColor(Color.BLUE)
.strokeWidth(5));
}
private void drawPolygon( LatLng startingLocation ) {
LatLng point2 = new LatLng( startingLocation.latitude + .001, startingLocation.longitude );
LatLng point3 = new LatLng( startingLocation.latitude, startingLocation.longitude + .001 );
PolygonOptions options = new PolygonOptions();
options.add(startingLocation, point2, point3);
options.fillColor( getResources().getColor( R.color.fill_color ) );
options.strokeColor( getResources().getColor( R.color.stroke_color ) );
options.strokeWidth( 10 );
getMap().addPolygon(options);
}
/**
* Adds a DataPolygon to the map as a Polygon
*
* @param polygonOptions
* @param polygon contains coordinates for the Polygon
* @return Polygon object created from given DataPolygon
*/
private Polygon addPolygonToMap(PolygonOptions polygonOptions, DataPolygon polygon) {
// First array of coordinates are the outline
polygonOptions.addAll(polygon.getOuterBoundaryCoordinates());
// Following arrays are holes
List<List<LatLng>> innerBoundaries = polygon.getInnerBoundaryCoordinates();
for (List<LatLng> innerBoundary : innerBoundaries) {
polygonOptions.addHole(innerBoundary);
}
Polygon addedPolygon = mPolygons.addPolygon(polygonOptions);
addedPolygon.setClickable(polygonOptions.isClickable());
return addedPolygon;
}
/**
* Creates a new Style object
*/
public Style() {
mMarkerOptions = new MarkerOptions();
mPolylineOptions = new PolylineOptions();
mPolylineOptions.clickable(true);
mPolygonOptions = new PolygonOptions();
mPolygonOptions.clickable(true);
}
/**
* Gets a new PolygonOptions object containing styles for the GeoJsonPolygon
*
* @return new PolygonOptions object
*/
public PolygonOptions toPolygonOptions() {
PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.fillColor(mPolygonOptions.getFillColor());
polygonOptions.geodesic(mPolygonOptions.isGeodesic());
polygonOptions.strokeColor(mPolygonOptions.getStrokeColor());
polygonOptions.strokeJointType(mPolygonOptions.getStrokeJointType());
polygonOptions.strokePattern(mPolygonOptions.getStrokePattern());
polygonOptions.strokeWidth(mPolygonOptions.getStrokeWidth());
polygonOptions.visible(mPolygonOptions.isVisible());
polygonOptions.zIndex(mPolygonOptions.getZIndex());
polygonOptions.clickable(mPolygonOptions.isClickable());
return polygonOptions;
}
/**
* Gets a PolygonOption
*
* @return new PolygonOptions
*/
public PolygonOptions getPolygonOptions() {
if (mInlineStyle == null) {
return null;
}
return mInlineStyle.getPolygonOptions();
}
/**
* Creates a new PolygonOption from given properties of an existing PolygonOption
*
* @param originalPolygonOption An existing PolygonOption instance
* @param isFill Whether the fill for a polygon is set
* @param isOutline Whether the outline for a polygon is set
* @return A new PolygonOption
*/
private static PolygonOptions createPolygonOptions(PolygonOptions originalPolygonOption,
boolean isFill, boolean isOutline) {
PolygonOptions polygonOptions = new PolygonOptions();
if (isFill) {
polygonOptions.fillColor(originalPolygonOption.getFillColor());
}
if (isOutline) {
polygonOptions.strokeColor(originalPolygonOption.getStrokeColor());
polygonOptions.strokeWidth(originalPolygonOption.getStrokeWidth());
}
polygonOptions.clickable(originalPolygonOption.isClickable());
return polygonOptions;
}
/**
* 戦場1つを描画する
*/
private void drawField(Field field) {
// マーカー定義
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.title(field.getName()); // 名前を設定
markerOptions.snippet(field.getMemo()); // 説明を設定
markerOptions.position(calcCenter(field.getVertexes())); // マーカーの座標を設定(区画の中心を自動算出)
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(field.getColorHue())); // 色を設定
// マップにマーカーを追加
mGoogleMap.addMarker(markerOptions);
// 区画を描画
final LatLng[] vertexes = field.getVertexes();
if (vertexes != null && vertexes.length > 3) {
// ポリゴン定義
PolygonOptions polygonOptions = new PolygonOptions();
// RGBそれぞれの色を作成
final int[] colorRgb = field.getColorRgb();
int colorRed = colorRgb[0];
int colorGreen = colorRgb[1];
int colorBlue = colorRgb[2];
// 区画の輪郭について設定
polygonOptions.strokeColor(Color.argb(0x255, colorRed, colorGreen, colorBlue));
polygonOptions.strokeWidth(5);
// 区画の塗りつぶしについて設定
polygonOptions.fillColor(Color.argb(0x40, colorRed, colorGreen, colorBlue));
// 各頂点の座標を設定
polygonOptions.add(vertexes); // LatLngでもLatLng[]でもOK
// マップにポリゴンを追加
mGoogleMap.addPolygon(polygonOptions);
}
}
public void drawCandyNoFlyZone(LatLng[] lats) {
PolygonOptions options = new PolygonOptions();
}
/**
* Is the point on or near the shape
*
* @param point lat lng point
* @param shape map shape
* @param geodesic geodesic check flag
* @param tolerance distance tolerance
* @return true if point is on shape
*/
public static boolean isPointOnShape(LatLng point,
GoogleMapShape shape, boolean geodesic, double tolerance) {
boolean onShape = false;
switch (shape.getShapeType()) {
case LAT_LNG:
onShape = isPointNearPoint(point, (LatLng) shape.getShape(), tolerance);
break;
case MARKER_OPTIONS:
onShape = isPointNearMarker(point, (MarkerOptions) shape.getShape(), tolerance);
break;
case POLYLINE_OPTIONS:
onShape = isPointOnPolyline(point, (PolylineOptions) shape.getShape(), geodesic, tolerance);
break;
case POLYGON_OPTIONS:
onShape = isPointOnPolygon(point, (PolygonOptions) shape.getShape(), geodesic, tolerance);
break;
case MULTI_LAT_LNG:
onShape = isPointNearMultiLatLng(point, (MultiLatLng) shape.getShape(), tolerance);
break;
case MULTI_POLYLINE_OPTIONS:
onShape = isPointOnMultiPolyline(point, (MultiPolylineOptions) shape.getShape(), geodesic, tolerance);
break;
case MULTI_POLYGON_OPTIONS:
onShape = isPointOnMultiPolygon(point, (MultiPolygonOptions) shape.getShape(), geodesic, tolerance);
break;
case COLLECTION:
@SuppressWarnings("unchecked")
List<GoogleMapShape> shapeList = (List<GoogleMapShape>) shape
.getShape();
for (GoogleMapShape shapeListItem : shapeList) {
onShape = isPointOnShape(point, shapeListItem, geodesic, tolerance);
if (onShape) {
break;
}
}
break;
default:
throw new GeoPackageException("Unsupported Shape Type: "
+ shape.getShapeType());
}
return onShape;
}
/**
* Updates visibility of all objects
*
* @param visible visible flag
* @since 1.3.2
*/
public void setVisible(boolean visible) {
switch (shapeType) {
case MARKER_OPTIONS:
((MarkerOptions) shape).visible(visible);
break;
case POLYLINE_OPTIONS:
((PolylineOptions) shape).visible(visible);
break;
case POLYGON_OPTIONS:
((PolygonOptions) shape).visible(visible);
break;
case MULTI_POLYLINE_OPTIONS:
((MultiPolylineOptions) shape).visible(visible);
break;
case MULTI_POLYGON_OPTIONS:
((MultiPolygonOptions) shape).visible(visible);
break;
case MARKER:
((Marker) shape).setVisible(visible);
break;
case POLYGON:
((Polygon) shape).setVisible(visible);
break;
case POLYLINE:
((Polyline) shape).setVisible(visible);
break;
case MULTI_MARKER:
((MultiMarker) shape).setVisible(visible);
break;
case MULTI_POLYLINE:
((MultiPolyline) shape).setVisible(visible);
break;
case MULTI_POLYGON:
((MultiPolygon) shape).setVisible(visible);
break;
case POLYLINE_MARKERS:
((PolylineMarkers) shape).setVisible(visible);
break;
case POLYGON_MARKERS:
((PolygonMarkers) shape).setVisible(visible);
break;
case MULTI_POLYLINE_MARKERS:
((MultiPolylineMarkers) shape).setVisible(visible);
break;
case MULTI_POLYGON_MARKERS:
((MultiPolygonMarkers) shape).setVisible(visible);
break;
case COLLECTION:
@SuppressWarnings("unchecked")
List<GoogleMapShape> shapeList = (List<GoogleMapShape>) shape;
for (GoogleMapShape shapeListItem : shapeList) {
shapeListItem.setVisible(visible);
}
break;
default:
}
}