M
M
Masteron2016-05-20 05:00:32
Java
Masteron, 2016-05-20 05:00:32

location android?

public class MapsActivity extends AppCompatActivity implements LocationListener {

    GoogleMap map;
    double lon = 0;
    double lat = 0;
    LocationManager lm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        locationWork();
        createMap();

        createMap();
    }

    private void locationWork() {
        lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(MapsActivity.this, "Включите GPS и откройте интернет подключение", Toast.LENGTH_SHORT).show();
            return;
        }
        lm.requestLocationUpdates(lm.NETWORK_PROVIDER,0,0,this);

    }

    private void createMap() {
        map = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();
        if (map == null) {
            Toast.makeText(this, "NULL MAP!", Toast.LENGTH_SHORT).show();
        }

    }
    private void addMarker(double lon, double lat){
        map.addMarker(new MarkerOptions()
                .position(new LatLng(lon, lat))
                .title("Hello world"));

    }


    @Override
    public void onLocationChanged(Location location) {
        lon = location.getLongitude();
        lat = location.getLatitude();
        addMarker(lon,lat);

    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

There is such a code, how can I not find out the location location is always null what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kutu1, 2018-05-21
@kutu1

Did you specify permissions in the manifest?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question