public MapRoute(GoogleMap map, List<Station> stations) { _map = map; _mapRoutes = new List<Polyline>(); _mapStations = new List<Marker>(); // Choose color; Color color = Color.DodgerBlue; // Create polyline. var polyline = new PolylineOptions(); polyline.InvokeWidth(4f); polyline.InvokeColor(color); for (var i = 0; i < stations.Count; i++) { // Add points to polyline. var station = stations[i]; 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((i + 1) + ". " + station.postName); marker.Draggable(false); marker.SetSnippet("ul. " + station.street); _mapStations.Add(_map.AddMarker(marker)); } } // Add polyline to map. _mapRoutes.Add(_map.AddPolyline(polyline)); }
protected override void DrawPolyline (List<GeoLoc> polyLine) { if (polyLine == null || polyLine.Count == 0) return; var map = _mapFragment.Map; // draw routes PolylineOptions options = new PolylineOptions ().Geodesic (true); if(polyLine == _highlighedPoint.Polyline){ options.InvokeWidth (7); options.InvokeColor (System.Drawing.Color.Blue.ToArgb()); } else { options.InvokeWidth (3); options.InvokeColor (System.Drawing.Color.LightBlue.ToArgb()); } polyLine.ForEach (x => options.Add (Utils.GetLatLng(x))); _routeLines.Add(map.AddPolyline(options)); }
private PolylineOptions CreatePolylineOptions(MapPolyline polyline) { var op = new PolylineOptions(); op.InvokeColor(polyline.Color.ToAndroid().ToArgb()); op.InvokeWidth((float)polyline.Width); op.InvokeZIndex(polyline.ZIndex); return op; }
/// <summary> /// Calculates and adds the route to the map /// </summary> /// <param name="route">The route to add</param> private async void AddRoute(TKRoute route) { route.PropertyChanged += OnRoutePropertyChanged; GmsDirectionResult routeData = null; try { routeData = await GmsDirection.Instance.CalculateRoute(route.Source, route.Destination, route.TravelMode.ToGmsTravelMode()); if (routeData == null || routeData.Routes == null) return; var r = routeData.Routes.FirstOrDefault(); if (r == null || r.Polyline.Positions == null || !r.Polyline.Positions.Any()) return; this.SetRouteData(route, r); var routeOptions = new PolylineOptions(); if (route.Color != Color.Default) { routeOptions.InvokeColor(route.Color.ToAndroid().ToArgb()); } if (route.LineWidth > 0) { routeOptions.InvokeWidth(route.LineWidth); } routeOptions.Add(r.Polyline.Positions.Select(i => i.ToLatLng()).ToArray()); this._routes.Add(route, this._googleMap.AddPolyline(routeOptions)); if (this.FormsMap.RouteCalculationFinishedCommand != null && this.FormsMap.RouteCalculationFinishedCommand.CanExecute(route)) { this.FormsMap.RouteCalculationFinishedCommand.Execute(route); } } finally { if ((routeData == null || routeData.Status != GmsDirectionResultStatus.Ok) && this.FormsMap.RouteCalculationFailedCommand != null) { if (this.FormsMap.RouteCalculationFailedCommand.CanExecute(route)) { this.FormsMap.RouteCalculationFailedCommand.Execute(route); } } } }
/// <summary> /// Adds a route to the map /// </summary> /// <param name="line">The route to add</param> private void AddLine(TKPolyline line) { line.PropertyChanged += OnLinePropertyChanged; var polylineOptions = new PolylineOptions(); if (line.Color != Color.Default) { polylineOptions.InvokeColor(line.Color.ToAndroid().ToArgb()); } if (line.LineWidth > 0) { polylineOptions.InvokeWidth(line.LineWidth); } if (line.LineCoordinates != null) { polylineOptions.Add(line.LineCoordinates.Select(i => i.ToLatLng()).ToArray()); } this._polylines.Add(line, this._googleMap.AddPolyline(polylineOptions)); }
private void SetupMapIfNeeded() { if (_map == null) { _map = _mapFragment.Map; if (_map != null) { var centerPoint = new LatLng (Station.TroncalRouteCenter.Y, Station.TroncalRouteCenter.X); Activity.RunOnUiThread (delegate{ _map.AnimateCamera(CameraUpdateFactory.NewLatLngZoom(centerPoint,15.0f)); _map.MyLocationEnabled = true; }); stations = stationRepository.Stations; Activity.RunOnUiThread (delegate{ foreach(var station in stations){ var location = new LatLng(station.Latitude, station.Longitude); var stationImageId = Resources.GetIdentifier( station.ImageFilename().ToLower(), "drawable", Activity.PackageName); var originalStationBitmap = BitmapFactory.DecodeResource(Resources, stationImageId); var scale = Resources.DisplayMetrics.Density; var pixels = 35; var stationBitmap = Bitmap.CreateScaledBitmap(originalStationBitmap, (int) (pixels * scale + 0.5f), (int) (pixels * scale + 0.5f), false); MarkerOptions markerOptions = new MarkerOptions() .SetPosition(location) .InvokeIcon(BitmapDescriptorFactory.FromBitmap(stationBitmap)) .SetTitle(station.Name); _map.AddMarker(markerOptions); } var polylineOptions = new PolylineOptions(); polylineOptions.InvokeWidth(5.0f); polylineOptions.InvokeColor(Color.Black); foreach(var point in Station.TroncalRoutePath){ polylineOptions.Add(new LatLng(point.Y, point.X)); } _map.AddPolyline(polylineOptions); }); } } }
protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); // Set our view from the "main" layout resource SetContentView (Resource.Layout.Main); Inicializa_MapFragment (); //Definimos las ciudades en las cuales pondremos nuestros marcadores en el mapa MisCiudades.Add ( new Ciudad_Marca ("MÉXICO D.F.","En su forma abreviada México, D. F., es la capital y sede de los poderes federales de los Estados Unidos Mexicanos.",new LatLng (19.430334, -99.13599),"p1")); MisCiudades.Add ( new Ciudad_Marca ("LEÓN","Es una ciudad mexicana localizada en el Estado de Guanajuato,México, en la región del Bajío.",new LatLng ( 21.12806,-101.689163),"p2")); MisCiudades.Add ( new Ciudad_Marca ("LAGOS DE MORENO"," Es un municipio y la ciudad más grande y poblada de la Región denominada Los Altos de Jalisco, ubicados en el estado mexicano de Jalisco.",new LatLng ( 21.36453,-101.939076),"p3")); MisCiudades.Add ( new Ciudad_Marca ("GUADALAJARA","es una ciudad mexicana, capital del estado de Jalisco, así como principal localidad del área urbana denominada Zona Metropolitana de Guadalajara.",new LatLng ( 20.663626,-103.343754),"p4")); CargamosMarcadores (); Button btnLinea = FindViewById<Button> (Resource.Id.btnLinea); Button btnCirculo = FindViewById<Button> (Resource.Id.btnCirculo); Button btnPoligono = FindViewById<Button> (Resource.Id.btnPoligono); txtPosicion=(TextView) FindViewById (Resource.Id.txtPosicion); btnLinea.Click += delegate { PolylineOptions rectOptions = new PolylineOptions(); if (mMap != null) { foreach (Ciudad_Marca ciudad in MisCiudades) { rectOptions.Add (ciudad.Ubicacion ); } rectOptions.InvokeColor(Color.Red.ToArgb()); rectOptions.InvokeWidth(25); rectOptions.Geodesic(true) ; mMap.AddPolyline(rectOptions); } }; btnCirculo.Click += delegate { if (mMap != null) { foreach (Ciudad_Marca ciudad in MisCiudades) { CircleOptions circleOptions = new CircleOptions(); circleOptions.InvokeCenter(ciudad.Ubicacion); circleOptions.InvokeRadius(10000); circleOptions.InvokeStrokeWidth(10); circleOptions.InvokeStrokeColor(Color.Blue.ToArgb() ); Color micolorfill=Color.FromArgb(150,149,153,225); circleOptions.InvokeFillColor(micolorfill.ToArgb() ); mMap.AddCircle(circleOptions); } } }; btnPoligono.Click += delegate { if (mMap != null) { PolygonOptions Poligono = new PolygonOptions (); foreach (Ciudad_Marca ciudad in MisCiudades) { Poligono.Add (ciudad.Ubicacion); } Poligono.InvokeStrokeWidth(10); Poligono.InvokeStrokeColor(Color.Green.ToArgb ()); Color micolorfill=Color.FromArgb(150,0,255,51); Poligono.InvokeFillColor(micolorfill.ToArgb() ); mMap.AddPolygon (Poligono); } }; }
public Task <List<PolylineOptions>> Filling (string data) { return Task.Run(() => { var polyLineOptions = new PolylineOptions (); var ListPolylineOptions = new List<PolylineOptions>(); var jsonData = ""; jsonData = data; var jObject = new JSONObject (jsonData); var parser = new PathJsonParser (); var ListCoords = parser.parse (jObject); if(ListCoords.Count > 0){ for (int j = 0; j < ListCoords.Count; j++) { var listCoord = ListCoords[j]; foreach(var list in listCoord) { polyLineOptions.Add (list); } polyLineOptions.InvokeWidth (5); polyLineOptions.InvokeColor (Color.Red); } ListPolylineOptions.Add(polyLineOptions); } return ListPolylineOptions; }); }
public void HandleDirectionPointsResult(List<LatLng> directionPoints, LatLng startLocation, LatLng endLocation) { var line = new PolylineOptions(); line.InvokeWidth(8); line.InvokeColor(global::Android.Graphics.Color.Blue); int i = 0; foreach (var point in directionPoints) { line.Add(point); } if (mMap != null) { CameraPosition.Builder builder = CameraPosition.InvokeBuilder(); builder.Target(startLocation); builder.Zoom(10); builder.Bearing(0); builder.Tilt(0); CameraPosition cameraPosition = builder.Build(); CameraUpdate cameraUpdate = CameraUpdateFactory.NewCameraPosition(cameraPosition); this.RunOnUiThread(() => { mMap.Clear(); // Uses a colored icon. mMap.AddMarker(new MarkerOptions() .SetPosition(startLocation) .SetTitle(GeoStartAddress) .SetSnippet("Population: Unknown") .InvokeIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueRed))); mMap.AddMarker(new MarkerOptions() .SetPosition(endLocation) .SetTitle(GeoEndAddress) .SetSnippet("Population: Unknown") .InvokeIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueRed))); var img = BitmapDescriptorFactory.FromResource (Resource.Drawable.badge_victoria); var groundOverlayOptions1 = new GroundOverlayOptions () .Position (startLocation, 1600, 1600) .InvokeImage (img); var mapOverlay1 = mMap.AddGroundOverlay(groundOverlayOptions1); //Randomly adding Map OVerlays on the map. foreach (var point in directionPoints) { i++; if (i > 520) { i = 0; var image = BitmapDescriptorFactory.FromResource (Resource.Drawable.balloon_overlay_focused); var groundOverlayOptions = new GroundOverlayOptions () .Position (point, 2000, 2000) .InvokeImage (image); var mapOverlay = mMap.AddGroundOverlay(groundOverlayOptions); var checadd = mapOverlay; } } mMap.AnimateCamera(cameraUpdate); var drawline = mMap.AddPolyline(line); }); } // return 1; }
/// <summary> /// Draw line from active location to active object, if it is a zone. /// </summary> void UpdateDistanceLine () { if (activeObject != null && activeObject is Zone) { if (activeObject is Zone && ((Zone)activeObject).State != PlayerZoneState.Inside) { if (distanceLine == null) { // Draw line PolylineOptions po = new PolylineOptions(); po.Points.Add (new LatLng (Main.GPS.Location.Latitude, Main.GPS.Location.Longitude)); po.Points.Add (new LatLng (((Zone)activeObject).ObjectLocation.Latitude, ((Zone)activeObject).ObjectLocation.Longitude)); //.ObjectLocation.Latitude, ((Zone)activeObject).ObjectLocation.Longitude)); po.InvokeColor(Color.Cyan); po.InvokeWidth(4); po.InvokeZIndex(2); distanceLine = _map.AddPolyline(po); } else { // Set new line points List<LatLng> points = new List<LatLng>(2); points.Add (new LatLng (Main.GPS.Location.Latitude, Main.GPS.Location.Longitude)); points.Add (new LatLng (((Zone)activeObject).ObjectLocation.Latitude, ((Zone)activeObject).ObjectLocation.Longitude)); //.ObjectLocation.Latitude, ((Zone)activeObject).ObjectLocation.Longitude)); distanceLine.Points = points; } } else { // Delete line if (distanceLine != null) { distanceLine.Remove (); distanceLine = null; } } } else { // Delete line if (distanceLine != null) { distanceLine.Remove (); distanceLine = null; } } }
private static PolylineOptions CloneOptions(PolylineOptions op) { var options = new PolylineOptions(); options.InvokeColor(op.Color); options.InvokeWidth(op.Width); options.InvokeZIndex(op.ZIndex); return options; }
//void MapOnCircleClick(object sender, GoogleMap.CircleClickEventArgs eventArgs) //{ // // clicked polygon // var clickedCircle = eventArgs.Circle; // // lookup pin // Circle targetCircle = null; // for (var i = 0; i < Map.Circles.Count; i++) // { // var circle = Map.Circles[i]; // if ((string)circle.Id != clickedCircle.Id) // continue; // targetCircle = circle; // break; // } // // only consider event handled if a handler is present. // // Else allow default behavior of displaying an info window. // targetCircle?.SendTap(); //} void AddPolylines(IList polylines) { var map = NativeMap; if (map == null) return; if (_polylines == null) _polylines = new List<APolyline>(); _polylines.AddRange(polylines.Cast<Polyline>().Select(line => { var polyline = (Polyline)line; var opts = new PolylineOptions(); foreach (var p in polyline.Positions) opts.Add(new LatLng(p.Latitude, p.Longitude)); opts.InvokeWidth(polyline.StrokeWidth * _scaledDensity); // TODO: convert from px to pt. Is this collect? (looks like same iOS Maps) opts.InvokeColor(polyline.StrokeColor.ToAndroid()); opts.Clickable(polyline.IsClickable); var nativePolyline = map.AddPolyline(opts); // associate pin with marker for later lookup in event handlers polyline.Id = nativePolyline; return nativePolyline; })); }
public Task<DirectionHolder> GetDirectionsAsync() { if (directionsTCS != null) { if (!directionsTCS.Task.IsCompleted) directionsTCS.TrySetCanceled(); directionsTCS = new TaskCompletionSource<DirectionHolder>(); } if (_lastUserLocation != null && currentMarkerPosition != null) { Task.Run(async () => { try { // Getting URL to the Google Directions API var url = GetDirectionsUrl(_lastUserLocation, currentMarkerPosition); using (var client = new HttpClient(new NativeMessageHandler())) { var response = await client.GetAsync(url).ConfigureAwait(false); var responseBodyAsText = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var directions = JsonConvert.DeserializeObject<DirectionsModel>(responseBodyAsText); PolylineOptions lineOptions = new PolylineOptions(); lineOptions = lineOptions.InvokeColor(Resources.GetColor(Resource.Color.accent).ToArgb()); lineOptions = lineOptions.InvokeWidth(15); if (directions.routes.FirstOrDefault() != null) { var duration = directions.routes.FirstOrDefault().legs.FirstOrDefault().duration; var distance = directions.routes.FirstOrDefault().legs.FirstOrDefault().distance; var points = MapHelper.DecodePolyline(directions.routes.FirstOrDefault().overview_polyline.points).AsEnumerable(); foreach (var point in points) { lineOptions.Add(point); } directionsTCS.SetResult(new DirectionHolder { Distance = distance.text, Duration = duration.text, Polylines = lineOptions }); } } } catch { } }); } return directionsTCS.Task; }
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)); } } }