CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Reposted on 2024-02-09
by trilogy

Original Post

Original - Posted on 2024-02-09
by trilogy



            
Present in both answers; Present only in the new answer; Present only in the old answer;

You can use the Java Google Maps SDK :
<dependency> <groupId>com.google.maps</groupId> <artifactId>google-maps-services</artifactId> <version>2.2.0</version> </dependency>
Code:


/** * * @param apiKey * @param origin * @param destination * @return */ public static DistanceMatrixRow[] getDistance(String apiKey, String origin, String destination) { DistanceMatrixApiRequest req = DistanceMatrixApi.newRequest(new GeoApiContext.Builder() .apiKey(apiKey) .build()); DistanceMatrix query = req.origins(origin) .destinations(destination) .mode(TravelMode.DRIVING) .avoid(RouteRestriction.TOLLS) .language("en-US") .awaitIgnoreError(); if (query != null && query.rows.length > 0) { return query.rows; } else { return null; } }
Through the Java Google Maps SDK, you can get the lat/lng for a specific address, then using the haversine distance formula to get the distance in KM.
<dependency> <groupId>com.google.maps</groupId> <artifactId>google-maps-services</artifactId> <version>2.2.0</version> </dependency>
Code:





public static Double getDistanceInKmAsTheCrowFlies(String apiKey, String address1, String address2) { GeocodingResult[] address1GeoCode = getGeocoding(apiKey, address1); GeocodingResult[] address2GeoCode = getGeocoding(apiKey, address2); if (address1GeoCode != null && address2GeoCode != null) { double lat1 = address1GeoCode[0].geometry.location.lat; double lng1 = address1GeoCode[0].geometry.location.lng; double lat2 = address2GeoCode[0].geometry.location.lat; double lng2 = address2GeoCode[0].geometry.location.lng; return getHaversineDistance(lat1, lng1, lat2, lng2); } return null; } public static GeocodingResult[] getGeocoding(String apiKey, String address) { GeocodingApiRequest req = GeocodingApi.newRequest(new GeoApiContext.Builder() .apiKey(apiKey) .build()); GeocodingResult[] query = req.address(address).awaitIgnoreError(); if (query != null && query.length > 0) { return query; } return null; } public static double getHaversineDistance(double lat1, double lng1, double lat2, double lng2) { int r = 6371; // average radius of the earth in km double dLat = Math.toRadians(lat2 - lat1); double dLon = Math.toRadians(lng2 - lng1); double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); double d = r * c; return d; }


        
Present in both answers; Present only in the new answer; Present only in the old answer;