private void moveCameraToKml(KmlLayer kmlLayer) { //Retrieve the first container in the KML layer KmlContainer container = kmlLayer.getContainers().iterator().next(); //Retrieve a nested container within the first container container = container.getContainers().iterator().next(); //Retrieve the first placemark in the nested container KmlPlacemark placemark = container.getPlacemarks().iterator().next(); //Retrieve a polygon object in a placemark KmlPolygon polygon = (KmlPolygon) placemark.getGeometry(); //Create LatLngBounds of the outer coordinates of the polygon LatLngBounds.Builder builder = new LatLngBounds.Builder(); for (LatLng latLng : polygon.getOuterBoundaryCoordinates()) { builder.include(latLng); } int width = getResources().getDisplayMetrics().widthPixels; int height = getResources().getDisplayMetrics().heightPixels; getMap().moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), width, height, 1)); }
/** * Creates a new KmlPolygon object. Parses only one outer boundary and no or many inner * boundaries containing the coordinates. * * @return KmlPolygon object */ private static KmlPolygon createPolygon(XmlPullParser parser) throws XmlPullParserException, IOException { // Indicates if an outer boundary needs to be defined Boolean isOuterBoundary = false; List<LatLng> outerBoundary = new ArrayList<>(); List<List<LatLng>> innerBoundaries = new ArrayList<>(); int eventType = parser.getEventType(); while (!(eventType == END_TAG && parser.getName().equals("Polygon"))) { if (eventType == START_TAG) { if (parser.getName().matches(BOUNDARY_REGEX)) { isOuterBoundary = parser.getName().equals("outerBoundaryIs"); } else if (parser.getName().equals("coordinates")) { if (isOuterBoundary) { outerBoundary = convertToLatLngArray(parser.nextText()); } else { innerBoundaries.add(convertToLatLngArray(parser.nextText())); } } } eventType = parser.next(); } return new KmlPolygon(outerBoundary, innerBoundaries); }
public void testGetKmlGeometryObject() throws Exception { kmlPolygon = createRegularPolygon(); assertNotNull(kmlPolygon); assertNotNull(kmlPolygon.getGeometryObject()); assertEquals(kmlPolygon.getGeometryObject().size(), 2); kmlPolygon = createOuterPolygon(); assertNotNull(kmlPolygon); assertNotNull(kmlPolygon.getGeometryObject()); assertEquals(kmlPolygon.getGeometryObject().size(), 1); } }
public void testPolygon() throws Exception { XmlPullParser xmlPullParser = createParser(R.raw.amu_basic_placemark); KmlPlacemark placemark = KmlFeatureParser.createPlacemark(xmlPullParser); assertNotNull(placemark); assertEquals(placemark.getGeometry().getGeometryType(), "Polygon"); KmlPolygon polygon = ((KmlPolygon) placemark.getGeometry()); assertEquals(polygon.getInnerBoundaryCoordinates().size(), 2); assertEquals(polygon.getOuterBoundaryCoordinates().size(), 5); }
public void testGetType() throws Exception { kmlPolygon = createRegularPolygon(); assertNotNull(kmlPolygon); assertNotNull(kmlPolygon.getGeometryType()); assertEquals("Polygon", kmlPolygon.getGeometryType()); }
public void testGetInnerBoundaryCoordinates() throws Exception { kmlPolygon = createRegularPolygon(); assertNotNull(kmlPolygon); assertNotNull(kmlPolygon.getInnerBoundaryCoordinates()); kmlPolygon = createOuterPolygon(); assertNotNull(kmlPolygon); assertNull(kmlPolygon.getInnerBoundaryCoordinates()); }
public void testGetOuterBoundaryCoordinates() throws Exception { kmlPolygon = createRegularPolygon(); assertNotNull(kmlPolygon); assertNotNull(kmlPolygon.getOuterBoundaryCoordinates()); kmlPolygon = createOuterPolygon(); assertNotNull(kmlPolygon); assertNotNull(kmlPolygon.getOuterBoundaryCoordinates()); }
public KmlPolygon createOuterPolygon() { ArrayList<LatLng> outerCoordinates = new ArrayList<LatLng>(); outerCoordinates.add(new LatLng(10, 10)); outerCoordinates.add(new LatLng(20, 20)); outerCoordinates.add(new LatLng(30, 30)); outerCoordinates.add(new LatLng(10, 10)); return new KmlPolygon(outerCoordinates, null); }
public static void moveCameraToKml(KmlLayer kmlLayer, GoogleMap googleMap) { //TODO fixed error with some kml file https://developers.google.com/maps/documentation/android-api/utility/kml //Only use in this kml file correctly. LatLngBounds.Builder builder = new LatLngBounds.Builder(); KmlContainer container = kmlLayer.getContainers().iterator().next(); container = container.getContainers().iterator().next(); KmlPlacemark placemark = container.getPlacemarks().iterator().next(); KmlPolygon polygon = (KmlPolygon) placemark.getGeometry(); for (LatLng latLng : polygon.getOuterBoundaryCoordinates()) { builder.include(latLng); } googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 50)); } }
public KmlPolygon createRegularPolygon() { ArrayList<LatLng> outerCoordinates = new ArrayList<LatLng>(); outerCoordinates.add(new LatLng(10, 10)); outerCoordinates.add(new LatLng(20, 20)); outerCoordinates.add(new LatLng(30, 30)); outerCoordinates.add(new LatLng(10, 10)); ArrayList<List<LatLng>> innerCoordinates = new ArrayList<List<LatLng>>(); ArrayList<LatLng> innerHole = new ArrayList<LatLng>(); innerHole.add(new LatLng(20, 20)); innerHole.add(new LatLng(10, 10)); innerHole.add(new LatLng(20, 20)); innerCoordinates.add(innerHole); return new KmlPolygon(outerCoordinates, innerCoordinates); }