Example #1
0
        private async void BtnLoc_Click(object sender, EventArgs e)
        {
            try
            {
                currentLocation = await Geolocation.GetLastKnownLocationAsync();

                if (currentLocation != null)
                {
                    txtLoc.Text = $"Latitude={currentLocation.Latitude} , Longitude={currentLocation.Longitude} ";
                    Console.WriteLine($"Latitude: {currentLocation.Latitude}, Longitude: {currentLocation.Longitude}, Altitude: {currentLocation.Altitude}");

                    //mapView.GetMapAsync(this);


                    Com.Mapbox.Geojson.Point point = Com.Mapbox.Geojson.Point.FromLngLat(currentLocation.Longitude, currentLocation.Latitude);
                    mapbox.MoveCamera(CameraUpdateFactory.NewLatLngZoom(new LatLng(point.Latitude(), point.Longitude()), 15));

                    var options = new SymbolOptions();
                    options.WithIconImage("airport-15");
                    options.WithGeometry(point);
                    options.WithIconSize(new Float(2f))
                    .WithDraggable(true);

                    var symbol = symbolManager.Create(options);
                }
            }
            catch
            {
            }
        }
Example #2
0
        public void updateMapPerspective(ICameraPerspective cameraPerspective)
        {
            if (nMap == null)
            {
                return;
            }

            if (cameraPerspective is CenterAndZoomCameraPerspective centerAndZoom)
            {
                var camera = CameraUpdateFactory.NewLatLngZoom(centerAndZoom.position.toNativeLatLng(), centerAndZoom.zoomLevel);
                if (centerAndZoom.isAnimated)
                {
                    nMap.AnimateCamera(camera);
                }
                else
                {
                    nMap.MoveCamera(camera);
                }
            }
            else if (cameraPerspective is CoordinatesAndPaddingCameraPerspective span)
            {
                var density = Resources.DisplayMetrics.Density;
                var list    = span.positions.Select((Position p) => new LatLng(p.latitude, p.longitude));
                var builder = new LatLngBounds.Builder();
                builder.Includes(list.ToList());

                var camera = nMap.GetCameraForLatLngBounds(
                    builder.Build(),
                    new int[] {
                    (int)(span.padding.Left * density),
                    (int)(span.padding.Top * density),
                    (int)(span.padding.Right * density),
                    (int)(span.padding.Bottom * density)
                });

                if (span.isAnimated)
                {
                    nMap.AnimateCamera(CameraUpdateFactory.NewCameraPosition(camera));
                }
                else
                {
                    nMap.MoveCamera(CameraUpdateFactory.NewCameraPosition(camera));
                }
            }
            else if (cameraPerspective is CoordinateCameraPerspective center)
            {
                if (center.isAnimated)
                {
                    nMap.AnimateCamera(CameraUpdateFactory.NewLatLng(center.position.toNativeLatLng()));
                }
                else
                {
                    nMap.MoveCamera(CameraUpdateFactory.NewLatLng(center.position.toNativeLatLng()));
                }
            }
        }
Example #3
0
        public void OnStyleLoaded(Style p0)
        {
            symbolManager = new SymbolManager(mapView, mapboxMap, p0);

            var point = Com.Mapbox.Geojson.Point.FromLngLat(105.505, 21.033);

            mapboxMap.MoveCamera(CameraUpdateFactory.NewLatLngZoom(new LatLng(point.Latitude(), point.Longitude()), 15));

            var options = new SymbolOptions();

            options.WithIconImage("fire-station-11");
            options.WithGeometry(point);
            options.WithIconSize(new Float(4f))
            .WithDraggable(true);

            var symbol = symbolManager.Create(options);

            var options2 = new SymbolOptions()
                           .WithIconImage("fire-station-11")
                           .WithGeometry(point)
                           .WithIconSize(new Float(2f));

            var symbol2 = symbolManager.Create(options2);

            symbolManager.AddClickListener(this);
        }
        public void OnMapReady(MapboxMap map)
        {
            var uiSettings = map.UiSettings;

            uiSettings.SetAllGesturesEnabled(true);

            var marker = new MarkerOptions();

            marker.SetTitle("Los Angeles");
            marker.SetSnippet("City Hall");
            marker.SetPosition(LOS_ANGELES);

            map.AddMarker(marker);

            marker.SetTitle("New York");
            marker.SetSnippet("City Hall");
            marker.SetPosition(NEW_YORK);

            map.AddMarker(marker);

            var bounds = new LatLngBounds.Builder()
                         .Include(NEW_YORK)
                         .Include(LOS_ANGELES)
                         .Build();

            map.MoveCamera(CameraUpdateFactory.NewLatLngBounds(bounds, 8));
        }
        public void OnStyleLoaded(Style style)
        {
            this.style = style;
            MostrarUbicacionTiempoReal();
            FindViewById <View>(Resource.Id.fabStyles).Click += MainActivity_Click;
            mapboxMap.MoveCamera(CameraUpdateFactory.ZoomTo(2.0));
            markerViewManager = new MarkerViewManager(mapView, mapboxMap);

            createCustomMarker();
            //createRandomMarkers()

            //mapboxMap.addOnMapLongClickListener(this@MarkerViewActivity)
            //mapboxMap.addOnMapClickListener(this@MarkerViewActivity)
        }
        public void OnMapReady(MapboxMap map)
        {
            var uiSettings = map.UiSettings;

            uiSettings.SetAllGesturesEnabled(true);

            map.AddMarker(new MarkerOptions()
                          .SetTitle("Austin")
                          .SetSnippet("Texas, Baby")
                          .SetPosition(new LatLng(30.266461, -97.748630)));

            var bounds = new LatLngBounds.Builder()
                         .Include(new LatLng(30.266461, -97.748630))
                         .Include(new LatLng(30, -97))
                         .Build();

            map.InfoWindowClick += Map_InfoWindowClick;
            map.CameraChange    += Map_CameraChange;

            map.MoveCamera(CameraUpdateFactory.NewLatLngBounds(bounds, 8));
        }
Example #7
0
        public void SetupFunctions()
        {
            Element.TakeSnapshot           += TakeMapSnapshot;
            Element.GetFeaturesAroundPoint += GetFeaturesAroundPoint;

            Element.ResetPositionFunc = new Command(x =>
            {
                //map.ResetNorth();
                //map.AnimateCamera(CameraUpdateFactory.ZoomTo(Element.ZoomLevel));
            });


            Element.UpdateLayerFunc = (string layerId, bool isVisible, bool IsCustom) =>
            {
                if (!string.IsNullOrEmpty(layerId))
                {
                    string layerIdStr = IsCustom ? layerId.Prefix() : layerId;
                    var    layer      = map.GetLayer(layerIdStr);
                    if (layer != null)
                    {
                        layer.SetProperties(layer.Visibility,
                                            isVisible ? Sdk.Style.Layers.PropertyFactory.Visibility(Sdk.Style.Layers.Property.Visible)
                                            : Sdk.Style.Layers.PropertyFactory.Visibility(Sdk.Style.Layers.Property.None));

                        if (IsCustom && Element.MapStyle.CustomLayers != null)
                        {
                            var count = Element.MapStyle.CustomLayers.Count();
                            for (var i = 0; i < count; i++)
                            {
                                if (Element.MapStyle.CustomLayers.ElementAt(i).Id == layerId)
                                {
                                    Element.MapStyle.CustomLayers.ElementAt(i).IsVisible = isVisible;
                                    break;
                                }
                            }
                        }
                        return(true);
                    }
                }
                return(false);
            };

            Element.UpdateShapeOfSourceFunc = (Annotation annotation, string sourceId) =>
            {
                if (annotation != null && !string.IsNullOrEmpty(sourceId))
                {
                    var shape  = annotation.ToFeatureCollection();
                    var source = map.GetSource(sourceId.Prefix()) as Sdk.Style.Sources.GeoJsonSource;
                    if (source != null)
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            source.SetGeoJson(shape);
                        });
                        if (Element.MapStyle.CustomSources != null)
                        {
                            var count = Element.MapStyle.CustomSources.Count();
                            for (var i = 0; i < count; i++)
                            {
                                if (Element.MapStyle.CustomSources.ElementAt(i).Id == sourceId)
                                {
                                    Element.MapStyle.CustomSources.ElementAt(i).Shape = annotation;
                                    break;
                                }
                            }
                        }
                        return(true);
                    }
                }
                return(false);
            };

            Element.ReloadStyleFunc = new Command((obj) => {
                //https://github.com/mapbox/mapbox-gl-native/issues/9511
                map.SetStyleUrl(map.StyleUrl, null);
            });

            Element.UpdateViewPortAction = (Position centerLocation, double?zoomLevel, double?bearing, bool animated, Action completionHandler) => {
                var newPosition = new CameraPosition.Builder()
                                  .Bearing(bearing ?? map.CameraPosition.Bearing)
                                  .Target(centerLocation?.ToLatLng() ?? map.CameraPosition.Target)
                                  .Zoom(zoomLevel ?? map.CameraPosition.Zoom)
                                  .Build();
                var callback = completionHandler == null ? null : new CancelableCallback()
                {
                    FinishHandler = completionHandler,
                    CancelHandler = completionHandler
                };
                var update = CameraUpdateFactory.NewCameraPosition(newPosition);
                if (animated)
                {
                    map.AnimateCamera(update, callback);
                }
                else
                {
                    map.MoveCamera(update, callback);
                }
            };
        }
        public void SetupFunctions()
        {
            Element.TakeSnapshotFunc           += TakeMapSnapshot;
            Element.GetFeaturesAroundPointFunc += GetFeaturesAroundPoint;
            Element.ResetPositionAction         = () =>
            {
                //TODO handle reset position call
                //map.ResetNorth();
                //map.AnimateCamera(CameraUpdateFactory.ZoomTo(Element.ZoomLevel));
            };
            Element.UpdateLayerFunc = (string layerId, bool isVisible, bool IsCustom) =>
            {
                if (!string.IsNullOrEmpty(layerId))
                {
                    string layerIdStr = IsCustom ? layerId.Prefix() : layerId;
                    var    layer      = map.GetLayer(layerIdStr);
                    if (layer != null)
                    {
                        layer.SetProperties(layer.Visibility,
                                            isVisible ? Sdk.Style.Layers.PropertyFactory.Visibility(Sdk.Style.Layers.Property.Visible)
                                            : Sdk.Style.Layers.PropertyFactory.Visibility(Sdk.Style.Layers.Property.None));

                        if (IsCustom && Element.MapStyle.CustomLayers != null)
                        {
                            var count = Element.MapStyle.CustomLayers.Count();
                            for (var i = 0; i < count; i++)
                            {
                                if (Element.MapStyle.CustomLayers.ElementAt(i).Id == layerId)
                                {
                                    Element.MapStyle.CustomLayers.ElementAt(i).IsVisible = isVisible;
                                    break;
                                }
                            }
                        }
                        return(true);
                    }
                }
                return(false);
            };

            Element.UpdateShapeOfSourceFunc = (Annotation annotation, string sourceId) =>
            {
                if (annotation != null && !string.IsNullOrEmpty(sourceId))
                {
                    var shape  = annotation.ToFeatureCollection();
                    var source = map.GetSource(sourceId.Prefix()) as Sdk.Style.Sources.GeoJsonSource;
                    if (source != null)
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            source.SetGeoJson(shape);
                        });
                        if (Element.MapStyle.CustomSources?.FirstOrDefault((arg) => arg.Id == sourceId) is ShapeSource shapeSource)
                        {
                            shapeSource.Shape = annotation;
                        }
                        return(true);
                    }
                }
                return(false);
            };

            Element.ReloadStyleAction = () =>
            {
                //https://github.com/mapbox/mapbox-gl-native/issues/9511
                map.SetStyleUrl(map.StyleUrl, null);
            };

            Element.UpdateViewPortAction = (Position centerLocation, double?zoomLevel, double?bearing, bool animated, Action completionHandler) =>
            {
                var newPosition = new CameraPosition.Builder()
                                  .Bearing(bearing ?? map.CameraPosition.Bearing)
                                  .Target(centerLocation?.ToLatLng() ?? map.CameraPosition.Target)
                                  .Zoom(zoomLevel ?? map.CameraPosition.Zoom)
                                  .Build();
                var callback = completionHandler == null ? null : new CancelableCallback()
                {
                    FinishHandler = completionHandler,
                    CancelHandler = completionHandler
                };
                var update = CameraUpdateFactory.NewCameraPosition(newPosition);
                if (animated)
                {
                    map.AnimateCamera(update, callback);
                }
                else
                {
                    map.MoveCamera(update, callback);
                }
            };

            Element.GetStyleImageFunc += GetStyleImage;

            Element.GetStyleLayerFunc = GetStyleLayer;

            Element.SelectAnnotationAction = (Tuple <string, bool> obj) =>
            {
                if (obj == null || map == null || map.Annotations == null)
                {
                    return;
                }
                foreach (var item in map.Annotations)
                {
                    if (item is Marker marker && marker.Id.ToString() == obj.Item1)
                    {
                        map.SelectMarker(marker);
                    }
                }
            };

            Element.DeselectAnnotationAction = (Tuple <string, bool> obj) =>
            {
                if (obj == null || map == null || map.Annotations == null)
                {
                    return;
                }
                foreach (var item in map.Annotations)
                {
                    if (item is Marker marker && marker.Id.ToString() == obj.Item1)
                    {
                        map.DeselectMarker(marker);
                    }
                }
            };

            Element.ApplyOfflinePackFunc = (offlinePack) =>
            {
                //var region = offlinePack.Region;
                //OfflineTilePyramidRegionDefinition definition = new OfflineTilePyramidRegionDefinition(
                //    region.StyleURL,
                //    LatLngBounds.From(offlinePack.Region.Bounds.NorthEast.Lat, offlinePack.Region.Bounds.NorthEast.Long, offlinePack.Region.Bounds.SouthWest.Lat, offlinePack.Region.Bounds.SouthWest.Long),
                //    region.MinimumZoomLevel,
                //    region.MaximumZoomLevel,
                //    Android.App.Application.Context.Resources.DisplayMetrics.Density);
                //var xxx = new OfflineTilePyramidRegionDefinition(null);
                LatLngBounds   bounds         = LatLngBounds.From(offlinePack.Region.Bounds.NorthEast.Lat, offlinePack.Region.Bounds.NorthEast.Long, offlinePack.Region.Bounds.SouthWest.Lat, offlinePack.Region.Bounds.SouthWest.Long);
                double         regionZoom     = offlinePack.Region.MaximumZoomLevel;
                CameraPosition cameraPosition = new CameraPosition.Builder()
                                                .Target(bounds.Center)
                                                .Zoom(regionZoom)
                                                .Build();
                map.MoveCamera(CameraUpdateFactory.NewCameraPosition(cameraPosition));
                return(true);
            };
        }