decodeAndSelectPlace method

void decodeAndSelectPlace(
  1. String placeId
)

To navigate to the selected place from the autocomplete list to the map, the lat,lng is required. This method fetches the lat,lng of the place and proceeds to moving the map to that location.

Implementation

void decodeAndSelectPlace(String placeId) async {
  clearOverlay();

  try {
    final url = Uri.parse(
        "https://maps.googleapis.com/maps/api/place/details/json?key=${widget.apiKey}&" +
            "language=${widget.localizationItem!.languageCode}&" +
            "placeid=$placeId");

    final response = await http.get(url);

    if (response.statusCode != 200) {
      throw Error();
    }

    final responseJson = jsonDecode(response.body);

    if (responseJson['result'] == null) {
      throw Error();
    }

    final location = responseJson['result']['geometry']['location'];
    if (mapController.isCompleted) {
      moveToLocation(LatLng(location['lat'], location['lng']));
    }
  } catch (e) {
    print(e);
  }
}