My own experince

I've started creating my own platform for couriers and it has already got some functionality:

1) finds closest underground stations
2) shows the distance between your endpoint and nearest underground stations

How I did It: Code

JSONObject jsonObject = new JSONObject(result.toString());
                        JSONArray jsonArray = jsonObject.getJSONObject("response").getJSONObject("GeoObjectCollection").getJSONArray("featureMember");
                        String[] longitudeAndLatitudeOfDestination = geoCode.getLocation().split(" ", 2);
                        String[] longitudeAndLatitudeOFUndergroundStation1 = jsonArray.getJSONObject(0).getJSONObject("GeoObject")
                                .getJSONObject("Point").getString("pos").split(" ", 2);
                        String[] longitudeAndLatitudeOFUndergroundStation2 = jsonArray.getJSONObject(1).getJSONObject("GeoObject")
                                .getJSONObject("Point").getString("pos").split(" ", 2);
                        nearestUndergroundStation = jsonArray.getJSONObject(0).getJSONObject("GeoObject").getString("name") +
                                "(" + calculateDistanceFromTheUndergroundStation(Double.parseDouble(longitudeAndLatitudeOfDestination[0]),
                                Double.parseDouble(longitudeAndLatitudeOfDestination[1]), Double.parseDouble(longitudeAndLatitudeOFUndergroundStation1[0]),
                                Double.parseDouble(longitudeAndLatitudeOFUndergroundStation1[1])) + "км), ";
                        nearestUndergroundStation = nearestUndergroundStation.replace("станция ", "");
                        nearestUndergroundStation = nearestUndergroundStation.replace("метро ", "");
                        nearestUndergroundStation2 = jsonArray.getJSONObject(1).getJSONObject("GeoObject").getString("name") +
                                "(" + calculateDistanceFromTheUndergroundStation(Double.parseDouble(longitudeAndLatitudeOfDestination[0]),
                                Double.parseDouble(longitudeAndLatitudeOfDestination[1]), Double.parseDouble(longitudeAndLatitudeOFUndergroundStation2[0]),
                                Double.parseDouble(longitudeAndLatitudeOFUndergroundStation2[1])) + "км)";
                        nearestUndergroundStation2 = nearestUndergroundStation2.replace("станция ", "");
                        nearestUndergroundStation2 = nearestUndergroundStation2.replace("метро ", "");
                        String nameOfLine = jsonArray.getJSONObject(0).getJSONObject("GeoObject").getString("description");
                        String nameOfLine2 = jsonArray.getJSONObject(1).getJSONObject("GeoObject").getString("description");
                        Log.d("KSI", nameOfLine);


I also used the formula to calculate the distance between 2 points on the planet Earth using latitude and longitude and then I converted it to kilometres:

public String calculateDistanceFromTheUndergroundStation(double longitude1, double latitude1, double longitude2, double latitude2){
                double distance = Math.acos(Math.sin(Math.toRadians(latitude1)) * Math.sin(Math.toRadians(latitude2))
                        + Math.cos(Math.toRadians(latitude1)) * Math.cos(Math.toRadians(latitude2))
                        * Math.cos(Math.toRadians(longitude1) - Math.toRadians(longitude2)));
                distance = distance * 6371;
                return String.format("%.1f", distance);
            }


Outputs



0 comments

Only registered users can comment.