コード例 #1
0
        //Implements the IOnMapReadyCallback interface
        public void OnMapReady(MapboxMap map)
        {
            mapboxMap = map;

            // This will refer to OnStyleLoaded
            map.SetStyle(Style.MapboxStreets, this);
            var title1 = "Chicago";
            var lat1   = 41.897627;
            var lng1   = -87.643526;
            var title2 = "St.Louis";
            var lat2   = 38.742291;
            var lng2   = -90.064928;

            pickupPoint   = new LatLng(lat1, lng1);
            delivertPoint = new LatLng(lat2, lng2);

            bool crossesMeridianOrDateline;

            if ((lng1 > 0.0 && lng2 < 0.0) || (lng1 < 0.0 && lng2 > 0.0))
            {
                crossesMeridianOrDateline = true;
            }
            else
            {
                crossesMeridianOrDateline = false;
            }

            var xDiffPrimeMeridianZero = FindXDiffPrimeMeridianZero(lng1, lng2);
            var xDiffDatelineZero      = FindXDiffDatelineZero(lng1, lng2);

            List <double> xList;
            List <double> lngList;

            if ((Math.Abs(xDiffDatelineZero) > Math.Abs(xDiffPrimeMeridianZero)) && crossesMeridianOrDateline == true)
            {
                var latCenter = (lat1 + lat2) / 2.0;

                // Find center coordinate here
                var    x1      = FindXPrimeMeridianZero(lng1);
                var    xCenter = x1 + (xDiffPrimeMeridianZero / 2.0);
                double lngCenter;

                if (x1 > 180.0)
                {
                    lngCenter = xCenter - 360.0;
                }
                else
                {
                    lngCenter = xCenter;
                }

                var cameraCenter = new CameraPosition.Builder().Target(new LatLng(latCenter, lngCenter)).Zoom(1.5).Build();

                map.AnimateCamera(CameraUpdateFactory.NewCameraPosition(cameraCenter));

                xList   = CreateXList(xDiffPrimeMeridianZero, x1);
                lngList = ConvertToLngsPrimeMeridianZero(xList);
            }
            else
            {
                // find max/min lat/lng here
                // use those to determine the initialBounds

                var latDict = FindLowHigh(lat1, lat2);
                var latHigh = latDict["high"];
                var latLow  = latDict["low"];
                var lngDict = FindLowHigh(lng1, lng2);
                var lngHigh = lngDict["high"];
                var lngLow  = lngDict["low"];

                var latBuffer = (latHigh - latLow) / 4.0;
                var lngBuffer = (lngHigh - lngLow) / 10.0;

                var swLat = latLow - latBuffer;
                var swLng = lngLow - lngBuffer;
                var neLat = latHigh + latBuffer;
                var neLng = lngHigh + lngBuffer;

                var swPoint = new LatLng(swLat, swLng);
                var nePoint = new LatLng(neLat, neLng);

                var latLngBounds = new LatLngBounds.Builder()
                                   .Include(nePoint) // Northeast
                                   .Include(swPoint) // Southwest
                                   .Build();

                mapboxMap.EaseCamera(CameraUpdateFactory.NewLatLngBounds(latLngBounds, 0));

                xList   = CreateXList(xDiffDatelineZero, FindXDatelineZero(lng1));
                lngList = ConvertToLngsDatelineZero(xList);
            }

            var yList = CreateYList(lat1, lat2);

            var latList = ConvertToLats(yList);
            var coords  = ConvertToCoords(latList, lngList);

            CreateDashedLine(coords);

            var marker1 = new MarkerOptions();
            var marker2 = new MarkerOptions();

            marker1.SetPosition(pickupPoint);
            marker1.SetTitle(title1);
            marker2.SetPosition(delivertPoint);
            marker2.SetTitle(title2);
            map.AddMarker(marker1);
            map.AddMarker(marker2);
        }