use of com.google.android.libraries.maps.model.LatLng in project android-maps-utils by googlemaps.
the class ClusteringViewModelDemoActivity method startDemo.
@Override
protected void startDemo(boolean isRestore) {
if (!isRestore) {
getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 10));
}
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int widthDp = (int) (metrics.widthPixels / metrics.density);
int heightDp = (int) (metrics.heightPixels / metrics.density);
mViewModel.getAlgorithm().updateViewSize(widthDp, heightDp);
mClusterManager = new ClusterManager<>(this, getMap());
mClusterManager.setAlgorithm(mViewModel.getAlgorithm());
getMap().setOnCameraIdleListener(mClusterManager);
}
use of com.google.android.libraries.maps.model.LatLng in project android-maps-utils by googlemaps.
the class HeatmapsPlacesDemoActivity method getPoints.
/**
* Makes four radar search requests for the given keyword, then parses the
* json output and returns the search results as a collection of LatLng objects.
*
* @param keyword A string to use as a search term for the radar search
* @return Returns the search results from radar search as a collection
* of LatLng objects, or null if there was an error calling the API
*/
private Collection<LatLng> getPoints(String keyword) {
HashMap<String, LatLng> results = new HashMap<>();
// Calculate four equidistant points around Sydney to use as search centers
// so that four searches can be done.
ArrayList<LatLng> searchCenters = new ArrayList<>(4);
for (int heading = 45; heading < 360; heading += 90) {
searchCenters.add(SphericalUtil.computeOffset(SYDNEY, SEARCH_RADIUS / 2, heading));
}
for (int j = 0; j < 4; j++) {
String jsonResults = getJsonPlaces(keyword, searchCenters.get(j));
if (jsonResults == null) {
// Error calling Places API
return null;
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults);
JSONArray pointsJsonArray = jsonObj.getJSONArray("results");
// Extract the Place descriptions from the results
for (int i = 0; i < pointsJsonArray.length(); i++) {
if (!results.containsKey(pointsJsonArray.getJSONObject(i).getString("place_id"))) {
JSONObject location = pointsJsonArray.getJSONObject(i).getJSONObject("geometry").getJSONObject("location");
results.put(pointsJsonArray.getJSONObject(i).getString("place_id"), new LatLng(location.getDouble("lat"), location.getDouble("lng")));
}
}
} catch (JSONException e) {
Log.e(TAG, "Error parsing JSON:" + e);
runOnUiThread(() -> Toast.makeText(this, "Cannot process JSON results", Toast.LENGTH_SHORT).show());
}
}
return results.values();
}
use of com.google.android.libraries.maps.model.LatLng in project android-maps-utils by googlemaps.
the class KmlDemoActivity method moveCameraToKml.
private void moveCameraToKml(KmlLayer kmlLayer) {
if (mIsRestore)
return;
try {
// 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));
} catch (Exception e) {
// may fail depending on the KML being shown
e.printStackTrace();
}
}
use of com.google.android.libraries.maps.model.LatLng in project android-maps-utils by googlemaps.
the class MultiLayerDemoActivity method startDemo.
@Override
protected void startDemo(boolean isRestore) {
if (!isRestore) {
getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.403186, -0.126446), 10));
}
// Shared object managers - used to support multiple layer types on the map simultaneously
MarkerManager markerManager = new MarkerManager(getMap());
GroundOverlayManager groundOverlayManager = new GroundOverlayManager(getMap());
PolygonManager polygonManager = new PolygonManager(getMap());
PolylineManager polylineManager = new PolylineManager(getMap());
// Add clustering
ClusterManager<MyItem> clusterManager = new ClusterManager<>(this, getMap(), markerManager);
getMap().setOnCameraIdleListener(clusterManager);
addClusterItems(clusterManager);
// Add GeoJSON from resource
try {
// GeoJSON polyline
GeoJsonLayer geoJsonLineLayer = new GeoJsonLayer(getMap(), R.raw.south_london_line_geojson, this, markerManager, polygonManager, polylineManager, groundOverlayManager);
// Make the line red
GeoJsonLineStringStyle geoJsonLineStringStyle = new GeoJsonLineStringStyle();
geoJsonLineStringStyle.setColor(Color.RED);
for (GeoJsonFeature f : geoJsonLineLayer.getFeatures()) {
f.setLineStringStyle(geoJsonLineStringStyle);
}
geoJsonLineLayer.addLayerToMap();
geoJsonLineLayer.setOnFeatureClickListener((GeoJsonLayer.GeoJsonOnFeatureClickListener) feature -> Toast.makeText(MultiLayerDemoActivity.this, "GeoJSON polyline clicked: " + feature.getProperty("title"), Toast.LENGTH_SHORT).show());
// GeoJSON polygon
GeoJsonLayer geoJsonPolygonLayer = new GeoJsonLayer(getMap(), R.raw.south_london_square_geojson, this, markerManager, polygonManager, polylineManager, groundOverlayManager);
// Fill it with red
GeoJsonPolygonStyle geoJsonPolygonStyle = new GeoJsonPolygonStyle();
geoJsonPolygonStyle.setFillColor(Color.RED);
for (GeoJsonFeature f : geoJsonPolygonLayer.getFeatures()) {
f.setPolygonStyle(geoJsonPolygonStyle);
}
geoJsonPolygonLayer.addLayerToMap();
geoJsonPolygonLayer.setOnFeatureClickListener((GeoJsonLayer.GeoJsonOnFeatureClickListener) feature -> Toast.makeText(MultiLayerDemoActivity.this, "GeoJSON polygon clicked: " + feature.getProperty("title"), Toast.LENGTH_SHORT).show());
} catch (IOException e) {
Log.e(TAG, "GeoJSON file could not be read");
} catch (JSONException e) {
Log.e(TAG, "GeoJSON file could not be converted to a JSONObject");
}
// Add KMLs from resources
try {
// KML Polyline
KmlLayer kmlPolylineLayer = new KmlLayer(getMap(), R.raw.south_london_line_kml, this, markerManager, polygonManager, polylineManager, groundOverlayManager, null);
kmlPolylineLayer.addLayerToMap();
kmlPolylineLayer.setOnFeatureClickListener(feature -> Toast.makeText(MultiLayerDemoActivity.this, "KML polyline clicked: " + feature.getProperty("name"), Toast.LENGTH_SHORT).show());
// KML Polygon
KmlLayer kmlPolygonLayer = new KmlLayer(getMap(), R.raw.south_london_square_kml, this, markerManager, polygonManager, polylineManager, groundOverlayManager, null);
kmlPolygonLayer.addLayerToMap();
kmlPolygonLayer.setOnFeatureClickListener(feature -> Toast.makeText(MultiLayerDemoActivity.this, "KML polygon clicked: " + feature.getProperty("name"), Toast.LENGTH_SHORT).show());
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Unclustered marker - instead of adding to the map directly, use the MarkerManager
MarkerManager.Collection markerCollection = markerManager.newCollection();
markerCollection.addMarker(new MarkerOptions().position(new LatLng(51.150000, -0.150032)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)).title("Unclustered marker"));
markerCollection.setOnMarkerClickListener(marker -> {
Toast.makeText(MultiLayerDemoActivity.this, "Marker clicked: " + marker.getTitle(), Toast.LENGTH_SHORT).show();
return false;
});
}
use of com.google.android.libraries.maps.model.LatLng in project android-maps-utils by googlemaps.
the class PolySimplifyDemoActivity method startDemo.
@Override
protected void startDemo(boolean isRestore) {
GoogleMap map = getMap();
// Original line
List<LatLng> line = PolyUtil.decode(LINE);
map.addPolyline(new PolylineOptions().addAll(line).color(Color.BLACK));
if (!isRestore) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(28.05870, -82.4090), 15));
}
List<LatLng> simplifiedLine;
/*
* Simplified lines - increasing the tolerance will result in fewer points in the simplified
* line
*/
// meters
double tolerance = 5;
simplifiedLine = PolyUtil.simplify(line, tolerance);
map.addPolyline(new PolylineOptions().addAll(simplifiedLine).color(Color.RED - ALPHA_ADJUSTMENT));
// meters
tolerance = 20;
simplifiedLine = PolyUtil.simplify(line, tolerance);
map.addPolyline(new PolylineOptions().addAll(simplifiedLine).color(Color.GREEN - ALPHA_ADJUSTMENT));
// meters
tolerance = 50;
simplifiedLine = PolyUtil.simplify(line, tolerance);
map.addPolyline(new PolylineOptions().addAll(simplifiedLine).color(Color.MAGENTA - ALPHA_ADJUSTMENT));
// meters
tolerance = 500;
simplifiedLine = PolyUtil.simplify(line, tolerance);
map.addPolyline(new PolylineOptions().addAll(simplifiedLine).color(Color.YELLOW - ALPHA_ADJUSTMENT));
// meters
tolerance = 1000;
simplifiedLine = PolyUtil.simplify(line, tolerance);
map.addPolyline(new PolylineOptions().addAll(simplifiedLine).color(Color.BLUE - ALPHA_ADJUSTMENT));
// Triangle polygon - the polygon should be closed
ArrayList<LatLng> triangle = new ArrayList<>();
// Should match last point
triangle.add(new LatLng(28.06025, -82.41030));
triangle.add(new LatLng(28.06129, -82.40945));
triangle.add(new LatLng(28.06206, -82.40917));
triangle.add(new LatLng(28.06125, -82.40850));
triangle.add(new LatLng(28.06035, -82.40834));
triangle.add(new LatLng(28.06038, -82.40924));
// Should match first point
triangle.add(new LatLng(28.06025, -82.41030));
map.addPolygon(new PolygonOptions().addAll(triangle).fillColor(Color.BLUE - ALPHA_ADJUSTMENT).strokeColor(Color.BLUE).strokeWidth(5));
// Simplified triangle polygon
// meters
tolerance = 88;
List simplifiedTriangle = PolyUtil.simplify(triangle, tolerance);
map.addPolygon(new PolygonOptions().addAll(simplifiedTriangle).fillColor(Color.YELLOW - ALPHA_ADJUSTMENT).strokeColor(Color.YELLOW).strokeWidth(5));
// Oval polygon - the polygon should be closed
List<LatLng> oval = PolyUtil.decode(OVAL_POLYGON);
map.addPolygon(new PolygonOptions().addAll(oval).fillColor(Color.BLUE - ALPHA_ADJUSTMENT).strokeColor(Color.BLUE).strokeWidth(5));
// Simplified oval polygon
// meters
tolerance = 10;
List simplifiedOval = PolyUtil.simplify(oval, tolerance);
map.addPolygon(new PolygonOptions().addAll(simplifiedOval).fillColor(Color.YELLOW - ALPHA_ADJUSTMENT).strokeColor(Color.YELLOW).strokeWidth(5));
}
Aggregations