1

I am using the new Android place Api to get autocomplete predictions while the user type.

From what I saw so far the API takes a LatLngBounds object which is created using two locations.

Is there a way to generate a LatLngBounds object using one LatLng as a center point and a radius?

Thank you.

Distwo
  • 11,569
  • 8
  • 42
  • 65
  • LatLngBounds is a square bounding box, rather than circular - how did you want to inset the circle and square? – ianhanniballake Apr 10 '15 at 01:22
  • something along the lines of 'spherical.computeOffset'. i've done this recently, take a latlng to create a viewport. let me know if this is what you were thinking. – luke_mclachlan Apr 10 '15 at 21:56
  • @luke_mclachlan yes something like that but in java. I am trying to remember my trigonometry classes and building that up. – Distwo Apr 10 '15 at 22:05
  • I have a code that is in javascript, i'll post it as an answer just in case it's useful for you. it's fully working code. – luke_mclachlan Apr 10 '15 at 22:08
  • 4
    Possible duplicate of [How to convert a LatLng and a radius to a LatLngBounds in Android Google Maps API v2?](http://stackoverflow.com/questions/15319431/how-to-convert-a-latlng-and-a-radius-to-a-latlngbounds-in-android-google-maps-ap) – Eugene Berdnikov Oct 04 '16 at 00:08

2 Answers2

1

As promised Distwo, here is how I am creating a viewport based on a single latitude and longitude. The reason for me wanting to do this is that my store locator script searches from the viewport returned by the Places API, and so if a viewport isn't returned (rare but it happens) I go ahead and create one from the lat and lng returned by places. Please note that I am not using the android app, this is for regular web viewing.

console.log("no viewport found so lets spherically compute the bounds");
var sw = google.maps.geometry.spherical.computeOffset(place.geometry.location, 1609, 225);
var ne = google.maps.geometry.spherical.computeOffset(place.geometry.location, 1609, 45);
swlat = sw.lat().toFixed(6);
swlng = sw.lng().toFixed(6);
nelat = ne.lat().toFixed(6);
nelng = ne.lng().toFixed(6);

Not sure this is of use to you. In my case the radius is fixed, but in your situation it sounds though it's a variable and hence you'll have to refer to it as a variable.

OK so a little edit: I've changed 1423 to 1000, which represents a lilometer radius. If you're using kilometers then 1000 is the number to use, but if you're using miles then use "1609.3440006" instead.

luke_mclachlan
  • 1,035
  • 1
  • 15
  • 35
  • 1
    Thanks to you, I found a github for java that has the computeOffset! I will post it bellow. – Distwo Apr 10 '15 at 22:41
0

Thanks to @luke_mclachlan, I found the method I was looking for on the google map github.

https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/SphericalUtil.java

Here is the method I was looking for:

public static LatLng computeOffset(LatLng from, double distance, double heading) {
        distance /= EARTH_RADIUS;
        heading = toRadians(heading);
        // http://williams.best.vwh.net/avform.htm#LL
        double fromLat = toRadians(from.latitude);
        double fromLng = toRadians(from.longitude);
        double cosDistance = cos(distance);
        double sinDistance = sin(distance);
        double sinFromLat = sin(fromLat);
        double cosFromLat = cos(fromLat);
        double sinLat = cosDistance * sinFromLat + sinDistance * cosFromLat * cos(heading);
        double dLng = atan2(
                sinDistance * cosFromLat * sin(heading),
                cosDistance - sinFromLat * sinLat);
        return new LatLng(toDegrees(asin(sinLat)), toDegrees(fromLng + dLng));
    }

Note that Heart radius here is expected in meters.

Using this method, I find the southwest (heading = 225) and northeast (heading = 45) coordinates and then create my LatLongBounds object.

Distwo
  • 11,569
  • 8
  • 42
  • 65