简体   繁体   中英

Android location problems

I read posts like this, android getlastknownlocation returns null , but I have a problem apart from the known location.

i know that if i don't have any provider, i can't have any known location, getLastKnowedLocation(). but, How i can get the location when the provider is detected?

i use this code:

 public void myPosition() {
    if (mapa.getMyLocation() != null)
        mapa.animateCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng( mapa.getMyLocation().getLatitude(), mapa.getMyLocation().getLongitude()), 13));
    else{
        loca = locaMan.getLastKnownLocation(bestProvi);

        if (loca != null)
        mapa.moveCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng( loca.getLatitude(), loca.getLongitude()), 15));

    }
}
public void onLocationChanged(Location location) {
    myPosition();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    myPosition();
}

@Override
public void onProviderEnabled(String provider) {
    myPosition();
    Toast toast1 =Toast.makeText(getApplicationContext(),"Provider ON", Toast.LENGTH_SHORT);
    toast1.show();
}

@Override
public void onProviderDisabled(String provider) {
    Toast toast1 =Toast.makeText(getApplicationContext(),"Provider OFF", Toast.LENGTH_SHORT);
    toast1.show();
}

But when the provider is activated, it isn't execute the code of method onProviderEnabled(), only when i turn off and turn on GPS on my smartphone.

When i start the aplication, i see a map, and 5 seconds after i see a blue point over my position, but myPosition() method isn't executed.

How i can do this?

Thank you for all.

I was trying to search something about that and i found the metod requestLocationUpdates() in these links: 1 2 (Spanish), but it didn't work in my project.

So i had to search more but from other way, because my problem wasn't the location, but the camera movement to this location. i found the TimerTask library and i used it.

I add this code into the other code:

 public class TimerClass extends TimerTask{
        public void run () {
            runOnUiThread(new Runnable() {
                @Override
                public void run () {
                    if(!pass) {
                        myPosition();
                    }
                }
            });
        }
}

it is a class into the MainActivity class.

i don't know if it is correct, but it runs without any problem. If someone know other way for fix this problem, answer this comment please.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM