public void DrawPolyLines(string json) { var directionData = JsonConvert.DeserializeObject <DirectionParser>(json); // Decode Encoded Route var points = directionData.routes[0].overview_polyline.points; var line = PolyUtil.Decode(points); ArrayList routeList = new ArrayList(); foreach (LatLng item in line) { routeList.Add(item); } // Draw on map PolylineOptions polylineOptions = new PolylineOptions() .AddAll(routeList) .InvokeWidth(10) .InvokeColor(Color.Teal) .InvokeStartCap(new SquareCap()) .InvokeEndCap(new SquareCap()) .InvokeJointType(JointType.Round) .Geodesic(true); Android.Gms.Maps.Model.Polyline mPolyline = map.AddPolyline(polylineOptions); }
public async void OnMapReady(GoogleMap googleMap) { this.googleMap = googleMap; googleMap.MyLocationEnabled = true; this.googleMap.UiSettings.MyLocationButtonEnabled = true; googleMap.UiSettings.ScrollGesturesEnabled = true; MarkerOptions mrkerBagLocation = new MarkerOptions(); mrkerBagLocation.SetPosition(new LatLng(Double.Parse(ViewModel.BagLatitude), Double.Parse(ViewModel.BagLongitude))); mrkerBagLocation.SetTitle("Bag Location"); MarkerOptions mrkerUserLocation = new MarkerOptions(); mrkerUserLocation.SetPosition(new LatLng(47.639466, -122.130665)); mrkerUserLocation.SetTitle("Your Location"); googleMap.AddMarker(mrkerUserLocation); googleMap.AddMarker(mrkerBagLocation); googleMap.MoveCamera(CameraUpdateFactory.NewLatLng(new LatLng(47.639466, -122.130665))); var x = await GetRouteInfo(); RouteLine = GetPolylines(x); RouteLine.InvokeColor(-65536); RouteLine.InvokeWidth(5); Routes = googleMap.AddPolyline(RouteLine); }
public async void GetRouteInfo() { string response = string.Empty; try { await Task.Run(() => { URL url = new URL(string.Format("https://maps.googleapis.com/maps/api/directions/json?" + "origin={0},{1}&destination={2},{3}&key={4}", 47.636372, -122.126888, 47.639466, -122.130665, "AIzaSyCn2XMz_4-GjsAu2Ge1M6h8mFBVpResBYs")); HttpsURLConnection urlConnection = (HttpsURLConnection)url.OpenConnection(); urlConnection.Connect(); var stream = urlConnection.InputStream; using (var streamReader = new StreamReader(stream)) { response = streamReader.ReadToEnd(); } }); } catch (Exception e) { var x = e.Message; } if (Routes != null) { Routes.Remove(); } RouteLine = GetPolylines(response); RouteLine.InvokeColor(-65536); RouteLine.InvokeWidth(5); Routes = googleMap.AddPolyline(RouteLine); }
private void DrawPolyline() { PolylineOptions polylineOptions = GetPolylineRenderer(); foreach (var position in _routeCoordinates) { polylineOptions.Add(new LatLng(position.Latitude, position.Longitude)); } _polyline = NativeMap.AddPolyline(polylineOptions); }
protected Polyline GetFormsPolyline(APolyline polyline) { Polyline targetPolyline = null; for (int i = 0; i < Map.MapElements.Count; i++) { var element = Map.MapElements[i]; if ((string)element.MapElementId == polyline.Id) { targetPolyline = element as Polyline; break; } } return(targetPolyline); }
protected APolyline GetNativePolyline(Polyline polyline) { APolyline targetPolyline = null; if (_polylines != null) { for (int i = 0; i < _polylines.Count; i++) { var native = _polylines[i]; if (native.Id == (string)polyline.MapElementId) { targetPolyline = native; break; } } } return(targetPolyline); }
public void DrawTripMap(string json) { var directionData = JsonConvert.DeserializeObject <DirectionParser>(json); //Decode Encoded Route var points = directionData.routes[0].overview_polyline.points; var line = PolyUtil.Decode(points); ArrayList routeList = new ArrayList(); foreach (LatLng item in line) { routeList.Add(item); } //Draw Polylines on Map PolylineOptions polylineOptions = new PolylineOptions() .AddAll(routeList) .InvokeWidth(10) .InvokeColor(Color.Teal) .InvokeStartCap(new SquareCap()) .InvokeEndCap(new SquareCap()) .InvokeJointType(JointType.Round) .Geodesic(true); Android.Gms.Maps.Model.Polyline mPolyline = map.AddPolyline(polylineOptions); //Get the first point and last point LatLng firstpoint = line[0]; LatLng lastpoint = line[line.Count - 1]; //Pickup marker options MarkerOptions pickupMarkerOptions = new MarkerOptions(); pickupMarkerOptions.SetPosition(firstpoint); pickupMarkerOptions.SetTitle("Pickup Location"); pickupMarkerOptions.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueGreen)); //Destination marker options MarkerOptions destinationMarkerOptions = new MarkerOptions(); destinationMarkerOptions.SetPosition(lastpoint); destinationMarkerOptions.SetTitle("Destination"); destinationMarkerOptions.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueRed)); Marker pickupMarker = map.AddMarker(pickupMarkerOptions); Marker destinationMarker = map.AddMarker(destinationMarkerOptions); //Get Trip Bounds double southlng = directionData.routes[0].bounds.southwest.lng; double southlat = directionData.routes[0].bounds.southwest.lat; double northlng = directionData.routes[0].bounds.northeast.lng; double northlat = directionData.routes[0].bounds.northeast.lat; LatLng southwest = new LatLng(southlat, southlng); LatLng northeast = new LatLng(northlat, northlng); LatLngBounds tripBound = new LatLngBounds(southwest, northeast); //map.AnimateCamera(CameraUpdateFactory.NewLatLngBounds(tripBound, 470)); //causes crashing the app map.SetPadding(40, 70, 40, 70); pickupMarker.ShowInfoWindow(); destinationMarker.ShowInfoWindow(); duration = directionData.routes[0].legs[0].duration.value; distance = directionData.routes[0].legs[0].distance.value; durationString = directionData.routes[0].legs[0].duration.text; distanceString = directionData.routes[0].legs[0].distance.text; }
public void DrawTripOnMap(string json) { var directionData = JsonConvert.DeserializeObject <DirectionParser>(json); //Decode Encoded Route var points = directionData.routes[0].overview_polyline.points; var line = PolyUtil.Decode(points); ArrayList routeList = new ArrayList(); foreach (LatLng item in line) { routeList.Add(item); } //double distance = 0; //Xamarin.Essentials.Location source; //Xamarin.Essentials.Location dest; //for(int i=0; i<line.Count-1; i++) //{ // source = new Xamarin.Essentials.Location(line[i].Latitude, line[i].Longitude); // dest = new Xamarin.Essentials.Location(line[i + 1].Latitude, line[i + 1].Longitude); // distance += source.CalculateDistance(dest, DistanceUnits.Kilometers); // routeList.Add(line[i]); // if (i == line.Count - 2) // routeList.Add(line[i + 1]); //} //Draw Polylines on Map PolylineOptions polylineOptions = new PolylineOptions() .AddAll(routeList) .InvokeWidth(10) .InvokeColor(Color.Teal) .InvokeStartCap(new SquareCap()) .InvokeEndCap(new SquareCap()) .InvokeJointType(JointType.Round) .Geodesic(true); Android.Gms.Maps.Model.Polyline mPolyline = map.AddPolyline(polylineOptions); //Get first point and lastpoint LatLng firstpoint = line[0]; LatLng lastpoint = line[line.Count - 1]; //Pickup marker options MarkerOptions pickupMarkerOptions = new MarkerOptions(); pickupMarkerOptions.SetPosition(firstpoint); pickupMarkerOptions.SetTitle("Pickup Location"); pickupMarkerOptions.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueGreen)); //Destination marker options MarkerOptions destinationMarkerOptions = new MarkerOptions(); destinationMarkerOptions.SetPosition(lastpoint); destinationMarkerOptions.SetTitle("Destination"); destinationMarkerOptions.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueRed)); MarkerOptions driverMarkerOptions = new MarkerOptions(); driverMarkerOptions.SetPosition(firstpoint); driverMarkerOptions.SetTitle("Current Location"); driverMarkerOptions.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.position)); driverMarkerOptions.Visible(false); pickupMarker = map.AddMarker(pickupMarkerOptions); Marker destinationMarker = map.AddMarker(destinationMarkerOptions); driverLocationMarker = map.AddMarker(driverMarkerOptions); //Get Trip Bounds double southlng = directionData.routes[0].bounds.southwest.lng; double southlat = directionData.routes[0].bounds.southwest.lat; double northlng = directionData.routes[0].bounds.northeast.lng; double northlat = directionData.routes[0].bounds.northeast.lat; LatLng southwest = new LatLng(southlat, southlng); LatLng northeast = new LatLng(northlat, northlng); LatLngBounds tripBound = new LatLngBounds(southwest, northeast); map.AnimateCamera(CameraUpdateFactory.NewLatLngBounds(tripBound, 100)); map.SetPadding(40, 40, 40, 40); pickupMarker.ShowInfoWindow(); duration = directionData.routes[0].legs[0].duration.value; distance = directionData.routes[0].legs[0].distance.value; durationstring = directionData.routes[0].legs[0].duration.text; distanceString = directionData.routes[0].legs[0].distance.text; }
public async Task<Polyline> DrawRoute (string origin, string destination) { //string origin="45.81444,15.97798"; //string destination="45.80109,15.97082"; //Polyline ruta; PolylineOptions linija = new PolylineOptions(); linija.InvokeColor(-16776961); string url = "https://maps.googleapis.com/maps/api/directions/json?origin="+origin+"&destination="+destination+"&mode=walking®ion=hr&key=AIzaSyAi5T6O9DsVUJ3HMN4xDpfGf9qupGjY7xQ"; Console.WriteLine (url); var json = await getJson (url); string jsonString = json.ToString (); Regex points = new Regex ("\"polyline\": {\"points\": \"(.*?)},"); Match polypoints = points.Match (jsonString); while (polypoints.Success) { string finalni = polypoints.Groups [0].ToString (); finalni=finalni.Substring(24, finalni.Length-24); finalni=finalni.Remove(finalni.Length-3); finalni = finalni.Replace("\\\\", "\\"); List<LatLng> privremeni = DecodePolylinePoints(finalni); for (int i = 0; i <privremeni.Count; i++) { linija.Add (new LatLng (privremeni[i].Latitude, privremeni[i].Longitude)); } polypoints = polypoints.NextMatch (); } try{ ruta.Remove(); } catch (Exception){ } return ruta=mMap.AddPolyline(linija); }
public void DrawTripOnMap(string Json) { // TryCatch to handle any null Object exception. try { mPolyline.Remove(); PickupMarker.Remove(); destinationMarker.Remove(); } catch (Exception) { } var directionData = JsonConvert.DeserializeObject <DirectionParser>(Json); for (int i = 0; i < directionData.routes.Count; i++) { foreach (var point in RedissuePoints) { foreach (var waypoints in PolyUtil.Decode(directionData.routes[i].overview_polyline.points)) { if (Math.Abs(Convert.ToDouble(waypoints.Latitude) - Convert.ToDouble(point.Latitude)) <= 0.00001 && Math.Abs(Convert.ToDouble(waypoints.Longitude) - Convert.ToDouble(point.Longitude)) <= 0.00001) { issueWayPointCount[i] += 1; } } } } int routeIndex = issueWayPointCount.ToList().IndexOf(issueWayPointCount.Min()); //Decode Encoded route var Points = directionData.routes[routeIndex].overview_polyline.points; //enumerate through this line variable through for loop and //check for any points that matches for points contained in preloaded issues array var Line = PolyUtil.Decode(Points); ArrayList routeList = new ArrayList(); foreach (LatLng item in Line) { routeList.Add(item); } //Draw Polylines on map PolylineOptions polylineOptions = new PolylineOptions() .AddAll(routeList) .InvokeWidth(10) .InvokeColor(Color.BlueViolet) .InvokeStartCap(new SquareCap()) .InvokeEndCap(new RoundCap()) .InvokeJointType(JointType.Round) .Geodesic(true); mPolyline = googleMap.AddPolyline(polylineOptions); //Get first and last point LatLng firstpoint = Line[0]; LatLng lastpoint = Line[Line.Count - 1]; //Creating Marker Options MarkerOptions pickupMarkerOptions = new MarkerOptions(); pickupMarkerOptions.SetPosition(firstpoint); pickupMarkerOptions.SetTitle("Start Location"); BitmapDescriptor bitmapDescPickup = BitmapDescriptorFactory.FromResource(Resource.Drawable.starticon); pickupMarkerOptions.SetIcon(bitmapDescPickup); MarkerOptions destinationMarkerOptions = new MarkerOptions(); destinationMarkerOptions.SetPosition(lastpoint); destinationMarkerOptions.SetTitle("Destination"); BitmapDescriptor bitmapDescDestination = BitmapDescriptorFactory.FromResource(Resource.Drawable.destinationicon); destinationMarkerOptions.SetIcon(bitmapDescDestination); PickupMarker = googleMap.AddMarker(pickupMarkerOptions); destinationMarker = googleMap.AddMarker(destinationMarkerOptions); //Get Trip bounds double southlng = directionData.routes[0].bounds.southwest.lng; double southlat = directionData.routes[0].bounds.southwest.lat; double northlng = directionData.routes[0].bounds.northeast.lng; double northlat = directionData.routes[0].bounds.northeast.lat; LatLng southwest = new LatLng(southlat, southlng); LatLng northeast = new LatLng(northlat, northlng); LatLngBounds tripBounds = new LatLngBounds(southwest, northeast); googleMap.AnimateCamera(CameraUpdateFactory.NewLatLngBounds(tripBounds, 4)); //googleMap.SetPadding(40, 70, 40, 70); }
public void DrawTripOnMap(string json) { routeList.Clear(); var directionData = JsonConvert.DeserializeObject <DirectionParser>(json); //Decode Encoded Route var points = directionData.routes[0].overview_polyline.points; var line = PolyUtil.Decode(points); foreach (LatLng item in line) { routeList.Add(item); } //Draw Polylines on Map PolylineOptions polylineOptions = new PolylineOptions() .AddAll(routeList) .InvokeWidth(6) .InvokeColor(Color.Blue) .InvokeStartCap(new SquareCap()) .InvokeEndCap(new SquareCap()) .InvokeJointType(JointType.Round) .Geodesic(true); mPolyline = map.AddPolyline(polylineOptions); //Get first point and lastpoint LatLng firstpoint = line[0]; LatLng lastpoint = line[line.Count - 1]; duration = directionData.routes[0].legs[0].duration.value; distance = directionData.routes[0].legs[0].distance.value; durationstring = directionData.routes[0].legs[0].duration.text; distanceString = directionData.routes[0].legs[0].distance.text; startAddress = directionData.routes[0].legs[0].start_address; //Pickup marker options pickupMarkerOptions.SetPosition(firstpoint); pickupMarkerOptions.SetTitle("My Location"); pickupMarkerOptions.SetSnippet(durationstring); pickupMarkerOptions.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueRed)); //Destination marker options destinationMarkerOptions.SetPosition(lastpoint); destinationMarkerOptions.SetTitle(sessionManager.GetDestination()); destinationMarkerOptions.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueGreen)); pickupMarker = map.AddMarker(pickupMarkerOptions); destinationMarker = map.AddMarker(destinationMarkerOptions); //Get Trip Bounds double southlng = directionData.routes[0].bounds.southwest.lng; double southlat = directionData.routes[0].bounds.southwest.lat; double northlng = directionData.routes[0].bounds.northeast.lng; double northlat = directionData.routes[0].bounds.northeast.lat; LatLng southwest = new LatLng(southlat, southlng); LatLng northeast = new LatLng(northlat, northlng); LatLngBounds tripBound = new LatLngBounds(southwest, northeast); cameraUpdate = CameraUpdateFactory.NewLatLngBounds(tripBound, 200); map.AnimateCamera(cameraUpdate); destinationMarker.ShowInfoWindow(); }
public void DrawRouteOnMap(string json) { var directionData = JsonConvert.DeserializeObject <DirectionParser>(json); //Decode Encoded Route var points = directionData.routes[0].overview_polyline.points; var line = PolyUtil.Decode(points); ArrayList routeList = new ArrayList(); foreach (LatLng item in line) { routeList.Add(item); } //Draw Polylines on Map PolylineOptions polylineOptions = new PolylineOptions() .AddAll(routeList) .InvokeWidth(10) .InvokeColor(Color.Cyan) .InvokeStartCap(new SquareCap()) .InvokeEndCap(new SquareCap()) .InvokeJointType(JointType.Round) .Geodesic(true); Android.Gms.Maps.Model.Polyline mPolyline = map.AddPolyline(polylineOptions); //Getting start and end destination points LatLng startPoint = line[0]; LatLng endPoint = line[line.Count - 1]; //Start marker options MarkerOptions startMarkerOptions = new MarkerOptions(); startMarkerOptions.SetPosition(startPoint); startMarkerOptions.SetTitle("Start Location"); startMarkerOptions.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueGreen)); //End marker options MarkerOptions endMarkerOptions = new MarkerOptions(); endMarkerOptions.SetPosition(endPoint); endMarkerOptions.SetTitle("End Location"); endMarkerOptions.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueRed)); Marker startMarker = map.AddMarker(startMarkerOptions); Marker endMarker = map.AddMarker(endMarkerOptions); //Get route bounds double southlng = directionData.routes[0].bounds.southwest.lng; double southlat = directionData.routes[0].bounds.southwest.lat; double northlng = directionData.routes[0].bounds.northeast.lng; double northlat = directionData.routes[0].bounds.northeast.lat; LatLng southwest = new LatLng(southlat, southlng); LatLng northeast = new LatLng(northlat, northlng); LatLngBounds routeBound = new LatLngBounds(southwest, northeast); map.AnimateCamera(CameraUpdateFactory.NewLatLngBounds(routeBound, 470)); map.SetPadding(40, 70, 40, 70); startMarker.ShowInfoWindow(); distance = directionData.routes[0].legs[0].distance.value; distanceString = directionData.routes[0].legs[0].distance.text; }
/// <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 async void AddDirectionsAsync() { try { var directions = await GetDirectionsAsync(); RunOnUiThread(() => { ClearPolyline(); if (ActionMode != null) ActionMode.Title = $"{directions.Distance}, {directions.Duration} ({(_settingsService.Settings.IsBikeMode ? "walking" : "cycling")})"; _currentPolyline = _map.AddPolyline(directions.Polylines); }); } catch { } }
void UpdateMap(TripPoint point, bool updateCamera = true) { if (map == null) return; //Get trail position or current potion to move car var latlng = point == null ? viewModel?.CurrentPosition?.ToLatLng() : point.ToLatLng(); Activity?.RunOnUiThread(() => { UpdateCar(latlng); driveLine?.Remove(); var polyOptions = new PolylineOptions(); if (allPoints == null) { allPoints = viewModel.CurrentTrip.Points.ToLatLngs(); } else if (point != null) { allPoints.Add(point.ToLatLng()); } polyOptions.Add(allPoints.ToArray()); if (!driveLineColor.HasValue) driveLineColor = new Color(ContextCompat.GetColor(Activity, Resource.Color.recording_accent)); polyOptions.InvokeColor(driveLineColor.Value); driveLine = map.AddPolyline(polyOptions); if (updateCamera) UpdateCamera(latlng); }); }