Search in sources :

Example 1 with MarkerData

use of com.mapbox.mapboxsdk.plugins.geojson.model.MarkerData in project mapbox-plugins-android by mapbox.

the class GeoJsonPlugin method drawOnMap.

/**
 * Drawing {@link DataModel} element on to the map
 *
 * @param dataModel list of polylines, polygons, and points
 */
private void drawOnMap(DataModel dataModel) {
    if (dataModel != null) {
        List<MarkerData> markers = dataModel.getMarkers();
        if (markers != null) {
            if (!markers.isEmpty()) {
                for (MarkerData markerData : dataModel.getMarkers()) {
                    Marker marker = map.addMarker(new MarkerOptions().position(markerData.getPoint()));
                    markerCollectionHashMap.put(marker, markerData);
                }
            }
        }
        List<PolyData> polygons = dataModel.getPolygons();
        if (polygons != null) {
            if (!polygons.isEmpty()) {
                for (PolyData polyData : polygons) {
                    if (isRandomFillColor) {
                        fillColor = getRandomMaterialColor("400");
                    }
                    if (isRandomStockColor) {
                        stockColor = getRandomMaterialColor("400");
                    }
                    map.addPolygon(new PolygonOptions().addAll(polyData.getPoints()).fillColor(fillColor).strokeColor(stockColor));
                }
            }
        }
        List<PolyData> polylines = dataModel.getPolylines();
        if (polylines != null) {
            if (!polylines.isEmpty()) {
                for (PolyData polyData : polylines) {
                    if (isRandomStockColor) {
                        stockColor = getRandomMaterialColor("400");
                    }
                    map.addPolyline(new PolylineOptions().addAll(polyData.getPoints()).color(stockColor).width(width));
                }
            }
        }
        if (dataModel.getBounds() != null) {
            map.easeCamera(CameraUpdateFactory.newLatLngBounds(dataModel.getBounds(), 50), 500);
        }
        // set onMarkerClick Listener and pass properties of marker to the MarkerEventListener
        map.setOnMarkerClickListener(new MapboxMap.OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(@NonNull Marker marker) {
                MarkerData markerData = markerCollectionHashMap.get(marker);
                if (markerEventListener != null) {
                    markerEventListener.onMarkerClickListener(marker, markerData.getProperties());
                }
                return false;
            }
        });
    }
}
Also used : PolyData(com.mapbox.mapboxsdk.plugins.geojson.model.PolyData) MarkerData(com.mapbox.mapboxsdk.plugins.geojson.model.MarkerData) MarkerOptions(com.mapbox.mapboxsdk.annotations.MarkerOptions) PolygonOptions(com.mapbox.mapboxsdk.annotations.PolygonOptions) Marker(com.mapbox.mapboxsdk.annotations.Marker) MapboxMap(com.mapbox.mapboxsdk.maps.MapboxMap) PolylineOptions(com.mapbox.mapboxsdk.annotations.PolylineOptions)

Example 2 with MarkerData

use of com.mapbox.mapboxsdk.plugins.geojson.model.MarkerData in project mapbox-plugins-android by mapbox.

the class GeoJsonPlugin method parseGeoJsonString.

/**
 * @param geoJson String of the GeoJSON file
 * @return DataModel list of polylines, polygons, and point with bounded
 */
private DataModel parseGeoJsonString(String geoJson) {
    int pointCount = 0;
    DataModel dataModel = new DataModel();
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    FeatureCollection featureCollection = FeatureCollection.fromJson(geoJson);
    List<Feature> listFeature = featureCollection.features();
    for (Feature feature : listFeature) {
        String featureType = feature.geometry().type();
        if (!TextUtils.isEmpty(featureType)) {
            if (featureType.equalsIgnoreCase("LineString")) {
                List<LatLng> latLngs = new ArrayList<>();
                LineString lineString = (LineString) feature.geometry();
                List<Point> coordinates = lineString.coordinates();
                for (Point position : coordinates) {
                    LatLng latLng = new LatLng(position.latitude(), position.longitude());
                    latLngs.add(latLng);
                    pointCount++;
                    builder.include(latLng);
                }
                PolyData polylinePolyData = new PolyData();
                polylinePolyData.setPoints(latLngs);
                polylinePolyData.setType(featureType);
                dataModel.addPolyline(polylinePolyData);
            } else if (featureType.equalsIgnoreCase("Point")) {
                Point point = (Point) feature.geometry();
                if (point != null) {
                    LatLng latLng = new LatLng(point.latitude(), point.longitude());
                    MarkerData markerData = new MarkerData();
                    markerData.setPoint(latLng);
                    markerData.setProperties(feature.properties());
                    dataModel.addMarker(markerData);
                    pointCount++;
                    builder.include(latLng);
                }
            } else if (featureType.equalsIgnoreCase("Polygon")) {
                List<LatLng> latLngs = new ArrayList<>();
                Polygon polygon = (Polygon) feature.geometry();
                List<Point> listPosition = polygon.coordinates().get(0);
                for (Point position : listPosition) {
                    LatLng latLng = new LatLng(position.latitude(), position.longitude());
                    latLngs.add(latLng);
                    pointCount++;
                    builder.include(latLng);
                }
                PolyData polygonPolyData = new PolyData();
                polygonPolyData.setPoints(latLngs);
                polygonPolyData.setType(featureType);
                dataModel.addPolygon(polygonPolyData);
            } else {
            // TODO
            }
        }
    }
    if (pointCount > 1) {
        dataModel.setBounds(builder.build());
    }
    return dataModel;
}
Also used : MarkerData(com.mapbox.mapboxsdk.plugins.geojson.model.MarkerData) LatLngBounds(com.mapbox.mapboxsdk.geometry.LatLngBounds) ArrayList(java.util.ArrayList) LineString(com.mapbox.geojson.LineString) Point(com.mapbox.geojson.Point) Feature(com.mapbox.geojson.Feature) Point(com.mapbox.geojson.Point) PolyData(com.mapbox.mapboxsdk.plugins.geojson.model.PolyData) FeatureCollection(com.mapbox.geojson.FeatureCollection) LineString(com.mapbox.geojson.LineString) DataModel(com.mapbox.mapboxsdk.plugins.geojson.model.DataModel) LatLng(com.mapbox.mapboxsdk.geometry.LatLng) Polygon(com.mapbox.geojson.Polygon)

Aggregations

MarkerData (com.mapbox.mapboxsdk.plugins.geojson.model.MarkerData)2 PolyData (com.mapbox.mapboxsdk.plugins.geojson.model.PolyData)2 Feature (com.mapbox.geojson.Feature)1 FeatureCollection (com.mapbox.geojson.FeatureCollection)1 LineString (com.mapbox.geojson.LineString)1 Point (com.mapbox.geojson.Point)1 Polygon (com.mapbox.geojson.Polygon)1 Marker (com.mapbox.mapboxsdk.annotations.Marker)1 MarkerOptions (com.mapbox.mapboxsdk.annotations.MarkerOptions)1 PolygonOptions (com.mapbox.mapboxsdk.annotations.PolygonOptions)1 PolylineOptions (com.mapbox.mapboxsdk.annotations.PolylineOptions)1 LatLng (com.mapbox.mapboxsdk.geometry.LatLng)1 LatLngBounds (com.mapbox.mapboxsdk.geometry.LatLngBounds)1 MapboxMap (com.mapbox.mapboxsdk.maps.MapboxMap)1 DataModel (com.mapbox.mapboxsdk.plugins.geojson.model.DataModel)1 ArrayList (java.util.ArrayList)1