Search in sources :

Example 1 with RCTSource

use of com.mapbox.rctmgl.components.styles.sources.RCTSource in project maps by rnmapbox.

the class RCTMGLMapView method getTouchableSourceWithHighestZIndex.

private RCTSource getTouchableSourceWithHighestZIndex(List<RCTSource> sources) {
    if (sources == null || sources.size() == 0) {
        return null;
    }
    if (sources.size() == 1) {
        return sources.get(0);
    }
    Map<String, RCTSource> layerToSourceMap = new HashMap<>();
    for (RCTSource source : sources) {
        String[] layerIDs = source.getLayerIDs();
        for (String layerID : layerIDs) {
            layerToSourceMap.put(layerID, source);
        }
    }
    List<Layer> mapboxLayers = mMap.getStyle().getLayers();
    for (int i = mapboxLayers.size() - 1; i >= 0; i--) {
        Layer mapboxLayer = mapboxLayers.get(i);
        String layerID = mapboxLayer.getId();
        if (layerToSourceMap.containsKey(layerID)) {
            return layerToSourceMap.get(layerID);
        }
    }
    return null;
}
Also used : RCTSource(com.mapbox.rctmgl.components.styles.sources.RCTSource) HashMap(java.util.HashMap) Layer(com.mapbox.mapboxsdk.style.layers.Layer) RCTLayer(com.mapbox.rctmgl.components.styles.layers.RCTLayer)

Example 2 with RCTSource

use of com.mapbox.rctmgl.components.styles.sources.RCTSource in project maps by rnmapbox.

the class RCTMGLMapView method addFeature.

public void addFeature(View childView, int childPosition) {
    AbstractMapFeature feature = null;
    if (childView instanceof RCTSource) {
        RCTSource source = (RCTSource) childView;
        mSources.put(source.getID(), source);
        feature = (AbstractMapFeature) childView;
    } else if (childView instanceof RCTMGLImages) {
        RCTMGLImages images = (RCTMGLImages) childView;
        mImages.add(images);
        feature = (AbstractMapFeature) childView;
    } else if (childView instanceof RCTMGLLight) {
        feature = (AbstractMapFeature) childView;
    } else if (childView instanceof RCTMGLNativeUserLocation) {
        feature = (AbstractMapFeature) childView;
    } else if (childView instanceof RCTMGLPointAnnotation) {
        RCTMGLPointAnnotation annotation = (RCTMGLPointAnnotation) childView;
        mPointAnnotations.put(annotation.getID(), annotation);
        feature = (AbstractMapFeature) childView;
    } else if (childView instanceof RCTMGLMarkerView) {
        RCTMGLMarkerView marker = (RCTMGLMarkerView) childView;
        feature = (AbstractMapFeature) childView;
    } else if (childView instanceof RCTMGLCamera) {
        mCamera = (RCTMGLCamera) childView;
        feature = (AbstractMapFeature) childView;
    } else if (childView instanceof RCTLayer) {
        feature = (RCTLayer) childView;
    } else if (childView instanceof ViewGroup) {
        ViewGroup children = (ViewGroup) childView;
        for (int i = 0; i < children.getChildCount(); i++) {
            addFeature(children.getChildAt(i), childPosition);
        }
    }
    if (feature != null) {
        if (mQueuedFeatures == null) {
            feature.addToMap(this);
            mFeatures.add(childPosition, feature);
        } else {
            mQueuedFeatures.add(childPosition, feature);
        }
    }
}
Also used : RCTMGLMarkerView(com.mapbox.rctmgl.components.annotation.RCTMGLMarkerView) RCTMGLLight(com.mapbox.rctmgl.components.styles.light.RCTMGLLight) RCTSource(com.mapbox.rctmgl.components.styles.sources.RCTSource) ViewGroup(android.view.ViewGroup) RCTMGLNativeUserLocation(com.mapbox.rctmgl.components.location.RCTMGLNativeUserLocation) AbstractMapFeature(com.mapbox.rctmgl.components.AbstractMapFeature) RCTMGLImages(com.mapbox.rctmgl.components.images.RCTMGLImages) RCTLayer(com.mapbox.rctmgl.components.styles.layers.RCTLayer) RCTMGLPointAnnotation(com.mapbox.rctmgl.components.annotation.RCTMGLPointAnnotation) RCTMGLCamera(com.mapbox.rctmgl.components.camera.RCTMGLCamera)

