Search in sources :

Example 1 with Marker

use of com.google.android.libraries.maps.model.Marker in project google-maps by capacitor-community.

the class CapacitorGoogleMaps method addMarker.

@PluginMethod()
public void addMarker(final PluginCall call) {
    final Double latitude = call.getDouble("latitude", 0d);
    final Double longitude = call.getDouble("longitude", 0d);
    final Float opacity = call.getFloat("opacity", 1.0f);
    final String title = call.getString("title", "");
    final String snippet = call.getString("snippet", "");
    final Boolean isFlat = call.getBoolean("isFlat", true);
    final JSObject metadata = call.getObject("metadata");
    final String url = call.getString("iconUrl", "");
    final Boolean draggable = call.getBoolean("draggable", false);
    if (googleMap == null) {
        call.reject("Map is not ready");
        return;
    }
    Bitmap imageBitmap = getBitmapFromURL(url);
    getBridge().getActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
            LatLng latLng = new LatLng(latitude, longitude);
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.alpha(opacity);
            markerOptions.title(title);
            markerOptions.snippet(snippet);
            markerOptions.flat(isFlat);
            markerOptions.draggable(draggable);
            if (imageBitmap != null) {
                markerOptions.icon(BitmapDescriptorFactory.fromBitmap(imageBitmap));
            }
            Marker marker = googleMap.addMarker(markerOptions);
            // set metadata to marker
            marker.setTag(metadata);
            // get auto-generated id of the just added marker,
            // put this marker into a hashmap with the corresponding id,
            // so we can retrieve the marker by id later on
            mHashMap.put(marker.getId(), marker);
            // initialize JSObject to return when resolving this call
            JSObject result = new JSObject();
            JSObject markerResult = new JSObject();
            // get marker specific values
            markerResult.put("id", marker.getId());
            result.put("marker", markerResult);
            call.resolve(result);
        }
    });
}
Also used : Bitmap(android.graphics.Bitmap) MarkerOptions(com.google.android.libraries.maps.model.MarkerOptions) JSObject(com.getcapacitor.JSObject) LatLng(com.google.android.libraries.maps.model.LatLng) Marker(com.google.android.libraries.maps.model.Marker) PluginMethod(com.getcapacitor.PluginMethod)

Example 2 with Marker

use of com.google.android.libraries.maps.model.Marker in project android-samples by googlemaps.

the class MarkerDemoActivity method addMarkersToMap.

private void addMarkersToMap() {
    // Uses a colored icon.
    mBrisbane = mMap.addMarker(new MarkerOptions().position(BRISBANE).title("Brisbane").snippet("Population: 2,074,200").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    // Uses a custom icon with the info window popping out of the center of the icon.
    mSydney = mMap.addMarker(new MarkerOptions().position(SYDNEY).title("Sydney").snippet("Population: 4,627,300").icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)).infoWindowAnchor(0.5f, 0.5f));
    // Creates a draggable marker. Long press to drag.
    mMelbourne = mMap.addMarker(new MarkerOptions().position(MELBOURNE).title("Melbourne").snippet("Population: 4,137,400").draggable(true));
    // Place four markers on top of each other with differing z-indexes.
    mDarwin1 = mMap.addMarker(new MarkerOptions().position(DARWIN).title("Darwin Marker 1").snippet("z-index 1").zIndex(1));
    mDarwin2 = mMap.addMarker(new MarkerOptions().position(DARWIN).title("Darwin Marker 2").snippet("z-index 2").zIndex(2));
    mDarwin3 = mMap.addMarker(new MarkerOptions().position(DARWIN).title("Darwin Marker 3").snippet("z-index 3").zIndex(3));
    mDarwin4 = mMap.addMarker(new MarkerOptions().position(DARWIN).title("Darwin Marker 4").snippet("z-index 4").zIndex(4));
    // A few more markers for good measure.
    mPerth = mMap.addMarker(new MarkerOptions().position(PERTH).title("Perth").snippet("Population: 1,738,800"));
    mAdelaide = mMap.addMarker(new MarkerOptions().position(ADELAIDE).title("Adelaide").snippet("Population: 1,213,000"));
    // Vector drawable resource as a marker icon.
    mMap.addMarker(new MarkerOptions().position(ALICE_SPRINGS).icon(vectorToBitmap(R.drawable.ic_android, Color.parseColor("#A4C639"))).title("Alice Springs"));
    // Creates a marker rainbow demonstrating how to create default marker icons of different
    // hues (colors).
    float rotation = mRotationBar.getProgress();
    boolean flat = mFlatBox.isChecked();
    int numMarkersInRainbow = 12;
    for (int i = 0; i < numMarkersInRainbow; i++) {
        Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(-30 + 10 * Math.sin(i * Math.PI / (numMarkersInRainbow - 1)), 135 - 10 * Math.cos(i * Math.PI / (numMarkersInRainbow - 1)))).title("Marker " + i).icon(BitmapDescriptorFactory.defaultMarker(i * 360 / numMarkersInRainbow)).flat(flat).rotation(rotation));
        mMarkerRainbow.add(marker);
    }
}
Also used : MarkerOptions(com.google.android.libraries.maps.model.MarkerOptions) Marker(com.google.android.libraries.maps.model.Marker) LatLng(com.google.android.libraries.maps.model.LatLng)

Aggregations

LatLng (com.google.android.libraries.maps.model.LatLng)2 Marker (com.google.android.libraries.maps.model.Marker)2 MarkerOptions (com.google.android.libraries.maps.model.MarkerOptions)2 Bitmap (android.graphics.Bitmap)1 JSObject (com.getcapacitor.JSObject)1 PluginMethod (com.getcapacitor.PluginMethod)1