Пример #1
0
 private void DrawRoute(ServerResponse serverResponse)
 {
     // Remove current route.
     if (_mapRoute != null)
     {
         _mapRoute.Remove();
     }
     // Create new route and add it to map.
     _mapRoute = new MapRoute(_map, serverResponse.response);
 }
Пример #2
0
        public MapRoute(GoogleMap map, ServerResponse.ResponseRoute[] responseRoutes)
        {
            var numberOfRoutes = responseRoutes.Length * 2 - 1;
            _map = map;
            _mapRoutes = new List<Polyline>();
            _mapStations = new List<Marker>();
            var l = 1;
            for (var i = 0; i < numberOfRoutes; i++)
            {
                // Create new partial route.
                if (i % 2 == 0)
                {
                    // Choose color;
                    Color color = Color.DodgerBlue;

                    // Create polyline.
                    var polyline = new PolylineOptions();
                    polyline.InvokeWidth(4f);
                    polyline.InvokeColor(color);

                    // Add points to polyline.
                    int k = i / 2;
                    ServerResponse.ResponseRoute responseRoute = responseRoutes[k];
                    for (var j = 0; j < responseRoute.pointsId.Length; j++)
                    {
                        string stationId = responseRoute.pointsId[j];
                        Station station = App.Database.GetStationById(stationId);
                        if (station != null && station.latitude != 0f && station.longitude != 0f)
                        {
                            var latlng = new Android.Gms.Maps.Model.LatLng(station.latitude, station.longitude);
                            polyline.Add(latlng);
                            // Create marker.
                            var marker = new MarkerOptions();
                            marker.SetPosition(latlng);
                            marker.SetTitle(l++ + ". " + station.postName);
                            marker.Draggable(false);
                            marker.SetSnippet("ul. " + station.street + "\n"+ "linia: " + responseRoute.variantId);
                            _mapStations.Add(_map.AddMarker(marker));
                        }
                    }

                    // Add polyline to map.
                    _mapRoutes.Add(_map.AddPolyline(polyline));
                }
                // Create connection between two routes.
                else
                {
                    // Choose color;
                    Color color = Color.LightSkyBlue;

                    // Create polyline.
                    PolylineOptions polyline = new PolylineOptions();
                    polyline.InvokeWidth(3f);
                    polyline.InvokeColor(color);

                    // Add points to polyline (closest, non 0f points).
                    // Last point from previous route.
                    int k = i / 2;
                    for (var j = 0; j < responseRoutes[k].pointsId.Length; j++)
                    {
                        string prevStationId = responseRoutes[k].pointsId[responseRoutes[k].pointsId.Length - 1 - j];
                        Station prevStation = App.Database.GetStationById(prevStationId);
                        if (prevStation != null && prevStation.latitude != 0f && prevStation.longitude != 0f)
                        {
                            polyline.Add(new Android.Gms.Maps.Model.LatLng(prevStation.latitude, prevStation.longitude));
                            break;
                        }
                    }
                    // 1st point from next route.
                    k++;
                    for (var j = 0; j < responseRoutes[k].pointsId.Length; j++)
                    {
                        string nextStationId = responseRoutes[k].pointsId[j];
                        Station nextStation = App.Database.GetStationById(nextStationId);
                        if (nextStation != null && nextStation.latitude != 0f && nextStation.longitude != 0f)
                        {
                            polyline.Add(new Android.Gms.Maps.Model.LatLng(nextStation.latitude, nextStation.longitude));
                            break;
                        }
                    }

                    // Add polyline to map.
                    _mapRoutes.Add(_map.AddPolyline(polyline));
                }
            }
        }