Search code examples
c#xamarinxamarin.androidencapsulation

Cannot pass variable to object in c# due to protection level


I've got this code, and trying to pass two parameters to LatLng (if hardcoded it works fine), but keep getting is inaccessible due to its protection level - on var location = new LatLng...

What is wrong with my encapsulation?

namespace SomeApp
{
    [Activity (Label = "App")]          
    public class MapActivity : Activity
    {
        protected override void OnCreate (Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);
            SetContentView(Resource.Layout.MapLayout);

            MapLocationSetup ();

        }

        public void MapLocationSetup () {

            string currentLocationLatitude = Intent.GetStringExtra ("currentLocationLatitude");
            string currentLocationLongitude = Intent.GetStringExtra ("currentLocationLongitude");

            var location = new LatLng(currentLocationLatitude, currentLocationLongitude);
            CameraPosition.Builder builder = CameraPosition.InvokeBuilder();
            builder.Target(location);
            builder.Zoom(18);
            builder.Bearing(155);
            builder.Tilt(85);
            CameraPosition cameraPosition = builder.Build();
            CameraUpdate cameraUpdate = CameraUpdateFactory.NewCameraPosition(cameraPosition);

            MapFragment mapFrag = (MapFragment) FragmentManager.FindFragmentById(Resource.Id.mapLayout);
            GoogleMap map = mapFrag.Map;
            if (map != null) {
                map.MoveCamera(cameraUpdate);
                //map.MapType = GoogleMap.MapTypeSatellite;
            }
        }
    }
}

Solution

  • Android's LatLng construtor expects a double, and you are passing a string. This should give you a typecast error, not a protection level error, but that appears to be the basic problem.

    string currentLocationLatitude = Intent.GetStringExtra ("currentLocationLatitude");
    string currentLocationLongitude = Intent.GetStringExtra ("currentLocationLongitude");
    
    double lat = Double.Parse(currentLocationLatitude);
    double lng = Double.Parse(currentLocationLongitude);
    
    var location = new LatLng(lat, lng);