Skip to content

Instantly share code, notes, and snippets.

@ricardoogliari
Created June 18, 2019 03:36
Show Gist options
  • Save ricardoogliari/10e1f61bd6c67f1f499fc16c9271f45b to your computer and use it in GitHub Desktop.
Save ricardoogliari/10e1f61bd6c67f1f499fc16c9271f45b to your computer and use it in GitHub Desktop.
class MainActivity: FlutterActivity() {
private val CHANNEL_GEOLOCATOR = "geolocator"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
MethodChannel(flutterView, CHANNEL_GEOLOCATOR).setMethodCallHandler(
object : MethodCallHandler {
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
val latitude: Double? = call.argument("latitude");
val longitude: Double? = call.argument("longitude");
if (latitude != null && longitude != null) {
result.success(getAddress(latitude, longitude));
} else {
result.error("UNAVAILABLE", "Método não recebeu latitude e/ou longitude", null);
}
}
})
}
fun getAddress(latitude: Double, longitude: Double): String{
val geocoder: Geocoder = Geocoder(this)
val listAddress = geocoder.getFromLocation(latitude, longitude, 1)
val address = listAddress.get(0)
return address.getAddressLine(0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment