getNearbyPlaces method
void
getNearbyPlaces(
- LatLng latLng
Utility function to get clean readable name of a location. First checks for a human-readable name from the nearby list. This helps in the cases that the user selects from the nearby list (and expects to see that as a result, instead of road name). If no name is found from the nearby list, then the road name returned is used instead. Fetches and updates the nearby places to the provided lat,lng
Implementation
// String getLocationName() {
// if (locationResult == null) {
// return "Unnamed location";
// }
//
// for (NearbyPlace np in nearbyPlaces) {
// if (np.latLng == locationResult.latLng) {
// locationResult.name = np.name;
// return np.name;
// }
// }
//
// return "${locationResult.name}, ${locationResult.locality}";
// }
/// Fetches and updates the nearby places to the provided lat,lng
void getNearbyPlaces(LatLng latLng) async {
var endpoint =
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
"key=${widget.apiKey}&"
"location=${latLng.latitude},${latLng.longitude}&radius=150"
"&language=${widget.language}";
try {
final response = await http.get(Uri.parse(endpoint));
if (response.statusCode == 200) {
nearbyPlaces.clear();
for (Map<String, dynamic> item
in jsonDecode(response.body)['results']) {
NearbyPlace nearbyPlace = NearbyPlace();
nearbyPlace.name = item['name'];
nearbyPlace.icon = item['icon'];
double latitude = item['geometry']['location']['lat'];
double longitude = item['geometry']['location']['lng'];
LatLng _latLng = LatLng(latitude, longitude);
nearbyPlace.latLng = _latLng;
nearbyPlaces.add(nearbyPlace);
}
}
// to update the nearby places
setState(() {
// this is to require the result to show
hasSearchTerm = false;
});
} catch (error) {
logger.e(error);
}
}