Reputation: 11749
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.
Upvotes: 1
Views: 2239
Reputation: 1054
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.
Upvotes: 1
Reputation: 11749
Thanks to @luke_mclachlan, I found the method I was looking for on the google map github.
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.
Upvotes: 0