protected override void OnCreate(Android.OS.Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            AddOnlineBaseLayer(CartoBaseMapStyle.CartoBasemapStylePositron);

            // read json from assets and add to map
            string json;

            using (System.IO.StreamReader sr = new System.IO.StreamReader(Assets.Open("cities15000.geojson")))
            {
                json = sr.ReadToEnd();
            }

            // Initialize a local vector data source
            LocalVectorDataSource source = new LocalVectorDataSource(BaseProjection);

            // Initialize a vector layer with the previous data source
            ClusteredVectorLayer layer = new ClusteredVectorLayer(source, new MyClusterElementBuilder(this));

            layer.MinimumClusterDistance = 50;

            new System.Threading.Thread((obj) =>
            {
                // Create a basic style, as the ClusterElementBuilder will set the real style
                var markerStyleBuilder  = new MarkerStyleBuilder();
                markerStyleBuilder.Size = 14;
                MarkerStyle style       = markerStyleBuilder.BuildStyle();

                // Read GeoJSON, parse it using SDK GeoJSON parser
                GeoJSONGeometryReader reader = new GeoJSONGeometryReader();

                // Set target projection to base (mercator)
                reader.TargetProjection = BaseProjection;
                Alert("Starting load from .geojson");

                // Read features from local asset
                FeatureCollection features = reader.ReadFeatureCollection(json);
                Alert("Finished load from .geojson");

                VectorElementVector elements = new VectorElementVector();

                for (int i = 0; i < features.FeatureCount; i++)
                {
                    // This data set features point geometry,
                    // however, it can also be LineGeometry or PolygonGeometry
                    PointGeometry geometry = (PointGeometry)features.GetFeature(i).Geometry;
                    elements.Add(new Marker(geometry, style));
                }

                source.AddAll(elements);
                Alert("Clustering started. Please wait...");

                // Add the clustered vector layer to the map
                MapView.Layers.Add(layer);
            }).Start();
        }
        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);

            // Add default base layer
            AddOnlineBaseLayer(CartoBaseMapStyle.CartoBasemapStylePositron);

            // Initialize a local vector data source
            LocalVectorDataSource source = new LocalVectorDataSource(BaseProjection);

            // Initialize a vector layer with the previous data source
            var layer = new ClusteredVectorLayer(source, new MyClusterElementBuilder());

            // Default is 100. A good value depends on data
            layer.MinimumClusterDistance = 50;

            // read json from assets and add to map
            string json = System.IO.File.ReadAllText(AssetUtils.CalculateResourcePath("cities15000.geojson"));

            // Create a basic style, as the ClusterElementBuilder will set the real style
            var markerStyleBuilder = new MarkerStyleBuilder();

            markerStyleBuilder.Size = 14;
            MarkerStyle style = markerStyleBuilder.BuildStyle();

            // Read GeoJSON, parse it using SDK GeoJSON parser
            GeoJSONGeometryReader reader = new GeoJSONGeometryReader();

            // Set target projection to base (mercator)
            reader.TargetProjection = BaseProjection;

            // Read features from local asset
            FeatureCollection features = reader.ReadFeatureCollection(json);

            VectorElementVector elements = new VectorElementVector();

            for (int i = 0; i < features.FeatureCount; i++)
            {
                // This data set features point geometry,
                // however, it can also be LineGeometry or PolygonGeometry
                PointGeometry geometry = (PointGeometry)features.GetFeature(i).Geometry;
                elements.Add(new Marker(geometry, style));
            }

            // Add the clustered vector layer to the map
            source.AddAll(elements);

            MapView.Layers.Add(layer);
        }
Exemple #3
0
        public static void addJosnLayer(IMapView mapView, String json)
        {
            var features = Newtonsoft.Json.Linq.JObject.Parse(json)["features"];

            var geoJsonParser = new GeoJSONGeometryReader();

            var proj = new EPSG3857();
            var balloonPopupStyleBuilder = new BalloonPopupStyleBuilder();

            // Create overlay layer for markers
            var dataSource   = new LocalVectorDataSource(proj);
            var overlayLayer = new ClusteredVectorLayer(dataSource, new MyClusterElementBuilder());

            overlayLayer.MinimumClusterDistance = 80;             // in pixels
            mapView.Layers.Add(overlayLayer);

            foreach (var feature in features)
            {
                var featureType = feature ["type"];

                var geometry = feature ["geometry"];
                var ntGeom   = geoJsonParser.ReadGeometry(Newtonsoft.Json.JsonConvert.SerializeObject(geometry));

                var popup = new BalloonPopup(
                    ntGeom,
                    balloonPopupStyleBuilder.BuildStyle(),
                    (string)feature ["properties"]["Capital"], (string)feature ["properties"]["Country"]);

                var properties = (JObject)feature ["properties"];
                foreach (var property in properties)
                {
                    var key   = (string)property.Key;
                    var value = (string)property.Value;
                    popup.SetMetaDataElement(key, value);
                }

                dataSource.Add(popup);
            }
        }
        void LoadData(VectorElementVector elements, MapPos min, MapPos max, float zoom)
        {
            // Load and parse JSON
            string bbox = EncodeBBox(min.X, min.Y, max.X, max.Y);

            string unencoded = query.Replace("!bbox!", bbox);

            unencoded = unencoded.Replace("zoom('!scale_denominator!')", Convert.ToString(zoom));

            // UrlEncode does not replace parentheses by default as they are valid url elements
            string encoded = System.Web.HttpUtility.UrlEncode(unencoded).EncodeParenthesis();

            string fullPath = baseUrl + "?format=GeoJSON&q=" + encoded;

            try
            {
                string json = GetString(fullPath);

                GeoJSONGeometryReader geoJsonParser = new GeoJSONGeometryReader();
                FeatureCollection     features      = geoJsonParser.ReadFeatureCollection(json);

                for (int i = 0; i < features.FeatureCount; i++)
                {
                    Geometry geom  = features.GetFeature(i).Geometry;
                    Variant  props = features.GetFeature(i).Properties;

                    // Create object based on given style
                    VectorElement element;

                    if (style is PointStyle)
                    {
                        element = new Point((PointGeometry)geom, (PointStyle)style);
                    }
                    else if (style is MarkerStyle)
                    {
                        element = new Marker(geom, (MarkerStyle)style);
                    }
                    else if (style is LineStyle)
                    {
                        element = new Line((LineGeometry)geom, (LineStyle)style);
                    }
                    else if (style is PolygonStyle)
                    {
                        element = new Polygon((PolygonGeometry)geom, (PolygonStyle)style);
                    }
                    else
                    {
                        string text = "Object creation not implemented yet for style: " + style.SwigGetClassNameStyle();
                        Carto.Utils.Log.Debug(text);
                        break;
                    }

                    // Add all properties as MetaData, so you can use it with click handling
                    for (int j = 0; j < props.ObjectKeys.Count; j++)
                    {
                        var key = props.ObjectKeys[j];
                        var val = props.GetObjectElement(key);
                        element.SetMetaDataElement(key, val);
                    }

                    elements.Add(element);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
        }