0

In my Android application I want to request data for the location where the user touches the map. GoogleMap.OnMapClickListener provides the touched position as latitude and longitude coordinates.

public abstract void onMapClick(LatLng point)

In order to pass an area instead of a point I need to calculate the coordinates for a bounding box centered at the touch position. The extend of the bounding box should not depend on the zoom level of the map.

I do not want to request the bounding box of the visible screen - just a small bounding box area around the touch position.

Is there any framework method I could use? Otherwise, how do I find a suitable distance value for the extend of the bounding box?

JJD
  • 50,076
  • 60
  • 203
  • 339

3 Answers3

0

Isn't LatLngBounds what you need?

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
0

You can override onMarkerClick like below

@Override
public boolean onMarkerClick(Marker marker) {

    if (markerClicked) {

        if (polygon != null) {
            polygon.remove();
            polygon = null;
        }

        polygonOptions.add(marker.getPosition());

        polygonOptions.strokeColor(ContextCompat.getColor(getContext(), R.color.colorRedTransparent));
        polygonOptions.fillColor(ContextCompat.getColor(getContext(), R.color.colorBlueTransparent));

        polygonOptions.strokeWidth(5.0f);
        polygon = mMap.addPolygon(polygonOptions);
        polygon.setClickable(true);

    } else {
        if (polygon != null) {
            polygon.remove();
            polygon = null;
        }

        polygonOptions = new PolygonOptions().add(marker.getPosition());
        markerClicked = true;
    }

    return true;
}

Pass your polygon to generate bounding box

public static Rectangle getBoundingBox(Polygon polygon) {

    double boundsMinX = Double.MAX_VALUE; // bottom south latitude of the bounding box.
    double boundsMaxX = Double.MIN_VALUE; // top north latitude of bounding box.

    double boundsMinY = Double.MAX_VALUE; // left longitude of bounding box (western bound).
    double boundsMaxY = Double.MIN_VALUE; // right longitude of bounding box (eastern bound).

    for (int i = 0; i < polygon.getPoints().size(); i++) {
        double x = polygon.getPoints().get(i).latitude;
        boundsMinX = Math.min(boundsMinX, x);
        boundsMaxX = Math.max(boundsMaxX, x);

        double y = polygon.getPoints().get(i).longitude;
        boundsMinY = Math.min(boundsMinY, y);
        boundsMaxY = Math.max(boundsMaxY, y);
    }
     //Rectangle(double left, double bottom, double right, double top)
     return new Rectangle(boundsMinY, boundsMinX, boundsMaxY, boundsMaxX);
}
Nauman Zubair
  • 1,208
  • 15
  • 27
-1

I'm not sure I fully understand your question, but usually screen-to-map coordinates are done using a few methods, probably something like that:

Projection projection = googleMaps.getProjection();

Point p = projection.toScreenLocation(point);
LatLng topLeft = projection.fromScreenLocation(new Point(p.x - halfWidth, p.y - halfHeight));
LatLng bottomRight = projection.fromScreenLocation(new Point(p.x + halfWidth, p.y + halfHeight));

Edit:

on the above

  • point is the LatLng you received on your onMapClick.
  • p is the on screen position of said click event
  • p.x - halfWidth, p.y - halfHeight, p.x + halfWidth, p.y + halfHeight is a bounding box around the click location with 2 * halfWidth of width and 2 * halfHeight of height.
  • topLeft and bottomRight is a bounding box around the click event in Latitude-Longitude coordinates.
Budius
  • 39,391
  • 16
  • 102
  • 144
  • If I understand you correctly you are referring to the dimensions of the visible screen. That is not want I want. I want to calculate a bounding box around the touch position. – JJD Feb 11 '15 at 16:42
  • is the bound box specified in pixels or in meters? – Budius Feb 11 '15 at 16:52
  • But what is the value of `halfWidth`? That is the problem I am facing. – JJD Feb 11 '15 at 17:12
  • It's the number of pixels you want your box to be. – Budius Feb 11 '15 at 17:13
  • @Budius I am not sure I want to express the dimensions of the box in pixels. I rather want to express them in "ground units" (such as meters or centimeters), which is difficult to do since coordinates come in WGS84. – JJD Feb 12 '15 at 09:06
  • @JJD it's a bit more complex, but not impossible. That will narrow down pretty much to a math/geometry question than directly Android related. You probably will be interested in this http://stackoverflow.com/questions/837872/calculate-distance-in-meters-when-you-know-longitude-and-latitude-in-java and that https://developer.android.com/reference/android/location/Location.html#distanceBetween(double, double, double, double, float[]) . Possibly will use a interative approach (loop) or calculate the inverse of that equation to find out Meters-to-Degrees on that location. – Budius Feb 12 '15 at 09:48
  • @JJD this question http://stackoverflow.com/questions/5857523/calculate-latitude-and-longitude-having-meters-distance-from-another-latitude-lo seems to be calculating what u need. – Budius Feb 12 '15 at 09:52