public PointLocation getCenterLocations(double lat1, double lon1, double lat2, double lon2) { PointLocation point = new PointLocation(); MathUtils mathUtils = new MathUtils(); double dLon = mathUtils.ConvertToRadians(lon2 - lon1); double tempLat1 = mathUtils.ConvertToRadians(lat1); double tempLat2 = mathUtils.ConvertToRadians(lat2); double tempLon1 = mathUtils.ConvertToRadians(lon1); double Bx = Math.Cos(tempLat2) * Math.Cos(dLon); double By = Math.Cos(tempLat2) * Math.Sin(dLon); double lat3 = Math.Atan2(Math.Sin(tempLat1) + Math.Sin(tempLat2), Math.Sqrt((Math.Cos(tempLat1) + Bx) * (Math.Cos(tempLat1) + Bx) + By * By)); double lon3 = tempLon1 + Math.Atan2(By, Math.Cos(tempLat1) + Bx); lat3 = mathUtils.RadianToDegree(lat3); lon3 = mathUtils.RadianToDegree(lon3); point.setLatitude(lat3); point.setLongitude(lon3); return(point); }
private void buttonRouting_Click(object sender, EventArgs e) { if (!textBoxFrom.Text.Equals("") && !textBoxDestination.Text.Equals("")) { String nodeFromName = textBoxFrom.Text; String nodeDestinationName = textBoxDestination.Text; WayNodeModel mFrom = dbConnection.getWayNodeDataAttribute(nodeFromName); WayNodeModel mDestination = dbConnection.getWayNodeDataAttribute(nodeDestinationName); String lat1 = Convert.ToString(mFrom.getLatitude()); String lon1 = Convert.ToString(mFrom.getLongitude()); String lat2 = Convert.ToString(mDestination.getLatitude()); String lon2 = Convert.ToString(mDestination.getLongitude()); // create valid latitude & longitude type from database, ex : -69097867 to -6.9097867 and 1076106278 to 107.6106278 lat1 = lat1.Insert(2, "."); lon1 = lon1.Insert(3, "."); lat2 = lat2.Insert(2, "."); lon2 = lon2.Insert(3, "."); Console.WriteLine("Lat1:" + lat1 + " Lon1:" + lon1); Console.WriteLine("Lat2:" + lat2 + " Lon2:" + lon2); // add marker for source GMapOverlay markersOverlayFrom = new GMapOverlay(mFrom.getWayName()); GMarkerGoogle markerFrom = new GMarkerGoogle(new PointLatLng(Convert.ToDouble(lat1), Convert.ToDouble(lon1)), GMarkerGoogleType.green); markersOverlayFrom.Markers.Add(markerFrom); gMapControl.Overlays.Add(markersOverlayFrom); // add marker for destination GMapOverlay markersOverlayDest = new GMapOverlay(mDestination.getWayName()); GMarkerGoogle markerDest = new GMarkerGoogle(new PointLatLng(Convert.ToDouble(lat2), Convert.ToDouble(lon2)), GMarkerGoogleType.red); markersOverlayDest.Markers.Add(markerDest); gMapControl.Overlays.Add(markersOverlayDest); // getCenterMap from 2 location PointLocation centerMapLocation = new PointLocation(); MapUtils mapUtil = new MapUtils(); centerMapLocation = mapUtil.calculateMidPointLocations(Convert.ToDouble(lat1), Convert.ToDouble(lon1), Convert.ToDouble(lat2), Convert.ToDouble(lon2)); double bearing = mapUtil.calculateBearing(Convert.ToDouble(lat1), Convert.ToDouble(lon1), Convert.ToDouble(lat2), Convert.ToDouble(lon2)); double distance = mapUtil.calculateDistanceKM(Convert.ToDouble(lat1), Convert.ToDouble(lon1), Convert.ToDouble(lat2), Convert.ToDouble(lon2)); Console.WriteLine("Lat3:" + centerMapLocation.getLatitude() + " Lon3:" + centerMapLocation.getLongitude()); Console.WriteLine("Bearing:" + bearing); Console.WriteLine("Distance:" + distance); Console.WriteLine("Pixel Distance:" + CentimeterToPixel(distance * 10)); // get route result string jsonResultPath = getRoutePath(mFrom.getNodeId(), mDestination.getNodeId()); JObject results = JObject.Parse(jsonResultPath); List <NodeModel> listNode = new List <NodeModel>(); foreach (var result in results["path"]) { NodeModel node = new NodeModel(); string nodeId = (string)result["node_id"]; string lat = (string)result["latitude"]; string lon = (string)result["longitude"]; // create valid latitude & longitude type from database, ex : -69097867 to -6.9097867 and 1076106278 to 107.6106278 lat = lat.Insert(2, "."); lon = lon.Insert(3, "."); node.setNodeId(nodeId); node.setLatitude(Convert.ToDouble(lat)); node.setLongitude(Convert.ToDouble(lon)); listNode.Add(node); //Console.WriteLine("NodeId: {0}, Latitude: {1}, Longitude: {2}", nodeId, lat, lon); } // draw path GMapOverlay routeOverlay = new GMapOverlay("path"); List <PointLatLng> points = new List <PointLatLng>(); // path point for (int i = 0; i < listNode.Count; i++) { points.Add(new PointLatLng(listNode[i].getLatitude(), listNode[i].getLongitude())); Console.WriteLine("NodeId: {0}, Latitude: {1}, Longitude: {2}", listNode[i].getNodeId(), listNode[i].getLatitude(), listNode[i].getLongitude()); } // draw line GMapRoute gRoute = new GMapRoute(points, "route"); gRoute.Stroke.Width = 7; //gRoute.Stroke.Color = Color.SeaGreen; //gRoute.Stroke = new Pen(Color.Blue, 7); routeOverlay.Routes.Add(gRoute); gMapControl.Overlays.Add(routeOverlay); // show getCenterMap from 2 location gMapControl.Position = new GMap.NET.PointLatLng(centerMapLocation.getLatitude(), centerMapLocation.getLongitude()); gMapControl.Zoom = 16; int width = gMapControl.Width; int height = gMapControl.Height; Console.WriteLine("[MapControl] Width: " + width + " Height:" + height); /* * // AStar calculation * AStar astar = new AStar(); * astar.calcuateShortestPath(mFrom.getNodeId(), mDestination.getNodeId()); */ /* * // test take 5 screenshots * List<WayNodeModel> listWays = dbConnection.getWayNodeData(); * for(int i = 0; i < 5; i++){ * createMapTileScreenshots(listWays[i].getNodeId(), Convert.ToString(listWays[i].getLatitude()), Convert.ToString(listWays[i].getLongitude())); * }*/ /*String pathScreenshots = System.IO.Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory) + Path.DirectorySeparatorChar + "screenshots" + Path.DirectorySeparatorChar; * Console.WriteLine("Screenshots Path: " + pathScreenshots); * try * { * // setting for proxy * //PhantomJSOptions phOptions = new PhantomJSOptions(); * //phOptions.AddAdditionalCapability(CapabilityType.Proxy, "cache.itb.ac.id"); * * // hide cmd windows of phantomsjs * var driverService = PhantomJSDriverService.CreateDefaultService(); * driverService.HideCommandPromptWindow = true; * * // initiate phantomjs driver * PhantomJSDriver phantom; * phantom = new PhantomJSDriver(driverService); * // setting the default timeout to 30 seconds * phantom.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30)); * string screenshotUrl = "http://localhost/bsts_routing/tilegen/v2/index.php?latitude=-6.9131786&longitude=107.6474137"; * phantom.Navigate().GoToUrl(screenshotUrl); * * // grab the snapshot * Screenshot sh = phantom.GetScreenshot(); * sh.SaveAsFile(pathScreenshots + "test.png", ImageFormat.Png); * phantom.Quit(); * * } catch (Exception error) { * Console.WriteLine(error.Message); * }*/ } else { MessageBox.Show("Please input address first!"); } }