Reputation: 61
I want to fetch address to show on google map. I can only do address with lat,lng. Name of home address, road, sub-district, city, but I tried many ways and it's not showing up. Please help.
I'll have it written in the else section.
@override
void initState() {
findLocation();
super.initState();
}
LatLng centerMap = LatLng(1232,213123, 123.123213);
void findLocation() async {
var lat = double.tryParse(searchitems[0].address![widget.index].latitude.toString());
var lng = double.tryParse(searchitems[0].address![widget.index].longitude.toString());
if (lat != null && lng != null) {
centerMap = LatLng(lat, lng);
} else {
}
print("map:$centerMap");
}
Upvotes: 0
Views: 986
Reputation: 1238
If you want to get address by lattiude
and longitude
then you can use Google's API for finding the address.
For that, first you have to generate a key from Google Cloud Platform
and enable the Geocoding API from it. Then you can fetch address
this way:
getAddressFromLatLng(context, double lat, double lng) async {
String _host = 'https://maps.google.com/maps/api/geocode/json';
final url = '$_host?key=$mapApiKey&language=en&latlng=$lat,$lng';
if(lat != null && lng != null){
var response = await http.get(Uri.parse(url));
if(response.statusCode == 200) {
Map data = jsonDecode(response.body);
String _formattedAddress = data["results"][0]["formatted_address"];
print("response ==== $_formattedAddress");
return _formattedAddress;
} else return null;
} else return null;
}
Now if you want to get lat-long
by address, you can do it in following way. You can add this package called geocoder
:
(from source)
import 'package:geocoder/geocoder.dart';
// From a query
final query = "1600 Amphiteatre Parkway, Mountain View";
var addresses = await Geocoder.local.findAddressesFromQuery(query);
var first = addresses.first;
print("${first.featureName} : ${first.coordinates}");
There's also a package called geocoding
(link). You can try that too.
Upvotes: 1