| 36676 |
vikas |
1 |
package com.spice.profitmandi.dao.service;
|
|
|
2 |
|
|
|
3 |
import com.spice.profitmandi.common.model.MapBoxResult;
|
|
|
4 |
import com.spice.profitmandi.common.util.StringUtils;
|
|
|
5 |
import com.spice.profitmandi.common.web.client.RestClient;
|
|
|
6 |
import org.apache.logging.log4j.LogManager;
|
|
|
7 |
import org.apache.logging.log4j.Logger;
|
|
|
8 |
import org.json.JSONObject;
|
|
|
9 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
10 |
import org.springframework.stereotype.Service;
|
|
|
11 |
|
|
|
12 |
@Service
|
|
|
13 |
public class MapBoxService {
|
|
|
14 |
|
|
|
15 |
private static final Logger LOGGER = LogManager.getLogger(MapBoxService.class);
|
|
|
16 |
private static final String MAPBOX_TOKEN = "pk.eyJ1Ijoic2R0ZWNobm9sb2d5IiwiYSI6ImNtYzk2OWpoZjEwb2cybXIyaWZ6MzRmdmkifQ._UlFPHYYtCB7FIqbNbdZhg";
|
|
|
17 |
|
|
|
18 |
@Autowired
|
|
|
19 |
private RestClient restClient;
|
|
|
20 |
|
|
|
21 |
public String reverseGeocode(String latLng) {
|
|
|
22 |
try {
|
|
|
23 |
double[] coords = parseLatLng(latLng);
|
|
|
24 |
double lng = coords[0];
|
|
|
25 |
double lat = coords[1];
|
|
|
26 |
String url = String.format(
|
|
|
27 |
"https://api.mapbox.com/geocoding/v5/mapbox.places/%f,%f.json?access_token=%s&limit=1",
|
|
|
28 |
lng, lat, MAPBOX_TOKEN
|
|
|
29 |
);
|
|
|
30 |
LOGGER.info("Reverse geocode URL: {}", url);
|
|
|
31 |
String response = restClient.get(url, null, null);
|
|
|
32 |
JSONObject json = new JSONObject(response);
|
|
|
33 |
if (json.has("features") && json.getJSONArray("features").length() > 0) {
|
|
|
34 |
return json.getJSONArray("features").getJSONObject(0).getString("place_name");
|
|
|
35 |
}
|
|
|
36 |
} catch (Exception e) {
|
|
|
37 |
LOGGER.info("Error reverse geocoding: " + e.getMessage());
|
|
|
38 |
}
|
|
|
39 |
return null;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
public MapBoxResult calculateDistanceAndETA(double lat1, double lon1, double lat2, double lon2) {
|
|
|
43 |
try {
|
|
|
44 |
// Mapbox requires coordinates as lng,lat`
|
|
|
45 |
String coordinates = String.format("%f,%f;%f,%f", lon1, lat1, lon2, lat2);
|
|
|
46 |
String url = String.format(
|
|
|
47 |
"https://api.mapbox.com/directions/v5/mapbox/driving/%s?access_token=%s",
|
|
|
48 |
coordinates, MAPBOX_TOKEN
|
|
|
49 |
);
|
|
|
50 |
|
|
|
51 |
LOGGER.info("Request URL {}", url);
|
|
|
52 |
String response = restClient.get(url, null, null);
|
|
|
53 |
|
|
|
54 |
JSONObject json = new JSONObject(response);
|
|
|
55 |
JSONObject route = json.getJSONArray("routes").getJSONObject(0);
|
|
|
56 |
|
|
|
57 |
LOGGER.info("route: {}", route);
|
|
|
58 |
double distanceInMeters = route.getDouble("distance");
|
|
|
59 |
double durationInSeconds = route.getDouble("duration");
|
|
|
60 |
|
|
|
61 |
String distanceKm = StringUtils.formatDistance(distanceInMeters);
|
|
|
62 |
|
|
|
63 |
LOGGER.info("distanceKm: {}, durationInSeconds: {}", distanceKm, durationInSeconds);
|
|
|
64 |
return new MapBoxResult(distanceKm, durationInSeconds);
|
|
|
65 |
|
|
|
66 |
} catch (Exception e) {
|
|
|
67 |
LOGGER.info("Error calling Mapbox API: " + e);
|
|
|
68 |
return new MapBoxResult("0.0", 0L);
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public MapBoxResult calculateDistanceAndETA(String latLng1, String latLng2) {
|
|
|
73 |
try {
|
|
|
74 |
double[] origin = parseLatLng(latLng1);
|
|
|
75 |
double[] destination = parseLatLng(latLng2);
|
|
|
76 |
|
|
|
77 |
return calculateDistanceAndETA(origin[1], origin[0], destination[1], destination[0]);
|
|
|
78 |
|
|
|
79 |
} catch (Exception e) {
|
|
|
80 |
LOGGER.info("Invalid latLng format: " + e.getMessage());
|
|
|
81 |
return new MapBoxResult("0.0", 0L);
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
private static double[] parseLatLng(String latLng) {
|
|
|
86 |
String[] parts = latLng.split(",");
|
|
|
87 |
if (parts.length != 2) throw new IllegalArgumentException("Invalid format: " + latLng);
|
|
|
88 |
|
|
|
89 |
double lat = Double.parseDouble(parts[0].trim());
|
|
|
90 |
double lon = Double.parseDouble(parts[1].trim());
|
|
|
91 |
|
|
|
92 |
return new double[]{lon, lat};
|
|
|
93 |
}
|
|
|
94 |
}
|