Search code examples
javaandroidgoogle-mapsgoogle-maps-api-3

Fetching fuel price from google map


I am trying to retrieve fuel prices from all nearby stations. I am using PlacesApi. While it retrieves all the station information like location, address, ratings, etc it doesn't return the fuel price info. After a bit of searching I found that map api doesn't return fuel api. Is there any work around to get this info?

This is what I have implemented so far.

List<PlaceDetails> list = new ArrayList<>();

GeocodingResult[] geocodingResults = new GeocodingResult[0];
try {
    geocodingResults = GeocodingApi.geocode(context,"Dallas").await();
    if (geocodingResults.length > 0) {
        LatLng location = geocodingResults[0].geometry.location;

        // Perform the Places API request to get the gas stations in Dallas
        PlacesSearchResponse searchResponse = PlacesApi.nearbySearchQuery(context, location)
                .radius(20000) // Specify the search radius in meters (adjust as needed)
                .type(PlaceType.GAS_STATION) // Search for gas stations
                .await();


        // Iterate through the results and print the gas station names
        for (PlacesSearchResult result : searchResponse.results) {
            PlaceDetails placeDetails = PlacesApi.placeDetails(context, result.placeId).awaitIgnoreError();
            list.add(placeDetails);
            System.out.println(result.name);
        }
    } else {
        System.out.println("Unable to geocode Dallas.");
    }

} catch (Exception e) {
    throw new RuntimeException(e);
}

The placeDetails obj contains all the info except fuel info.

enter image description here


Solution

  • Google Places API (New) has fuel prices now. https://developers.google.com/maps/documentation/places/web-service/reference/rest/v1/places#fuelprice