onLocationChanged 方法在 Android 中不起作用



我正在尝试获取用户位置并在地图上用标记显示它,但是位置更改的方法不起作用..位置只能手动设置,我的意思是我必须将经度和纬度设置为分配的值,并且无法从用户位置获取它

以下是日志猫屏幕: https://i.stack.imgur.com/QkqsU.jpg

这是我的代码

package com.example.user.maps1
import android.content.Context
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.util.Log
import android.widget.Toast
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mMap: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready         to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
getPermission()
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Add a marker in Sydney and move the camera
val sydney = LatLng(-34.0, 151.0)
mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
}
var accessCode = 222
fun getPermission(){
if(Build.VERSION.SDK_INT >= 23){
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
requestPermissions(arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION) , accessCode)
return
}
getUserLocation()
}
}
fun getUserLocation(){
var myLocation = mylocationListener()
var locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager

if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3, 3f, myLocation)
}

var mythread=myThread()
mythread.start()

}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when(requestCode){
accessCode->{
if (grantResults[0]==PackageManager.PERMISSION_GRANTED){
getUserLocation()
}else{
Toast.makeText(this,"We cannot access to your location",Toast.LENGTH_LONG).show()
}
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
var location:Location?=null
inner class mylocationListener: LocationListener {
constructor(){
location= Location("Start")
//location!!.longitude=0.0
//location!!.latitude=0.0
}
override fun onLocationChanged(p0: Location?) {
location=p0
}
override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) {
//TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onProviderEnabled(p0: String?) {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onProviderDisabled(p0: String?) {
//TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
inner class myThread:Thread{
constructor():super(){}
override fun run(){
try {
runOnUiThread {
mMap!!.clear()
// show me
val sydney = LatLng(location!!.latitude/*55.7558*/, location!!.longitude/*37.6173*/)
mMap!!.addMarker(MarkerOptions()
.position(sydney)
.title("Hello WOOORLD")
.snippet(" here is my location")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.mario)))
mMap!!.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 12f))
}
Thread.sleep(10000)
}catch (ex:Exception){
Log.e("00000000000000",ex.message.toString())
//println(ex.message.toString())
}

}
}

}

如果您需要一次位置,可以使用以下类。你只需要这样称呼它

SingleShotLocationProvider.requestSingleUpdate(context, this);

public class SingleShotLocationProvider {
public static interface LocationCallback {
public void onNewLocationAvailable(GPSCoordinates location);
}
// calls back to calling thread, note this is for low grain: if you want higher precision, swap the
// contents of the else and if. Also be sure to check gps permission/settings are allowed.
// call usually takes <10ms
public static void requestSingleUpdate(final Context context, final LocationCallback callback) {
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isNetworkEnabled) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
//    ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
//   public void onRequestPermissionsResult(int requestCode, String[] permissions,
//                                          int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestSingleUpdate(criteria, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(new GPSCoordinates(location.getLatitude(), location.getLongitude()));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}, null);
} else {
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
locationManager.requestSingleUpdate(criteria, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(new GPSCoordinates(location.getLatitude(), location.getLongitude()));
}
@Override public void onStatusChanged(String provider, int status, Bundle extras) { }
@Override public void onProviderEnabled(String provider) { }
@Override public void onProviderDisabled(String provider) { }
}, null);
}
}
}

// consider returning Location instead of this dummy wrapper class
public static class GPSCoordinates {
public float longitude = -1;
public float latitude = -1;
public GPSCoordinates(float theLatitude, float theLongitude) {
longitude = theLongitude;
latitude = theLatitude;
}
public GPSCoordinates(double theLatitude, double theLongitude) {
longitude = (float) theLongitude;
latitude = (float) theLatitude;
}
}
}

我做到了,但用了另一种方式

本教程非常有帮助:https://github.com/codepath/android_guides/wiki/Retrieving-Location-with-LocationServices-API

相关内容

  • 没有找到相关文章

最新更新