1-) FLUTTER - bulunduğum konumu alma geolocator get user location map kullanımı
compileSdkVersion 33
minSdkVersion 21
targetSdkVersion 33
1. ADIM -> terminale yaz -> flutter pub add geolocator
2. ADIM (AndroidManifest.xml)
<uses-permission android:name=
"android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name=
"android.permission.ACCESS_FINE_LOCATION"/>
<application
2.1 ADIM (ios/Runner/Info.plist)
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
3. ADIM (determinePosition metodu)
Future<Position> _determinePosition() async {
bool serviceEnabled;
LocationPermission permission;
// Test if location services are enabled.
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
// Location services are not enabled don't continue
// accessing the position and request users of the
// App to enable the location services.
return Future.error('Location services are disabled.');
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
// Permissions are denied, next time you could try
// requesting permissions again (this is also where
// Android's shouldShowRequestPermissionRationale
// returned true. According to Android guidelines
// your App should show an explanatory UI now.
return Future.error('Location permissions are denied');
}
}
if (permission == LocationPermission.deniedForever) {
// Permissions are denied forever, handle appropriately.
return Future.error(
'Location permissions are permanently denied, we cannot request permissions.');
}
// When we reach here, permissions are granted and we can
// continue accessing the position of the device.
return await Geolocator.getCurrentPosition();
}
4. ADIM KULLANIMI
@override
void initState() {
super.initState();
var postion = _determinePosition().then((value) {
String latitude = value.latitude.toString();
String longitude = value.longitude.toString();
EasyLoading.showToast(latitude+" "+longitude);
});
}
5. ADIM şehir/il ve ilçe alma
terminal çalıştır -> flutter pub add geocoding
açıklama : determinePosition metodu yukarıdan kopyalıcaz
void getLocation() {
try {
_determinePosition().then((position) async {
List<Placemark> placemarks = await placemarkFromCoordinates(
position.latitude,
position.longitude,
);
//EasyLoading.showToast(placemarks[0].toString());
String il = placemarks.first.administrativeArea ?? ""; // null ise "" yap değilse kendini al
String ilce = placemarks.first.subAdministrativeArea ?? "";
EasyLoading.showToast(il+"-"+ilce);
});
} catch (err) {}
}
@override
void initState() {
super.initState();
getLocation();
}
ekran görüntüsü
kaynak : https://pub.dev/packages/geolocator
il ve ilçe alma kaynak :
https://pub.dev/packages/geocoding
https://stackoverflow.com/questions/65213035/how-to-get-city-from-coordinates-in-flutter
6. ADIM WEB İÇİN BU KODLARI KULLANMAN LAZIM
kaynak : https://pub.dev/packages/osm_nominatim/example
açıklama : webde ilçe de yanılma payı olabilir. lat ve log den sorgulayıp buluyoruz.
import 'package:flutter/foundation.dart' show kIsWeb;
void getLocation() {
try {
_determinePosition().then((position) async {
if (kIsWeb) {
final reverseSearchResult = Nominatim.reverseSearch(
lat: position.latitude,
lon: position.longitude,
addressDetails: true,
extraTags: true,
nameDetails: true,
).then((adres) {
adres.address!.forEach((key, value) {
switch(key){
case "province":
StatikSinif.il = value;
break;
case "town":
StatikSinif.ilce = value;
break;
}
print(key.toString()+"-"+value.toString());
});
EasyLoading.showToast(StatikSinif.il + "-" + StatikSinif.ilce);
});
} else {
List<Placemark> placemarks = await placemarkFromCoordinates(
position.latitude,
position.longitude,
);
String il = placemarks.first.administrativeArea ??
""; // null ise "" yap değilse kendini al
String ilce = placemarks.first.subAdministrativeArea ?? "";
StatikSinif.il = il;
StatikSinif.ilce = ilce;
EasyLoading.showToast(il + "-" + ilce);
}
});
} catch (err) {}
}
ayrıca direk bu siteden ili çekebilirsin -> https://geolocation-db.com/json/