0

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:

  1. Location permissions are verified and requested, if needed.
  2. On permission verified, zoom to current device location on map and assign current lat/lng values lat/lng variables.
  3. 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));
    }
c1scok1d
  • 57
  • 1
  • 9

1 Answers1

0

in your onCreate() or where you need it to run then move the logic from your onMyLocationClick to a new function and call it getMyLocation before you call onMapReady like in onCreate() and your onMyLocationClick



GoogleMap map = getMyLocation(location)
onMapReady(map)

public map getMyLocation(Location location){
   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));
}

Chooven
  • 105
  • 5
  • Thank you for your reply Chooven! How do I get an assignment for the original location in `GoogleMap map = getMyLocation(location)`? The compiler is telling me it cannot resolve the symbol `location`. – c1scok1d Oct 22 '19 at 01:14
  • Ahh, to get your current location have look at this thread, it help me [link](https://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android) – Chooven Oct 22 '19 at 16:54