Search in sources :

Example 21 with LatLng

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);
}
Also used : LatLng(com.google.android.libraries.maps.model.LatLng) DisplayMetrics(android.util.DisplayMetrics)

Example 22 with LatLng

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();
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) LatLng(com.google.android.libraries.maps.model.LatLng)

Example 23 with LatLng

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();
    }
}
Also used : LatLngBounds(com.google.android.libraries.maps.model.LatLngBounds) LatLng(com.google.android.libraries.maps.model.LatLng) KmlContainer(com.google.maps.android.data.kml.KmlContainer) KmlPlacemark(com.google.maps.android.data.kml.KmlPlacemark) KmlPolygon(com.google.maps.android.data.kml.KmlPolygon) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 24 with LatLng

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;
    });
}
Also used : GeoJsonFeature(com.google.maps.android.data.geojson.GeoJsonFeature) KmlLayer(com.google.maps.android.data.kml.KmlLayer) BitmapDescriptorFactory(com.google.android.libraries.maps.model.BitmapDescriptorFactory) MarkerOptions(com.google.android.libraries.maps.model.MarkerOptions) LatLng(com.google.android.libraries.maps.model.LatLng) IOException(java.io.IOException) ClusterManager(com.google.maps.android.clustering.ClusterManager) PolylineManager(com.google.maps.android.collections.PolylineManager) Color(android.graphics.Color) GeoJsonFeature(com.google.maps.android.data.geojson.GeoJsonFeature) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) MarkerManager(com.google.maps.android.collections.MarkerManager) JSONException(org.json.JSONException) List(java.util.List) Toast(android.widget.Toast) CameraUpdateFactory(com.google.android.libraries.maps.CameraUpdateFactory) GeoJsonLayer(com.google.maps.android.data.geojson.GeoJsonLayer) MyItem(com.google.maps.android.utils.demo.model.MyItem) GeoJsonPolygonStyle(com.google.maps.android.data.geojson.GeoJsonPolygonStyle) GroundOverlayManager(com.google.maps.android.collections.GroundOverlayManager) GeoJsonLineStringStyle(com.google.maps.android.data.geojson.GeoJsonLineStringStyle) Log(android.util.Log) PolygonManager(com.google.maps.android.collections.PolygonManager) InputStream(java.io.InputStream) KmlLayer(com.google.maps.android.data.kml.KmlLayer) GeoJsonLineStringStyle(com.google.maps.android.data.geojson.GeoJsonLineStringStyle) GroundOverlayManager(com.google.maps.android.collections.GroundOverlayManager) MarkerOptions(com.google.android.libraries.maps.model.MarkerOptions) GeoJsonPolygonStyle(com.google.maps.android.data.geojson.GeoJsonPolygonStyle) JSONException(org.json.JSONException) IOException(java.io.IOException) MyItem(com.google.maps.android.utils.demo.model.MyItem) GeoJsonLayer(com.google.maps.android.data.geojson.GeoJsonLayer) PolygonManager(com.google.maps.android.collections.PolygonManager) PolylineManager(com.google.maps.android.collections.PolylineManager) MarkerManager(com.google.maps.android.collections.MarkerManager) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) LatLng(com.google.android.libraries.maps.model.LatLng) ClusterManager(com.google.maps.android.clustering.ClusterManager)

Example 25 with LatLng

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));
}
Also used : GoogleMap(com.google.android.libraries.maps.GoogleMap) PolygonOptions(com.google.android.libraries.maps.model.PolygonOptions) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) LatLng(com.google.android.libraries.maps.model.LatLng) PolylineOptions(com.google.android.libraries.maps.model.PolylineOptions)

Aggregations

LatLng (com.google.android.libraries.maps.model.LatLng)31 JSONException (org.json.JSONException)8 MarkerOptions (com.google.android.libraries.maps.model.MarkerOptions)7 PluginMethod (com.getcapacitor.PluginMethod)5 PolylineOptions (com.google.android.libraries.maps.model.PolylineOptions)5 MyItem (com.google.maps.android.utils.demo.model.MyItem)5 InputStream (java.io.InputStream)5 JSONObject (org.json.JSONObject)5 PolygonOptions (com.google.android.libraries.maps.model.PolygonOptions)4 Nullable (androidx.annotation.Nullable)3 GoogleMap (com.google.android.libraries.maps.GoogleMap)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 JSONArray (org.json.JSONArray)3 DisplayMetrics (android.util.DisplayMetrics)2 Log (android.util.Log)2 View (android.view.View)2 CircleOptions (com.google.android.libraries.maps.model.CircleOptions)2 LatLngBounds (com.google.android.libraries.maps.model.LatLngBounds)2 Marker (com.google.android.libraries.maps.model.Marker)2