GPS points are not retrieved properly

Im new to Android development, and I understand the android activity life cycle. See the following code.

public class MyTest extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cont = getApplicationContext(); final MyLocation my_loc = new MyLocation (); my_loc.initialize(cont); myMethod(); } Public void myMethod() { //here when I retrieve the values, it always shows 0.0. } } public class MyLocation implements LocationListener { public double user_lat; public double user_lng; private LocationManager locationManager; private Context ctx; @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub user_lat = location.getLatitude(); user_lng = location.getLongitude(); //here i save the values for constands to use in myMethod } } public void initialize(Context context) { this.ctx = context; locationManager = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, this); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } } 

Why can't I initialize GPS and retrieve the long and lat values ​​in the same activity?

+1
source share
3 answers

LocationClient is a new way to get GPS. For more information about the recent update, see.

Please note that the LocationManager's way of doing things is buggy, as you need to add a lot of code for the GPS provider and NETWORK. The new method (internal to Google, called the Fused Location Provider) works with sensors to significantly reduce battery consumption. Also reduces the need for a sophisticated API and selectively select the best provider. Its only 2-3 lines of code, and you're done.

With many Samsung phones (including the Y-model), although there is a definite problem that in most cases they do not return the location at all. So you need to run this phone to get GPS back. For this you can use

 HomeScreen.getLocationManager().requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Override public void onLocationChanged(final Location location) { } }); 

And then name your locationClient.getLastLocation api. As in the above code, before , you will call your LocationClient.getLastLocation or LocationManager.getLastKnownLocation request.

Remember Samsung is a highly customizable, open source Android product. Google cannot respond to Samsung-related errors, and Samsung has no support from Android developers.

Edit: watch the video, trust me, not knowing what LocationClient gives you, you will not appreciate the change. You will also learn about GeoFenceing.

+3
source

To ask a question about your location, I offer you a new way to search for locations.

Check this out: google play util

The code below is just a snippet, not a complete one;). You no longer need to contact gps / network providers. It treats you which one is the best and returns the location.

 LocationClient mLocationClient = new LocationClient(...) mLocationClient.connect(); ... onConnected(Bundle ...){ LocationRequest request = LocationRequest.create(); request.setNumUpdates(1); ... mLocationClient.requestLocationUpdates(request, new LocationListener....) .... } 
+1
source

I have done one service for this. Easy to use Longitude / Latitude.

Copy / paste this class into your project.

 package com.sample; import com.sample.globalconstant; import android.app.Service; import android.content.Context; import android.content.Intent; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.widget.Toast; public class MyServiceGPS extends Service { private static final String TAG = "BOOMBOOMTESTGPS"; private LocationManager mLocationManager = null; private static final int LOCATION_INTERVAL = 1000; private static final float LOCATION_DISTANCE = 10f; private class LocationListener implements android.location.LocationListener{ Location mLastLocation; public LocationListener(String provider) { Log.e(TAG, "LocationListener " + provider); mLastLocation = new Location(provider); } public void onLocationChanged(Location location) { Log.e(TAG, "onLocationChanged: " + location.getLatitude() +"....."+ location.getLongitude()); globalconstant.lat = location.getLatitude(); globalconstant.lon = location.getLongitude(); Toast.makeText(getApplicationContext(), location.getLatitude() +"....."+ location.getLongitude(), 1000).show(); mLastLocation.set(location); } public void onProviderDisabled(String provider) { Log.e(TAG, "onProviderDisabled: " + provider); } public void onProviderEnabled(String provider) { Log.e(TAG, "onProviderEnabled: " + provider); } public void onStatusChanged(String provider, int status, Bundle extras) { Log.e(TAG, "onStatusChanged: " + provider); } } LocationListener[] mLocationListeners = new LocationListener[] { new LocationListener(LocationManager.GPS_PROVIDER), new LocationListener(LocationManager.NETWORK_PROVIDER) }; @Override public IBinder onBind(Intent arg0) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG, "onStartCommand"); super.onStartCommand(intent, flags, startId); return START_STICKY; } @Override public void onCreate() { Log.e(TAG, "onCreate"); initializeLocationManager(); try { mLocationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, mLocationListeners[1]); } catch (java.lang.SecurityException ex) { Log.i(TAG, "fail to request location update, ignore", ex); } catch (IllegalArgumentException ex) { Log.d(TAG, "network provider does not exist, " + ex.getMessage()); } try { mLocationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, mLocationListeners[0]); } catch (java.lang.SecurityException ex) { Log.i(TAG, "fail to request location update, ignore", ex); } catch (IllegalArgumentException ex) { Log.d(TAG, "gps provider does not exist " + ex.getMessage()); } } @Override public void onDestroy() { Log.e(TAG, "onDestroy"); super.onDestroy(); if (mLocationManager != null) { for (int i = 0; i < mLocationListeners.length; i++) { try { mLocationManager.removeUpdates(mLocationListeners[i]); } catch (Exception ex) { Log.i(TAG, "fail to remove location listners, ignore", ex); } } } } private void initializeLocationManager() { Log.e(TAG, "initializeLocationManager"); if (mLocationManager == null) { mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); } } } 

Copy this code into your activity if you want to start:

 startService(new Intent(this,MyServiceGPS.class)); 

Create one globalconstant class:

 public class globalconstant { public static double lat, lon; } 

when you want the current latitude and longitude in your project to write only this globalconstant.lat ,globalconstant.lon

Add uses-permission in Manifest

0
source

Source: https://habr.com/ru/post/950416/


All Articles