Es gibt zwei Konten in meiner App, Familie und ältere.Ich habe die Konten in einem Paar.Und ich möchte den Standort der älteren auf den Familienaccount abrufen, und zeigen Sie es in einer Karte. Jetzt habe ich Probleme mit dem Breiten- und Längengrad. Der Standort kann nicht ordnungsgemäß auf Firebase aktualisiert werden. Insbesondere wenn ich gehe, kann die App den Standort nicht abrufen. Hier ist die Datenbankstruktur.android - Breitengrad und Längengrad nicht richtig erhalten
Struktur Firebase
Ältere Seite:
public class MainActivity extends Activity implements
android.location.LocationListener {
LocationManager locationManager;
String provider;
private FirebaseUser user;
private FirebaseAuth mAuth;
private DatabaseReference mDatabase;
private ImageButton helpButton;
private Button logout;
private double longitude;
private double latitude;
private String userId = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helpButton = (ImageButton) findViewById(R.id.help);
logout = (Button) findViewById(R.id.logout);
mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference();
user = mAuth.getCurrentUser();
helpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, HelpActivity.class));
}
});
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(MainActivity.this, HomeActivity.class));
}
});
if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Check Permissions Now
ActivityCompat.requestPermissions(this,
new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
0);
}
// Getting LocationManager object
statusCheck();
locationManager = (LocationManager) getSystemService(
Context.LOCATION_SERVICE);
// Creating an empty criteria object
Criteria criteria = new Criteria();
// Getting the name of the provider that meets the criteria
provider = locationManager.getBestProvider(criteria, false);
if (provider != null && !provider.equals("")) {
if (!provider.contains("gps")) { // if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
// Get the location from the given provider
Location location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 0, this);
if (location != null)
onLocationChanged(location);
else
location = locationManager.getLastKnownLocation(provider);
if (location != null)
onLocationChanged(location);
else
Toast.makeText(getBaseContext(), "Location can't be retrieved",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "No Provider Found",
Toast.LENGTH_SHORT).show();
}
}
public void statusCheck() {
final LocationManager manager = (LocationManager) getSystemService(
Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
"Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false).setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog,
final int id) {
startActivity(new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog,
final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
@Override
public void onLocationChanged(Location location) {
// Getting reference to TextView tv_longitude
TextView tvLongitude = (TextView) findViewById(R.id.tv_longitude);
// Getting reference to TextView tv_latitude
TextView tvLatitude = (TextView) findViewById(R.id.tv_latitude);
// Setting Current Longitude
tvLongitude.setText("Longitude:" + location.getLongitude());
// Setting Current Latitude
tvLatitude.setText("Latitude:" + location.getLatitude());
latitude = location.getLatitude();
longitude = location.getLongitude();
mAuth = FirebaseAuth.getInstance();
user = mAuth.getCurrentUser();
//save text in edittext into the firebase
if (!String.valueOf(latitude).equals(""))
mDatabase.child("users").child(user.getUid()).child("latitude").setValue(latitude);
if (!String.valueOf(longitude).equals(""))
mDatabase.child("users").child(user.getUid()).child("longitude").setValue(longitude);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Check Permissions Now
ActivityCompat.requestPermissions(this,
new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
0);
}
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
Familie Seite:
public class MainActivity_Family extends FragmentActivity implements
OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
GoogleMap.OnMarkerDragListener,
GoogleMap.OnMapLongClickListener,
View.OnClickListener {
private Button logout;
private ImageButton buttonCurrent;
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
private FirebaseUser user;
private double longitude;
private double latitude;
private String currentUserId;
private com.google.firebase.database.Query mQueryMF;
private TextView tt;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__family);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mAuth = FirebaseAuth.getInstance();
user = mAuth.getCurrentUser();
currentUserId = user.getUid();
mDatabase = FirebaseDatabase.getInstance().getReference();
logout = (Button) findViewById(R.id.logout);
buttonCurrent = (ImageButton) findViewById(R.id.buttonCurrent);
tt = (TextView) findViewById(R.id.tt);
buttonCurrent.setOnClickListener(this);
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(MainActivity_Family.this, HomeActivity.class));
}
});
}
private void getCurrentLocation() {
mQueryMF = mDatabase.child("users").orderByChild("familyId").equalTo(currentUserId);
mQueryMF.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
latitude = userSnapshot.child("latitude").getValue(Double.class);
longitude = userSnapshot.child("longitude").getValue(Double.class);
}
//String to display current latitude and longitude
String msg = latitude + ", " + longitude;
tt.setText(msg);
//Creating a LatLng Object to store Coordinates
LatLng latLng = new LatLng(latitude, longitude);
//Adding marker to map
mMap.addMarker(new MarkerOptions()
.position(latLng) //setting position
.draggable(true) //Making the marker draggable
.title("Current Location")); //Adding a title
//Moving the camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//Animating the camera
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng latLng = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(latLng).draggable(true));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.setOnMarkerDragListener(this);
mMap.setOnMapLongClickListener(this);
}
@Override
public void onConnected(Bundle bundle) {
getCurrentLocation();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onMapLongClick(LatLng latLng) {
//Clearing all the markers
mMap.clear();
//Adding a new marker to the current pressed position
mMap.addMarker(new MarkerOptions()
.position(latLng)
.draggable(true));
}
@Override
public void onMarkerDragStart(Marker marker) {
}
@Override
public void onMarkerDrag(Marker marker) {
}
@Override
public void onMarkerDragEnd(Marker marker) {
//Getting the coordinates
latitude = marker.getPosition().latitude;
longitude = marker.getPosition().longitude;
}
@Override
public void onClick(View v) {
if (v == buttonCurrent) {
getCurrentLocation();
}
}
}
Service:
public static final String BROADCAST_ACTION = "Hello World";
private static final int TWO_MINUTES = 1000 * 60 * 2;
public LocationManager locationManager;
public MyLocationListener listener;
public Location previousBestLocation = null;
private FirebaseUser user;
private FirebaseAuth mAuth;
private DatabaseReference mDatabase;
private ImageButton helpButton;
private Button logout;
Intent intent;
int counter = 0;
@Override
public void onCreate()
{
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference();
user = mAuth.getCurrentUser();
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(MainActivity.this, HomeActivity.class));
}
});
}
@Override
public void onStart(Intent intent, int startId)
{
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return true;
}
return false;
}
/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
@Override
public void onDestroy() {
// handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
Log.v("STOP_SERVICE", "DONE");
locationManager.removeUpdates(listener);
}
public static Thread performOnBackgroundThread(final Runnable runnable) {
final Thread t = new Thread() {
@Override
public void run() {
try {
runnable.run();
} finally {
}
}
};
t.start();
return t;
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(final Location loc)
{
if(isBetterLocation(loc, previousBestLocation)) {
loc.getLatitude();
loc.getLongitude();
mAuth = FirebaseAuth.getInstance();
user = mAuth.getCurrentUser();
//save text in edittext into the firebase
if (!String.valueOf(loc.getLatitude()).equals(""))
mDatabase.child("users").child(user.getUid()).child("latitude").setValue(loc.getLatitude());
if (!String.valueOf(loc.getLongitude()).equals(""))
mDatabase.child("users").child(user.getUid()).child("longitude").setValue(loc.getLongitude());
sendBroadcast(intent);
}
}
public void onProviderDisabled(String provider)
{
Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String provider)
{
Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
Haben Sie einen Fehler haben? Wenn Sie versuchen, 'Log.d (" TAG ", msg) zu verwenden;', was ist die Ausgabe? –