Skip to content

Instantly share code, notes, and snippets.

@preetjdp
Last active May 15, 2020 07:51
Show Gist options
  • Select an option

  • Save preetjdp/a26a72c83fe9234c77d2ebdfe2fcf048 to your computer and use it in GitHub Desktop.

Select an option

Save preetjdp/a26a72c83fe9234c77d2ebdfe2fcf048 to your computer and use it in GitHub Desktop.
Future<String> getLocalityNameFromLocation(BuddyLocation location) async {
GoogleMapsGeocoding geo = GoogleMapsGeocoding(apiKey: "API_KEY");
GeocodingResponse geoCodingResponse = await geo.searchByLocation(location.gMapslocation);
List<AddressComponent> results = geoCodingResponse.results
.expand((result) => result.addressComponents
.where((result) => result.types.contains('sublocality_level_2')))
.toList();
AddressComponent searchResultAddr = results.first;
return searchResultAddr.shortName;
}
Future<gMapsWebPlaces.PlaceDetails> getPlaceFromPlaceId(String placeId) async {
gMapsWebPlaces.GoogleMapsPlaces places = gMapsWebPlaces.GoogleMapsPlaces(apiKey: "API_KEY");
gMapsWebPlaces.PlacesDetailsResponse placeIdResponse =
await places.getDetailsByPlaceId(placeId);
return placeIdResponse.result;
}
Future<List<BuddyLocation>> pointsFromCurrentLocationToPrediction(
BuildContext context, gMapsWebPlaces.Prediction prediction) async {
DatabaseService databaseService = DatabaseService();
gMapsWebDirections.GoogleMapsDirections directions =
gMapsWebDirections.GoogleMapsDirections(
apiKey: "API_KEY");
gMapsWebDirections.Location currentLocation =
Provider.of<User>(context).location.gMapslocation;
gMapsWebPlaces.PlaceDetails destinationPlace =
await databaseService.getPlaceFromPlaceId(prediction.placeId);
gMapsWebDirections.Location destinationLocation =
destinationPlace.geometry.location;
gMapsWebDirections.DirectionsResponse directionResponse = await directions
.directionsWithLocation(currentLocation, destinationLocation);
List<LatLng> _listLatLngPoints =
//TODO explore why I'm taking only the first one here??
decodePolyline(directionResponse.routes[0].overviewPolyline.points);
List<BuddyLocation> listBuddyLocations = _listLatLngPoints
.map((point) => BuddyLocation.fromData(point.latitude, point.longitude))
.toList();
return listBuddyLocations;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment