ThinkGeo.com    |     Documentation    |     Premium Support

How to use CenterAt()

Hi,

I’m trying to center the map at one point using mapView.CenterAt (location). However, I’m getting the following message:

System.ArgumentOutOfRangeException: The input double value is out of range.
Parameter name: screenWidth

Also, my mapview has width and height equal to 0.

can anybody help me?

Hi Marco,

Could you please let me know your scenario? Because it looks your map is invisible and you want to set the center point.

And in fact I hadn’t reproduced the exception by the test code as below, I think maybe you can directly set the CurrentExtent instead of use center at.

 public ActionResult DisplayASimpleMap()
    {
        Map map = new Map("Map1",
            new System.Web.UI.WebControls.Unit(0, System.Web.UI.WebControls.UnitType.Pixel),
            new System.Web.UI.WebControls.Unit(0, System.Web.UI.WebControls.UnitType.Pixel));
                   

        map.MapUnit = GeographyUnit.DecimalDegree;            
        map.CenterAt(0, 0);

        map.MapBackground = new BackgroundLayer(new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF")));
        map.CustomOverlays.Add(new WorldMapKitWmsWebOverlay());

        return View(map);
    }

Regards,

Don

Hi Don, how are you?

Thanks for answering.

I’m following the sample code I found on the wiki. Currently my code looks like this:

public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);

            string targetDirectory = (@"/mnt/sdcard/Android.Sample/GetStarted/");
            CopySampleData(targetDirectory);

            MapView mapView = FindViewById<MapView>(Resource.Id.MapView);
            mapView.MapUnit = GeographyUnit.DecimalDegree;

            ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(Path.Combine(targetDirectory, "cntry02.shp"));
            worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
            worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

            ShapeFileFeatureLayer eixoNicolas = new ShapeFileFeatureLayer(Path.Combine(targetDirectory + "eixo_nicolas.SHP"));
            eixoNicolas.RequireIndex = false;
            eixoNicolas.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.CreateSimpleLineStyle(GeoColor.SimpleColors.Green, 5, true);
            eixoNicolas.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

            LayerOverlay overlay = new LayerOverlay();
            overlay.Opacity = 0.8;
            overlay.Layers.Add(worldLayer);
            overlay.Layers.Add(eixoNicolas);

            mapView.Overlays.Add("EixoNicolas", overlay);

            mapView.CurrentExtent = new RectangleShape(5, 78, 30, 26);
        }

private void CopySampleData(string targetDirectory)
        {
            if (!Directory.Exists(targetDirectory))
            {
                Directory.CreateDirectory(targetDirectory);
                foreach (string filename in Assets.List("AppData"))
                {
                    Stream stream = Assets.Open("AppData/" + filename);
                    FileStream fileStream = File.Create(Path.Combine(targetDirectory, filename));
                    stream.CopyTo(fileStream);
                    fileStream.Close();
                    stream.Close();
                }
            }
        }

Is it possible to center the map according to real-world coordinates?

Regards.

Hi Marco,

When I reply you last time, the post hadn’t shows it’s an Android question, so I reply you the code is our other product.

Because I hadn’t the environment today for Android, so maybe I will test that this Friday.

And if you want to find the correct value to set mapView.CurrentExtent, I think the code as below should be helpful.

        double scale = mapView.CurrentScale;
        PointShape point = new PointShape(0, 0); // Put your point here
        RectangleShape rect = new RectangleShape(point.X - 0.5, point.Y + 0.5, point.X + 0.5, point.Y - 0.5);

        // Please modify the parameter to make it works
        RectangleShape resultRectangle = ExtentHelper.ZoomToScale(scale, rect, mapView.MapUnit, mapView.Width, mapView.Height);
        mapView.CurrentExtent = resultRectangle;

Regards,

Don

Hi Don,

I await your response.

I tried to use the code from your last reply and what I got was:

Unhandled Exception:
System.ArgumentOutOfRangeException: The input double value is out of range.
Parameter name: screenWidth

What can I do?

Thanks,

Marco

Hi Marco,

Sorry that I was unable to recreate the problem on my end, also I tried the samples shown at http://wiki.thinkgeo.com/wiki/map_suite_android_edition#learning_samples, but seems like all works fine, would you please provide your ready-to-go sample for me ?

Thanks,
Johnny

Hi Johnny, thanks for answering.

Sorry, I can’t.

OK, Marco, I will try creating a demo to show how to call CenterAt and update it to you tomorrow. Now I’m out of office.

Regards,
Johnny

Marco,

see demo attached,

helloworld_android.zip (1.5 MB)

2 options mentioned in the code, just like below:

  1. Option 1: change the currentExtent to accord the map to a real-world coordinate.
    mapView.CurrentExtent = new RectangleShape(5, 78, 30, 26);

  2. Option 2: use Center and Scale
    RunOnUiThread(() =>
    {
    ZoomLevelSet decimalDegreeZoomLevelSet = new ZoomLevelSet();
    PointShape center = new PointShape(30, 26);
    mapView.ZoomTo(center, decimalDegreeZoomLevelSet.ZoomLevel08.Scale);
    });

Thanks,
Johnny