private lateinit var fusedLocationClient: FusedLocationProviderClient
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val name=findViewById(R.id.editText) as TextView
var text=""
locationManager = getSystemService(LOCATION_SERVICE) as LocationManager?
val button =findViewById(R.id.button) as Button
var mainlink=""
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
val adresstext= findViewById(R.id.editText3)as TextView
var deneme=""
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationClient.lastLocation.addOnSuccessListener {
}
fusedLocationClient.lastLocation.addOnSuccessListener {
location -> location
var locationx=location.longitude
var locationy=location.latitude
val name = findViewById(R.id.textView) as TextView
var geocoder: Geocoder
var listadress :List<Address>
geocoder = Geocoder(this, Locale.getDefault());
listadress =geocoder.getFromLocation(locationy,locationx,1)
var adress=listadress[0].getAddressLine(0)
adresstext.text=adress
text=deneme
mainlink="http://maps.google.com/maps?q="+locationy+","+locationx+""
mainlink=mainlink+" "+text
text=mainlink
}
}
else
{
val permission= arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION)
ActivityCompat.requestPermissions(this, permission,0)
}
一切都还好,但是当它定位时离我200m远。如何使其更准确?
我将设备的经度和纬度作为参数,但是当我在Google map 中显示时,它给出了200 m的位置,因此我检查了经度和谷歌 map 。
而纬度和经度上的失败离我很远。
最佳答案
请尝试一下!
private fun requestLocationUpdates() {
var mLocationRequest = LocationRequest()
mLocationRequest!!.setInterval(AppConstants.LOCATION_INTERVAL.toLong())
mLocationRequest!!.setFastestInterval(AppConstants.FASTEST_LOCATION_INTERVAL.toLong())
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
var client: FusedLocationProviderClient =
LocationServices.getFusedLocationProviderClient(applicationContext)
val permission = ContextCompat.checkSelfPermission(
applicationContext,
Manifest.permission.ACCESS_FINE_LOCATION
)
if (permission == PackageManager.PERMISSION_GRANTED) {
// Request location updates and when an update is
client.requestLocationUpdates(mLocationRequest, object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
val location = locationResult!!.lastLocation
if (location != null) {
latitude = location!!.latitude
longitude = location!!.longitude
AppConstants.CURRENT_LATITUDE = location!!.latitude
AppConstants.CURRENT_LONGITUDE = location!!.longitude
}
}
}, null)
}
}
class AppConstants {
var CURRENT_LATITUDE = 0.0
var CURRENT_LONGITUDE = 0.0
}