Exemplo n.º 1
0
        public void UpdateAllItems(GLMapInfo[] maps)
        {
            if (maps == null)
            {
                return;
            }

            GLMapManager.SortMaps(maps, center);
            ListView listView = (ListView)FindViewById(Android.Resource.Id.List);

            listView.Adapter    = new MapsAdapter(maps, this, localeSettings);
            listView.ItemClick += (object sender, ItemClickEventArgs e) => {
                GLMapInfo info = (GLMapInfo)listView.Adapter.GetItem(e.Position);
                if (info.IsCollection)
                {
                    Intent intent = new Intent(this, typeof(DownloadActivity));
                    intent.PutExtra("collectionID", info.MapID);
                    intent.PutExtra("cx", center.X);
                    intent.PutExtra("cy", center.Y);
                    StartActivity(intent);
                }
                else
                {
                    GLMapDownloadTask task = GLMapManager.GetDownloadTask(info);
                    if (task != null)
                    {
                        task.Cancel();
                    }
                    else if (info.State != GLMapInfoState.Downloaded)
                    {
                        GLMapManager.CreateDownloadTask(info, this).Start();
                    }
                }
            };

            listView.ItemLongClick += (object sender, ItemLongClickEventArgs e) => {
                GLMapInfo      info  = ((MapsAdapter)this.listView.Adapter).Maps[e.Position];
                GLMapInfoState state = info.State;
                if (state == GLMapInfoState.Downloaded || state == GLMapInfoState.NeedResume || state == GLMapInfoState.NeedUpdate)
                {
                    selectedMap = info;
                    //return false;
                }
                else
                {
                    //return true;
                }
            };
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here
            SetContentView(Resource.Layout.map);
            mapView = (GLMapView)FindViewById(Resource.Id.map_view);

            // Map list is updated, because download button depends on available map list and during first launch this list is empty
            GLMapManager.UpdateMapList(this, null);

            btnDownloadMap        = (Button)this.FindViewById(Resource.Id.button_dl_map);
            btnDownloadMap.Click += (object sender, EventArgs e) =>
            {
                if (mapToDownload != null)
                {
                    GLMapDownloadTask task = GLMapManager.GetDownloadTask(mapToDownload);
                    if (task != null)
                    {
                        task.Cancel();
                    }
                    else
                    {
                        GLMapManager.CreateDownloadTask(mapToDownload, this).Start();
                    }
                    updateMapDownloadButtonText();
                }
                else
                {
                    Intent i = new Intent(Application.Context, typeof(DownloadActivity));

                    MapPoint pt = mapView.MapCenter;
                    i.PutExtra("cx", pt.X);
                    i.PutExtra("cy", pt.Y);
                    Application.Context.StartActivity(i);
                }
            };

            GLMapManager.AddStateListener(this);

            localeSettings         = new GLMapLocaleSettings();
            mapView.LocaleSettings = localeSettings;
            mapView.LoadStyle(Assets, "DefaultStyle.bundle");
            mapView.SetUserLocationImages(mapView.ImageManager.Open("DefaultStyle.bundle/circle-new.svgpb", 1, 0), mapView.ImageManager.Open("DefaultStyle.bundle/arrow-new.svgpb", 1, 0));

            mapView.SetScaleRulerStyle(GLUnits.SI, GLMapPlacement.BottomCenter, new MapPoint(10, 10), 200);
            mapView.SetAttributionPosition(GLMapPlacement.TopCenter);

            CheckAndRequestLocationPermission();

            Bundle  b       = Intent.Extras;
            Samples example = (Samples)b.GetInt("example");

            switch (example)
            {
            case Samples.MAP_EMBEDD:
                if (!GLMapManager.AddMap(Assets, "Montenegro.vm", null))
                {
                    //Failed to unpack to caches. Check free space.
                }
                zoomToPoint();
                break;

            case Samples.MAP_ONLINE:
                GLMapManager.SetAllowedTileDownload(true);
                break;

            case Samples.MAP_ONLINE_RASTER:
                mapView.RasterTileSources = new GLMapRasterTileSource[] { new OSMTileSource(this) };
                break;

            case Samples.ZOOM_BBOX:
                zoomToBBox();
                break;

            case Samples.FLY_TO:
            {
                mapView.SetMapCenter(MapPoint.CreateFromGeoCoordinates(37.3257, -122.0353), false);
                mapView.SetMapZoom(14, false);

                Button btn = (Button)FindViewById(Resource.Id.button_action);
                btn.Visibility = ViewStates.Visible;
                btn.Text       = "Fly";
                btn.Click     += (object sender, EventArgs e) =>
                {
                    double min_lat = 33;
                    double max_lat = 48;
                    double min_lon = -118;
                    double max_lon = -85;

                    double lat = min_lat + (max_lat - min_lat) * new Random(1).NextDouble();
                    double lon = min_lon + (max_lon - min_lon) * new Random(2).NextDouble();

                    MapGeoPoint geoPoint = new MapGeoPoint(lat, lon);

                    mapView.FlyTo(geoPoint, 15, 0, 0);
                };
                GLMapManager.SetAllowedTileDownload(true);
                break;
            }

            case Samples.OFFLINE_SEARCH:
                GLMapManager.AddMap(Assets, "Montenegro.vm", null);
                zoomToPoint();
                offlineSearch();
                break;

            case Samples.MARKERS:
                mapView.LongClickable = true;

                gestureDetector = new GestureDetector(this, new SimpleOnGestureListenerAnonymousInnerClassHelper(this));

                mapView.SetOnTouchListener(new TouchListener(gestureDetector));

                addMarkers();
                GLMapManager.SetAllowedTileDownload(true);
                break;

            case Samples.MARKERS_MAPCSS:
                addMarkersWithMapcss();

                gestureDetector = new GestureDetector(this, new SimpleOnGestureListenerAnonymousInnerClassHelper2(this));

                mapView.SetOnTouchListener(new TouchListener(gestureDetector));

                GLMapManager.SetAllowedTileDownload(true);
                break;

            case Samples.MULTILINE:
                addMultiline();
                break;

            case Samples.POLYGON:
                addPolygon();
                break;

            case Samples.CAPTURE_SCREEN:
                zoomToPoint();
                captureScreen();
                break;

            case Samples.IMAGE_SINGLE:
            {
                Button btn = (Button)this.FindViewById(Resource.Id.button_action);
                btn.Visibility = ViewStates.Visible;
                delImage(btn, null);
                break;
            }

            case Samples.IMAGE_MULTI:
                mapView.LongClickable = true;

                gestureDetector = new GestureDetector(this, new SimpleOnGestureListenerAnonymousInnerClassHelper3(this));

                mapView.SetOnTouchListener(new TouchListener(gestureDetector));
                break;

            case Samples.GEO_JSON:
                loadGeoJSON();
                break;

            case Samples.STYLE_LIVE_RELOAD:
                styleLiveReload();
                break;
            }

            mapView.SetCenterTileStateChangedCallback(() =>
            {
                RunOnUiThread(() =>
                {
                    updateMapDownloadButton();
                });
            });

            mapView.SetMapDidMoveCallback(() =>
            {
                if (example == Samples.CALLBACK_TEST)
                {
                    Log.Warn("GLMapView", "Did move");
                }
                RunOnUiThread(() =>
                {
                    updateMapDownloadButtonText();
                });
            });
        }