Search code examples
androiddictionarygeolocation

How to fix ' java.lang.IllegalArgumentException: Not a valid geo location: -118.2541117, 33.985805'


I'm setting up a current location in my application.But it crashed

at com.example.webforest.quickaidlikeuber2nd.CustomerMapActivity.getDriversAround(CustomerMapActivity.java:577)

 boolean getDriversAroundStarted = false;
List<Marker> markers = new ArrayList<Marker>();
private void getDriversAround(){
    getDriversAroundStarted = true;
    DatabaseReference driverLocation = FirebaseDatabase.getInstance().getReference().child("driversAvailable");

    GeoFire geoFire = new GeoFire(driverLocation);
    GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(mLastLocation.getLongitude(), mLastLocation.getLatitude()), 999999999);

at com.example.webforest.quickaidlikeuber2nd.CustomerMapActivity.access$2300(CustomerMapActivity.java:76)

public class CustomerMapActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
Location mLastLocation;
LocationRequest mLocationRequest;

private FusedLocationProviderClient mFusedLocationClient;

private Button mLogout, mRequest, mSettings, mHistory;

private Boolean requestBol = false;

private Marker pickupMarker;

private LatLng pickupLocation;

private SupportMapFragment mapFragment;

private String destination, requestService;

private LatLng destinationLatLng;

private LinearLayout mDriverInfo;

private ImageView mDriverProfileImage;

private TextView mDriverName, mDriverPhone, mDriverCar;

private RadioGroup mRadioGroup;

private RatingBar mRatingBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_customer_map);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

    mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    destinationLatLng = new LatLng(0.0, 0.0);

    mDriverInfo = (LinearLayout) findViewById(R.id.driverInfo);

    mDriverProfileImage = (ImageView) findViewById(R.id.driverProfileImage);

    mDriverName = (TextView) findViewById(R.id.driverName);
    mDriverPhone = (TextView) findViewById(R.id.driverPhone);
    mDriverCar = (TextView) findViewById(R.id.driverCar);

    mRatingBar = (RatingBar) findViewById(R.id.ratingBar);

    mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
    mRadioGroup.check(R.id.UberX);

    mLogout = (Button) findViewById(R.id.logout);
    mRequest = (Button) findViewById(R.id.request);
    mSettings = (Button) findViewById(R.id.settings);
    mHistory = (Button) findViewById(R.id.history);

    mLogout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FirebaseAuth.getInstance().signOut();
            Intent intent = new Intent(CustomerMapActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
            return;
        }
    });

    mRequest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (requestBol) {
                endRide();


            } else {
                int selectId = mRadioGroup.getCheckedRadioButtonId();

                final RadioButton radioButton = (RadioButton) findViewById(selectId);

                if (radioButton.getText() == null) {
                    return;
                }

                requestService = radioButton.getText().toString();

                requestBol = true;

                String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();

                DatabaseReference ref = FirebaseDatabase.getInstance().getReference("customerRequest");
                GeoFire geoFire = new GeoFire(ref);
                geoFire.setLocation(userId, new GeoLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude()));

                pickupLocation = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
                pickupMarker = mMap.addMarker(new MarkerOptions().position(pickupLocation).title("Pickup Here").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_pickup)));

                mRequest.setText("Getting your Driver....");

                getClosestDriver();
            }
        }
    });
    mSettings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(CustomerMapActivity.this, CustomerSettingsActivity.class);
            startActivity(intent);
            return;
        }
    });

    mHistory.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(CustomerMapActivity.this, HistoryActivity.class);
            intent.putExtra("customerOrDriver", "Customers");
            startActivity(intent);
            return;
        }
    });

    PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            destination = place.getName().toString();
            destinationLatLng = place.getLatLng();
        }

        @Override
        public void onError(Status status) {
            // TODO: Handle the error.
        }
    });


}

it said at com.example.webforest.quickaidlikeuber2nd.CustomerMapActivity$10.onLocationResult(CustomerMapActivity.java:512)

 LocationCallback mLocationCallback = new LocationCallback(){
    @Override
    public void onLocationResult(LocationResult locationResult) {
        for(Location location : locationResult.getLocations()){
            if(getApplicationContext()!=null){
                mLastLocation = location;

                LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());

                //mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                //mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
                if(!getDriversAroundStarted)
                    getDriversAround();
            }
        }
    }
};

Following this error which i'm getting

java.lang.IllegalArgumentException: Not a valid geo location: -118.2541117, 33.985805
    at com.firebase.geofire.GeoLocation.<init>(GeoLocation.java:51)
    at com.example.webforest.quickaidlikeuber2nd.CustomerMapActivity.getDriversAround(CustomerMapActivity.java:577)
    at com.example.webforest.quickaidlikeuber2nd.CustomerMapActivity.access$2300(CustomerMapActivity.java:76)
    at com.example.webforest.quickaidlikeuber2nd.CustomerMapActivity$10.onLocationResult(CustomerMapActivity.java:512)
    at com.google.android.gms.internal.location.zzau.notifyListener(Unknown Source)
    at com.google.android.gms.common.api.internal.ListenerHolder.notifyListenerInternal(Unknown Source)
    at com.google.android.gms.common.api.internal.ListenerHolder$zaa.handleMessage(Unknown Source)

Solution

  • It looks like you have the latitude and longitude in the wrong order.

    The error message Not a valid geo location: -118.2541117, 33.985805 shows the latitude followed by the longitutude. A latitude of -118 degrees makes no sense. Latitude values must be in the range -90 to +90 degrees.


    Please tell me how can solve this.

    Carefully examine your code to see where the latitude and longitude have been switched around.

    It looks like it might be here:

      GeoQuery geoQuery = geoFire.queryAtLocation(
              new GeoLocation(mLastLocation.getLongitude(),
                              mLastLocation.getLatitude()), 999999999);
    

    You can verify that using the debugger.