I want to compare latitude and longitude from a parsed JSON GET. In order to compare the lat/lng from the GET I need to have to devices current location lat/lng before the GET happens.
I have a method that uses onMyLocationButtonClick and it works beautifully. What I need, however, is for this same method to execute onMapReady so I can compare the devices current lat/lng with what is parsed from the JSON GET response.
This works marvelously on click even on a wiped emulator, the issue however is getting and assigning the the devices current lat/lng values BEFORE the call to the API. Using the onClick method, these values are only obtained onClick and that happens after the API call is made and JSON parsed.
I have tried simply making a call to onMyLocationClick(Location location)
but I get an error "can't resolve location symbol". I think I have to assign a locationListener to location but I'm not quite sure how to do that. After a while all the reading and research becomes soup, so now I'm tapping out and asking for help. :)
The end result would be onMapReady:
- Location permissions are verified and requested, if needed.
- On permission verified, zoom to current device location on map and assign current lat/lng values lat/lng variables.
- Pass lat/lng variable values with API call to compare current lat/lng to lat/lng values retrieved from the API.
- I believe this can be done with getRetrofit(lat, lng) the same as with my call to setAddress.
Any assistance is greatly appreciated.
onMapReady method:
public void onMapReady(GoogleMap map) {
mMap = map;
mMap.setOnMyLocationButtonClickListener(this);
mMap.setOnMyLocationClickListener(this);
enableMyLocation(); // checks for fine and coarse location permissions
// onMyLocationClick(Location location);
getRetrofit(); //call to API
}
The method I want to execute onMapReady before the call to the API without having to click the location button:
@Override
public void onMyLocationClick(Location location) {
// get device current lat/lng
this.lat = location.getLatitude();
this.lng = location.getLongitude();
setAddress(lat, lng); // method to reverse geocode to physical address
// zoom to current location on map
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude())) // Sets the center of the map to location user
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}