Am I returning my current location correctly?

92 Views Asked by At

When I enter coordnates manualy, my returning JSON is correct:

{
   "destination_addresses" : [ "Warsaw, Poland" ],
   "origin_addresses" : [ "Modra 36, 71-220 Szczecin, Poland" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "572 km",
                  "value" : 571508
               },
               "duration" : {
                  "text" : "5 hours 24 mins",
                  "value" : 19434
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

But when I use Location class to get latitude and longitude, it returns me 14 digit lat and long, and when I ask api, I get this:

{
   "destination_addresses" : [ "Warsaw, Poland" ],
   "origin_addresses" : [ "37.421998333333335,-122.08400000000002" ],
   "rows" : [
      {
         "elements" : [
            {
               "status" : "ZERO_RESULTS"
            }
         ]
      }
   ],
   "status" : "OK"
}

I get that this might be incorrect format. I noticed that lat/long have 6 decimal places. I already tried cut String but the result is the same.

Method I use to retreive current location and coordinates:

 private Location getCurrentLocation() {
    if(ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ContextCompat.checkSelfPermission(context,  Manifest.permission.ACCESS_FINE_LOCATION);
        return null;
    }

    LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
    return lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}

private String getCurrentCoordinate() {
    Location location = getCurrentLocation();
    return location.getLatitude()+","+ location.getLongitude(); // returns 37.421998333333335,-122.08400000000002

}

and called here:

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.distance_id);
    distanceViewModel = new ViewModelProvider(this).get(DistanceViewModel.class);

    subscribeObservers();

}

private void subscribeObservers()
{
    distanceViewModel.getDistanceResponseModel().observe(this, new Observer<DistanceResponseModel>() {
        @Override
        public void onChanged(DistanceResponseModel distanceResponseModel) {
            textView.setText(showDestination(distanceResponseModel));
        }
    });
}

I'm using MVVM with ViewModel:

public class DistanceViewModel extends ViewModel {

private LiveData<DistanceResponseModel> distanceLiveData;
private DistanceRepository distanceRepository;
private Context context;

public DistanceViewModel() {
    secretValue = context.getString(R.string.google_maps_api_key);
    distanceRepository = DistanceRepository.getInstance();
    distanceLiveData = distanceRepository.getDistanceResponseModel();
    //First argument is your origin, second is your destination. Third is API key.
    distanceRepository.distanceResponseAPI(getCurrentCoordinate(), "54.5188898,18.5305409", secretValue);
}

public LiveData<DistanceResponseModel> getDistanceResponseModel() {
    return distanceLiveData;
}

Quite standard retorfit query with RxJava thread handling:

    @GET("maps/api/distancematrix/json")
Single<DistanceResponseModel> getDistanceModelListRx(@Query("origins") String origins,
                                                     @Query("destinations") String destinations,
                                                     @Query("key") String key);

Is this correct approach? Is there anything else I can use to inject lat and long from function into my url? Any help is much appreciated.

0

There are 0 best solutions below