Search in sources :

Example 1 with MapView

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

the class CapacitorGoogleMaps method create.

@PluginMethod()
public void create(PluginCall call) {
    final Integer width = call.getInt("width", DEFAULT_WIDTH);
    final Integer height = call.getInt("height", DEFAULT_HEIGHT);
    final Integer x = call.getInt("x", 0);
    final Integer y = call.getInt("y", 0);
    final Float zoom = call.getFloat("zoom", DEFAULT_ZOOM);
    final Double latitude = call.getDouble("latitude");
    final Double longitude = call.getDouble("longitude");
    final boolean liteMode = call.getBoolean("enabled", false);
    final CapacitorGoogleMaps ctx = this;
    getBridge().getActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
            LatLng latLng = new LatLng(latitude, longitude);
            CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(zoom).build();
            GoogleMapOptions googleMapOptions = new GoogleMapOptions();
            googleMapOptions.camera(cameraPosition);
            googleMapOptions.liteMode(liteMode);
            if (mapViewParentId != null) {
                View viewToRemove = ((ViewGroup) getBridge().getWebView().getParent()).findViewById(mapViewParentId);
                if (viewToRemove != null) {
                    ((ViewGroup) getBridge().getWebView().getParent()).removeViewInLayout(viewToRemove);
                }
            }
            FrameLayout mapViewParent = new FrameLayout(getBridge().getContext());
            mapViewParentId = View.generateViewId();
            mapViewParent.setId(mapViewParentId);
            mapView = new MapView(getBridge().getContext(), googleMapOptions);
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(getScaledPixels(width), getScaledPixels(height));
            lp.topMargin = getScaledPixels(y);
            lp.leftMargin = getScaledPixels(x);
            mapView.setLayoutParams(lp);
            mapViewParent.addView(mapView);
            ((ViewGroup) getBridge().getWebView().getParent()).addView(mapViewParent);
            mapView.onCreate(null);
            mapView.onStart();
            mapView.getMapAsync(ctx);
        }
    });
    call.resolve();
}
Also used : View(android.view.View) MapView(com.google.android.libraries.maps.MapView) CameraPosition(com.google.android.libraries.maps.model.CameraPosition) GoogleMapOptions(com.google.android.libraries.maps.GoogleMapOptions) FrameLayout(android.widget.FrameLayout) MapView(com.google.android.libraries.maps.MapView) LatLng(com.google.android.libraries.maps.model.LatLng) PluginMethod(com.getcapacitor.PluginMethod)

Aggregations

View (android.view.View)1 FrameLayout (android.widget.FrameLayout)1 PluginMethod (com.getcapacitor.PluginMethod)1 GoogleMapOptions (com.google.android.libraries.maps.GoogleMapOptions)1 MapView (com.google.android.libraries.maps.MapView)1 CameraPosition (com.google.android.libraries.maps.model.CameraPosition)1 LatLng (com.google.android.libraries.maps.model.LatLng)1