Currently I want to get the user current location of latitude and longitude but my code can get until I pressed the send button in the emulator setting but what I want is to auto get the location and set it to the textview so what should I do? Anyone please advice me to solve this problem please because I really not familiar with the location code.
Here's my code
package com.example.rex.ota30;
import android.*;
import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import static com.example.rex.ota30.R.id.textView;
public class Search extends AppCompatActivity implements LocationListener {
private FirebaseDatabase db;
private DatabaseReference sref, lref;
private TextView searchun, searchlo, searchla;
private String un, la, lo;
private LocationManager lm;
private LocationListener ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
db = FirebaseDatabase.getInstance();
sref = db.getReference("users");
lref = db.getReference("userslocation");
searchun = (TextView) findViewById(R.id.sun);
Intent i = this.getIntent();
un = i.getStringExtra("username");
searchun.setText(un);
searchlo = (TextView) findViewById(R.id.slo);
searchla = (TextView) findViewById(R.id.sla);
lm = (LocationManager) getSystemService(Context.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) {
return;
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) this);
}
@Override
public void onLocationChanged(Location location) {
la = String.valueOf(location.getLatitude());
searchla.setText(la);
lo = String.valueOf(location.getLongitude());
searchlo.setText(lo);
lref.child(un).child("latitude").setValue(la);
lref.child(un).child("longitude").setValue(lo);
}
@Override
public void onProviderDisabled(String provider) {
Log.d("Latitude","disable");
}
@Override
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude","status");
}
}
OnLocationChanged is only called when the location has changed.
So the location is displayed in the Textview only when the send button is pressed.
If you want to display the location from the beginning, try this
protected void onCreate(Bundle savedInstanceState) {
...
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) this);
...
// add code
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
la = String.valueOf(location.getLatitude());
searchla.setText(la);
lo = String.valueOf(location.getLongitude());
searchlo.setText(lo);
}