Example 3 with RCTSource

use of com.mapbox.rctmgl.components.styles.sources.RCTSource in project maps by rnmapbox.

the class RCTMGLMapView method onMapClick.

@Override
public boolean onMapClick(@NonNull LatLng point) {
    if (mAnnotationClicked) {
        mAnnotationClicked = false;
        return true;
    }
    PointF screenPoint = mMap.getProjection().toScreenLocation(point);
    List<RCTSource> touchableSources = getAllTouchableSources();
    Map<String, List<Feature>> hits = new HashMap<>();
    List<RCTSource> hitTouchableSources = new ArrayList<>();
    for (RCTSource touchableSource : touchableSources) {
        Map<String, Double> hitbox = touchableSource.getTouchHitbox();
        if (hitbox == null) {
            continue;
        }
        float halfWidth = hitbox.get("width").floatValue() / 2.0f;
        float halfHeight = hitbox.get("height").floatValue() / 2.0f;
        RectF hitboxF = new RectF();
        hitboxF.set(screenPoint.x - halfWidth, screenPoint.y - halfHeight, screenPoint.x + halfWidth, screenPoint.y + halfHeight);
        List<Feature> features = mMap.queryRenderedFeatures(hitboxF, touchableSource.getLayerIDs());
        if (features.size() > 0) {
            hits.put(touchableSource.getID(), features);
            hitTouchableSources.add(touchableSource);
        }
    }
    if (hits.size() > 0) {
        RCTSource source = getTouchableSourceWithHighestZIndex(hitTouchableSources);
        if (source != null && source.hasPressListener()) {
            source.onPress(new RCTSource.OnPressEvent(hits.get(source.getID()), point, screenPoint));
            return true;
        }
    }
    MapClickEvent event = new MapClickEvent(this, point, screenPoint);
    mManager.handleEvent(event);
    return false;
}
Also used : RCTSource(com.mapbox.rctmgl.components.styles.sources.RCTSource) HashMap(java.util.HashMap) PointF(android.graphics.PointF) ArrayList(java.util.ArrayList) Feature(com.mapbox.geojson.Feature) AbstractMapFeature(com.mapbox.rctmgl.components.AbstractMapFeature) RectF(android.graphics.RectF) MapClickEvent(com.mapbox.rctmgl.events.MapClickEvent) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with RCTSource

use of com.mapbox.rctmgl.components.styles.sources.RCTSource in project maps by rnmapbox.

the class RCTMGLMapView method onMapClick.

@Override
public boolean onMapClick(@NonNull Point point) {
    RCTMGLMapView _this = this;
    /*if (mPointAnnotationManager != nil) {
            getAnnotations()
        }*/
    if (mAnnotationClicked) {
        mAnnotationClicked = false;
        return true;
    }
    ScreenCoordinate screenPoint = mMap.pixelForCoordinate(point);
    List<RCTSource> touchableSources = getAllTouchableSources();
    HashMap<String, List<Feature>> hits = new HashMap<>();
    handleTapInSources(new LinkedList<>(touchableSources), screenPoint, hits, new ArrayList<>(), new HandleTap() {

        @Override
        public void run(List<RCTSource> hitTouchableSources, Map<String, List<Feature>> hits) {
            if (hits.size() > 0) {
                RCTSource source = getTouchableSourceWithHighestZIndex(hitTouchableSources);
                if (source != null && source.hasPressListener()) {
                    source.onPress(new RCTSource.OnPressEvent(hits.get(source.getID()), GeoJSONUtils.toLatLng(point), new PointF((float) screenPoint.getX(), (float) screenPoint.getY())));
                    return;
                }
            }
            MapClickEvent event = new MapClickEvent(_this, new LatLng(point), screenPoint);
            mManager.handleEvent(event);
        }
    });
    return false;
}
Also used : RCTSource(com.mapbox.rctmgl.components.styles.sources.RCTSource) HashMap(java.util.HashMap) ScreenCoordinate(com.mapbox.maps.ScreenCoordinate) PointF(android.graphics.PointF) MapClickEvent(com.mapbox.rctmgl.events.MapClickEvent) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) LatLng(com.mapbox.rctmgl.utils.LatLng)

Example 5 with RCTSource

use of com.mapbox.rctmgl.components.styles.sources.RCTSource in project maps by rnmapbox.

the class RCTMGLMapView method handleTapInSources.

void handleTapInSources(LinkedList<RCTSource> sources, ScreenCoordinate screenPoint, HashMap<String, List<Feature>> hits, ArrayList<RCTSource> hitTouchableSources, HandleTap handleTap) {
    if (sources.isEmpty()) {
        handleTap.run(hitTouchableSources, hits);
        return;
    }
    RCTSource source = sources.removeFirst();
    Map<String, Double> hitbox = source.getTouchHitbox();
    if (hitbox != null) {
        double halfWidth = hitbox.get("width").floatValue() / 2.0f;
        double halfHeight = hitbox.get("height").floatValue() / 2.0f;
        ScreenBox screenBox = new ScreenBox(new ScreenCoordinate(screenPoint.getX() - halfWidth, screenPoint.getY() - halfHeight), new ScreenCoordinate(screenPoint.getX() + halfWidth, screenPoint.getY() + halfHeight));
        getMapboxMap().queryRenderedFeatures(screenBox, new RenderedQueryOptions(source.getLayerIDs(), null), new QueryFeaturesCallback() {

            @Override
            public void run(@NonNull Expected<String, List<QueriedFeature>> features) {
                HashMap<String, List<Feature>> newHits = hits;
                if (features.isValue()) {
                    if (features.getValue().size() > 0) {
                        ArrayList<Feature> featuresList = new ArrayList<>();
                        for (QueriedFeature i : features.getValue()) {
                            featuresList.add(i.getFeature());
                        }
                        newHits.put(source.getID(), featuresList);
                        hitTouchableSources.add(source);
                    }
                } else {
                    Logger.e("handleTapInSources", features.getError());
                }
                handleTapInSources(sources, screenPoint, newHits, hitTouchableSources, handleTap);
            }
        });
    }
}
Also used : RCTSource(com.mapbox.rctmgl.components.styles.sources.RCTSource) HashMap(java.util.HashMap) ScreenCoordinate(com.mapbox.maps.ScreenCoordinate) ArrayList(java.util.ArrayList) QueryFeaturesCallback(com.mapbox.maps.QueryFeaturesCallback) Feature(com.mapbox.geojson.Feature) QueriedFeature(com.mapbox.maps.QueriedFeature) AbstractMapFeature(com.mapbox.rctmgl.components.AbstractMapFeature) RenderedQueryOptions(com.mapbox.maps.RenderedQueryOptions) QueriedFeature(com.mapbox.maps.QueriedFeature) ScreenBox(com.mapbox.maps.ScreenBox) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Aggregations

RCTSource (com.mapbox.rctmgl.components.styles.sources.RCTSource)8 AbstractMapFeature (com.mapbox.rctmgl.components.AbstractMapFeature)4 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 PointF (android.graphics.PointF)2 Feature (com.mapbox.geojson.Feature)2 ScreenCoordinate (com.mapbox.maps.ScreenCoordinate)2 RCTMGLPointAnnotation (com.mapbox.rctmgl.components.annotation.RCTMGLPointAnnotation)2 RCTMGLImages (com.mapbox.rctmgl.components.images.RCTMGLImages)2 RCTLayer (com.mapbox.rctmgl.components.styles.layers.RCTLayer)2 MapClickEvent (com.mapbox.rctmgl.events.MapClickEvent)2 LinkedList (java.util.LinkedList)2 RectF (android.graphics.RectF)1 ViewGroup (android.view.ViewGroup)1 Layer (com.mapbox.mapboxsdk.style.layers.Layer)1 QueriedFeature (com.mapbox.maps.QueriedFeature)1 QueryFeaturesCallback (com.mapbox.maps.QueryFeaturesCallback)1 RenderedQueryOptions (com.mapbox.maps.RenderedQueryOptions)1 ScreenBox (com.mapbox.maps.ScreenBox)1